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.
Think about collision - you want to check if the snake collides with itself. Accessing a grid at the position to which the head moves is constant, searching the whole body of the snake is not. If a player is somewhat good, the snake will at some point fill most of the grid anyway, which means that there is not really any memory saved.
However: You have two other tasks at hand: Deleting the tail when the snake moves and painting the snake. For painting it, I assume that you want the player to be able to see how it wiggles, so you need an order in which the snake traverses the fields. This sounds like a queue for the positions of the snake.
We have talked about memory, but... is memory relevant in here? 400 tiles or so is nothing for a modern machine. The thing about having to search the whole body is still relevant, because that is less readable than accessing a grid tile.
Therefore, how I'd do it: Have one 2D-vector representing the grid for collisions AND a queue for the snake. Everything but the snake can be stored in the 2D-vector, if we are talking about food and walls. Go with that for now, implement it and see if problems arise, in the worst case, you will acquire experience, eh?