In this project, I recreated the old game "Snake" and created an AI that can never lose!
A Simple solution for this problem would be to generate a path that goes through every tile and connects to itself at the end, the snake can than just repeat this cycle indefinitely.

To generate such a path, I used my Maze Generator as a base, and expanded it. 
Any of these mazes with a grid of (Width x Height) Can be turned into a cycle path of size 
(Width*2 x Height*2) by "walking" down the maze and always holding left, turning the walls into the cycle path. 

After this I optimized the Algorithm further so the snake could decide to skip chunks of the path, when it was deemed safe to do so. This way It avoids having to do the whole loop, no matter where the target is. 
In The Demo:
Press UP/DOWN to change speed of snake
Press Space to reset the snake

You may also like

Back to Top