I want to code a little Snake game in C++ and I want to know what is the best way to represent the game matrix.
I could use a 2DArray (something like vector< vector< int > >) which will contain some values representing different objects (for example : 0 nothing, 1 snake, 2 wall, 3 food, ...) On the other hand, I could use no array for the grid, and use multiples arrays for the differents game objects (for example : vector< Wall >, vector < SnakeTile >, vector< Food >, ...).
Should I also consider using std::deque to represent my snake ?
Thank you for your help.
I think you shold start with writing a wrapper around std vector that provides col, row based access:
and that will represent you grid. Type T (or class) can represent Cell object that will contain all informations about cell. This way your grid data will be packed tightly ane easy to access.
You should also throughoutly think about how your Scake interacts with your Grid, and what will be the rues of that (you can't put a fruit where snake is right?). Then you can impelent these inetractions in your code and that will determine how your Grid and your Snake should exchange infromations (and which of them).