Snake Game
Watch as the snake automatically plays itself using AI pathfinding
💡 Press SPACEBAR to toggle Start/Stop · Grid size and obstacles can only be changed when stopped
How It Works
AI Pathfinding
The snake uses Breadth-First Search (BFS) algorithm to find the shortest path to the food. It recalculates the path every move, considering its own body and obstacles as blocked cells.
Game Rules
The game operates on a 50×50 grid with 5 obstacles. When the snake eats food, it grows. The game automatically restarts after 5 seconds if the snake gets stuck with no valid path to food.
Visual Elements
Green - Snake body
Orange - Food
Gray - Obstacles
Pro Tips
- • The game automatically restarts every 5 seconds - just sit back and watch
- • The snake's length increases each time it eats food
- • Watch how the AI navigates around obstacles to reach the food
- • Use fullscreen mode for an immersive experience
- • In fullscreen, press 'H' to toggle control hints
Frequently Asked Questions
How does the snake AI work?
The snake uses the Breadth-First Search (BFS) algorithm to find the shortest path from its head to the food. BFS guarantees finding the optimal route by exploring all possible moves level by level. The algorithm treats the snake's body segments and obstacles as blocked cells.
What happens when the snake is stuck?
If the snake cannot find any valid path to the food (all routes are blocked by its own body or obstacles), the game ends immediately. The display shows "Game Over!" with the final snake length, then automatically restarts a fresh game after 5 seconds.
How large is the game grid?
The game uses a 50×50 grid, giving plenty of space for the snake to grow and navigate. At the start of each game, exactly 5 obstacles are randomly placed on the grid to create interesting pathfinding challenges.
Does the snake grow in length?
Yes! Each time the snake eats food, it gains one additional segment in length. The new segment is added to the head position, and the rest of the body shifts forward. This is why the pathfinding algorithm must continuously recalculate - the snake's own body becomes an increasing obstacle.
Can I control the snake manually?
No, this is an automated AI game. The snake plays completely by itself using its pathfinding algorithm. Just sit back, relax, and watch the game unfold. You can enable fullscreen mode for an immersive viewing experience.
How often does the pathfinding algorithm run?
The pathfinding algorithm recalculates the path every game step (every move the snake makes). This ensures the snake can adapt to changes in the game state, such as when it eats food and grows, or when obstacles block its previous path.
Pro Tips
- • Watch how the BFS algorithm efficiently finds the shortest path - you'll see the snake take optimal routes around obstacles.
- • The game becomes more challenging as the snake grows - a longer body means more cells are blocked, making pathfinding harder.
- • Obstacles are placed randomly each game - some games will be easier than others depending on their positioning.
- • The 5-second restart gives you time to see the final snake length and plan your next observation.
- • Try fullscreen mode and set it as a screensaver on an extra monitor - it's oddly satisfying to watch!
- • The algorithm is efficient enough to recalculate instantly - there's no lag even with a long snake and full grid.
Update Logs
View the latest updates and features