LC: Design Snake Game - spiralgo/algorithms GitHub Wiki
The Essence:
The thing to notice is that the snake is just some set of points on the grid that the the game is being played on. when the snake moves in this direction, the game ends if the snake touches the boundaries of the grid or if any two points in the snake are equal.
The snake can't eat other food before eating the one with a previous index.
This problem especially focuses of the design of classes for interacting/communicating systems. With this problem, the problem-solver can also see how components of a dynamic system interact with each other when there is some input from the user. Handling such problems is a useful skill to have especially in designing dynamic websites and video games.
Details:
The snake can be represented as a linked list of points or a grid of values, the latter being harder to implement. The current food can be updated with the index based upon the number of food eaten.
Further explanation and code can be found here: https://github.com/spiralgo/algorithms/pull/349