Mapping an integer to an array
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开发者_如何学C 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...
}
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.)
As Kerrek said, you filled map_dir with automatic scoped variables, which means they are automatically destroyed when the function ended.
void fill_map_axis(std::map<int, directions*> &map_dir){
//This variable will be destroyed when the function ends
directions array_x[2] = {RIGHT,LEFT};
map_dir[0] = array_x; //should have a warning at least, probably not compile
What you probably want is actual directions in the map
void fill_map_axis(std::map<int, std::pair<directions, directions> > &map_dir){
//This variable will be destroyed when the function ends
std::pair<directions, directions> array_x = {RIGHT,LEFT};
map_dir[0] = array_x; //makes a copy
精彩评论