Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6590129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:15:41+00:00 2026-05-25T17:15:41+00:00

I recently started with C++; I’m an hobby programmer, and I know a bit

  • 0

I recently started with C++; I’m an hobby programmer, and I know a bit of Python.

I programmed a little snake. I wanted to insert another snake guided by the computer.

I decided to put the possible direction that the snake can take in an enum:

enum directions{UP, DOWN, RIGHT, LEFT, IN, OUT, FW, RW,NONE};

void fill_map(std::map<directions,V4> &map_vec);

void fill_map(std::map<int, directions*> &map_dir);

void fill_map(std::map<directions,directions> &map);

and map the enum for the needed function:

void fill_map(std::map<directions,V4> &map_vec){
    map_vec[UP] = V4(0,1,0,0);
    map_vec[DOWN] = V4(0,-1,0,0);
    //others

  }

 void fill_map(std::map<directions, directions> &map){
    map[UP]= DOWN;
    map[DOWN]= UP;
    //others
  }


void fill_map_axis(std::map<int, directions*> &map_dir){
    directions array_x[2] = {RIGHT,LEFT};
    map_dir[0] = array_x;

    directions array_y[2] = {UP,DOWN};//store the array
    map_dir[1] = array_y;

    directions array_z[2] = {FW,RW};//store the array
    map_dir[2] = array_z;

    directions array_w[2] = {IN,OUT};//store the array
    map_dir[3] = array_w;

}

The fill_map functions are called in the snake constructor.
Basically what I wanted to do in the fill_map_axis is to map an integer corresponding to the index of the coordinate (0 coord x, 1 coord y etc) and map the two directions that move along those axis.
So I stored an array of two directions.

Now I call the function:

directions SnakeCPU::find_dir(V4 point){
    //point is the target point
    directions dir;
    int index = get_coord_index(point); //get the index where to move
    double diff = head_pos[index]-point[index]; //find the difference between the head and the target point

    directions* axis = dir_coords[index]; //call the map containing the directions stored in an array.

    if(diff<0.){

        dir = *axis; //use the first
    }
    else if(diff>0.) {
        axis++;
        dir = *axis; //use the second
    }
    else{
        dir = NONE;
    }

    return dir;
}

Although the map are initialized in the Snake constructor, it seems that the returned value from the pointer axis is a random memory block.

So my question: do you see a mistake in the code? did I used the pointer axis with sense?

I’m really not expert with pointer; in Python the map is instantiated with a dictionary like this:

dir_coords = {0:[LEFT,RIGHT], ...}

so I just need to call it:

axis = dir_coords[index]
dir = axis[0]
#or
dir = axis[1]

edit:

Snake constructor:

Snake::Snake()
{    
    fill_map(dir_vectors);
    fill_map(dir_coords);
    fill_map(opposite_dir);
    head_pos = V4(0.,0.,0.,0.);
    //other stuff...
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T17:15:42+00:00Added an answer on May 25, 2026 at 5:15 pm

    Just a shot in the blue, here is how I would design this.

    #include <map>
    
    enum EDirection { NONE = 0, UP, DOWN, RIGHT, LEFT, IN, OUT, FW, RW };
    
    typedef std::map<EDirection, V4>           DirectionMap;
    typedef std::pair<EDirection, EDirection> DirectionPair;
    typedef std::map<int, DirectionPair>            PairMap;
    
    extern const DirectionMap map_vec {
      { UP,   (0, 1, 0, 0) },
      { DOWN, (0,-1, 0, 0) },
      // ...
    };   // using C++11 initialization lists for convenience
    
    extern const PairMap map_dir {
      { 0, { RIGHT, LEFT } },
      { 1, { UP, DOWN }    },
      // ...
    };
    

    Here I decided to make map_vec and map_dir global constants, because I gathered that that’s essentially what they are. To initialize those, I rely on the new C++11 initialization syntax. If that’s not an option, we can also fill the map in the traditional way:

    PairMap map_dir;
    map_dir.insert(std::make_pair(0, DirectionPair(RIGHT, LEFT)));
    map_dir.insert(std::make_pair(1, DirectionPair(UP, DOWN)));
    // ...
    
    DirectionMap map_vec;
    map_vec.insert(std::make_pair(UP,   V4(0, 1, 0, 0)));
    map_vec.insert(std::make_pair(DOWN, V4(0,-1, 0, 0)));
    // ...
    

    (Yes, you can also write map_dir[0] = DirectionPair(RIGHT, LEFT)'. I don’t like the square brackets, though, they feel too violent for my taste.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently started using the Yii framework trying to learn a little bit more
I recently started learning Python and I was rather surprised to find a 1000
Recently started on python, wondered what the equivalent object was for storing session &
I recently started learning 0MQ . Earlier today, I ran into a blog, Python
I recently started learning Objective-C 2.0, with a book, and I want to know
I recently started this question in another thread (to which Reed Copsey graciously responded)
I recently started work on a personal coding project using C++ and KDevelop. Although
I recently started a new webforms project and decided to separate the business classes
I recently started using Eclipse at work for my Java servlet projects. I've been
I recently started learning Emacs . I went through the tutorial, read some introductory

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.