I am making a snake game. I have two classes, snake
and food
. When I call snake->move()
it needs to check that there are no collisions and for this purpose it needs to know what food
's position is and therefore requires a pointer to food
. food
has a function that moves it to a new random position, move->setNewPosition()
, which needs to know the position of snake
so that it doesn't collide with the snake when it moves. For this purpose, it requires a pointer to snake
.
Therefore, for both classes, I would need to supply a pointer to the other class which must be initialised. But to initialise the other class I need to initialise the other class and so on. Is there any way to initialise two classes, that require pointers to each other, at the same time?
If not, what would be a better way of structuring my program that would allow me to check the coordinates of the other class?