Snake Algorithm
Autumn 2019
I recreated the classic Snake game, allowing user control, and then integrated a pathfinding algorithm to govern the movements of the snake player.
Key Features
- User Control: Originally, this Snake game allowed users to control the snake by changing its direction. The embedded game below is the version with user controls enabled.
- Algorithms: The algorithm demonstrated finds the shortest current route to the food while maneuvering around its own tail. However, this method requires recalculating the route with each move as the tail moves. It can still result in collisions with the tail, especially when the tail is long enough and already positioned around the next food. Another, albeit less dynamic, algorithm that effectively solves the problem involves moving along each row one by one, leaving a single column free to loop back. While this approach can lead to achieving the longest possible snake, it may not be suitable for more complex scenarios.
- Queue Structure: To smoothly extend the snake when food is eaten, a block is enqueued to the snake. While this may not be the fastest implementation, it was straightforward to set up and served well for a quick prototype.
Challenges, solutions and lessons learnt
My primary challenge was pathfinding around the snake's tail. However, this problem becomes significantly easier if you approximate the tail using a fixed grid and pathfind around it. To further enhance this approach, I would consider caching distances to food within the grid and updating the grid based on the new gap created by the end of the tail. This would allow for the recalculation of information only as necessary.
Embedded Game
![Placeholder Image](../Assets/Snake.png)