Skip to content

Conversation

@ErdemT09
Copy link
Collaborator

@ErdemT09 ErdemT09 commented Jul 3, 2021

Resolves: #85

Algorithm:

The thing that we can notice is that the snake is just a linked list of points on the grid that we are playing the game on. Using this intuition, we can easily represent the snake using a linked list of points, for which we can create a simple auxiliary class.

To move in any given input direction, we can peek the head of the snake and increment it in the given direction. If this new head of the snake intersects any of the points of the snake besides its moving tail, this means that the game should be over.
If this new head tried to move outside the bounds of the grid, this means that the game should be over.

Since all food does not appear on the screen at once, rather one-by-one, we can keep track of which food is on the screen through the score. If we the new head is in some food tile, then we increment the score counter and return the new score, with the incremented score also operating to make the new food "appear" on the grid.
If snake doesn't eat food, that means its length stays constant and its tail should be removed.
After doing all these, we return the score if possible.

@ErdemT09 ErdemT09 marked this pull request as ready for review July 3, 2021 15:26
Copy link
Collaborator

@altay9 altay9 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice problem to grasp the basics of object-oriented programming.
LinkedList is a good choice for this context, indeed.

Very nice, clean, and modular coding style. Thank you, Erdem.

@ErdemT09 ErdemT09 merged commit bd9e0ec into master Jul 4, 2021
@ErdemT09 ErdemT09 deleted the 353.-Design-Snake-Game branch July 4, 2021 03:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

353. Design Snake Game

3 participants