Note that there are some explanatory texts on larger screens.

plurals
  1. POMapping an integer to an array
    primarykey
    data
    text
    <p>I recently started with C++; I'm an hobby programmer, and I know a bit of Python.</p> <p>I programmed a little snake. I wanted to insert another snake guided by the computer.</p> <p>I decided to put the possible direction that the snake can take in an enum:</p> <pre><code>enum directions{UP, DOWN, RIGHT, LEFT, IN, OUT, FW, RW,NONE}; void fill_map(std::map&lt;directions,V4&gt; &amp;map_vec); void fill_map(std::map&lt;int, directions*&gt; &amp;map_dir); void fill_map(std::map&lt;directions,directions&gt; &amp;map); </code></pre> <p>and map the enum for the needed function:</p> <pre><code>void fill_map(std::map&lt;directions,V4&gt; &amp;map_vec){ map_vec[UP] = V4(0,1,0,0); map_vec[DOWN] = V4(0,-1,0,0); //others } void fill_map(std::map&lt;directions, directions&gt; &amp;map){ map[UP]= DOWN; map[DOWN]= UP; //others } void fill_map_axis(std::map&lt;int, directions*&gt; &amp;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; } </code></pre> <p>The fill_map functions are called in the snake constructor.<br> 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. </p> <p>Now I call the function:</p> <pre><code>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&lt;0.){ dir = *axis; //use the first } else if(diff&gt;0.) { axis++; dir = *axis; //use the second } else{ dir = NONE; } return dir; } </code></pre> <p>Although the map are initialized in the Snake constructor, it seems that the returned value from the pointer axis is a random memory block.</p> <p>So my question: do you see a mistake in the code? did I used the pointer axis with sense?</p> <p>I'm really not expert with pointer; in Python the map is instantiated with a dictionary like this:</p> <pre><code>dir_coords = {0:[LEFT,RIGHT], ...} </code></pre> <p>so I just need to call it:</p> <pre><code>axis = dir_coords[index] dir = axis[0] #or dir = axis[1] </code></pre> <p>edit:</p> <p>Snake constructor:</p> <pre><code>Snake::Snake() { fill_map(dir_vectors); fill_map(dir_coords); fill_map(opposite_dir); head_pos = V4(0.,0.,0.,0.); //other stuff... } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload