Afterglowscreensavers

Snake AI Screensaver

Snake AI is a free screensaver where the snake plays itself on a grid sized to your screen, using breadth-first search with a tail-safety rule and an optional Hamiltonian-cycle strategy. It shows its planned path, its length, step count and current mode, and restarts after a win or a failure. Press ESC to exit.

Live preview

Snake AI screensaver — Snake that plays itself: BFS, tail-safety, or a Hamiltonian cycle.

Press ESC to leave fullscreen. Settings are saved in your browser only.

How the Snake AI screensaver works

Every step the AI runs a breadth-first search from the head to the food. The search knows the tail moves: a body cell counts as free if the tail will have passed it by the time the head arrives. If a path exists it is checked before it is taken: the snake is replayed along the path in memory, and the move is allowed only if the head could still reach its own tail afterwards. That is the safety rule, and it is why the snake rarely traps itself.

When no safe path to the food exists, the mode changes to "stalling for space". The AI picks the neighbouring cell that still leaves a route to the tail and makes that route as long as possible, so the body keeps moving and opens up room. When the snake is crowded it prefers the direction of the cycle described below. If the snake goes 6 × the board area in steps without eating, the game is declared stuck in a loop and restarts.

The Hamiltonian strategy uses a fixed route that visits every cell once and returns to the start; it exists on any grid where the number of cells is even, which is why the grid sides are kept even. The snake follows the cycle and takes shortcuts only when the jump skips no body cell and does not pass the food, which keeps the body in cycle order and cannot trap it. It always fills the board, only more slowly. Switching from the search to the cycle in mid-game is not possible because the body is no longer in cycle order, so in Auto mode the cycle is used for the next game after one that looped or crashed.

The HUD shows length against board size, steps taken, the current mode and a session tally of games and wins. The planned path is drawn as a faint line (gold when stalling), and in cycle mode the whole cycle is traced underneath. A full board shows "board filled" and restarts after a moment. The HUD is hidden on small tiles.

About the search and the cycle

Breadth-first search finds the shortest route over free cells, and the tail-reachability check is a common safety rule for self-playing Snake. It does not guarantee a win on its own, which is why the failures are shown rather than hidden.

A Hamiltonian cycle visits every cell of the board exactly once and returns to its start. A snake that only ever follows it cannot collide with itself, and allowing shortcuts that skip no body cell speeds it up without losing that guarantee. The cost is that the snake spends a lot of time travelling around the board.

This is an independent implementation written for the site. It makes no claim about world records or about how any particular published Snake bot performs.

A worked example

On a 12 × 20 board (240 cells) the Hamiltonian cycle is 240 cells long. In 30 headless test games of this engine's search strategy on that board, 22 filled all 240 cells and 8 were declared stuck in a loop; none crashed into a wall or itself. In test games of the cycle strategy on boards from 4 × 4 to 16 × 28, every game filled the board.

Settings & tips

Frequently asked questions

Does the snake ever lose?
In search mode it rarely crashes because it always keeps a route to its tail, but it can get stuck going round in circles near the end of a game. That is detected and counted as a failed game. The Hamiltonian strategy cannot fail.
What does "stalling for space" mean?
No safe route to the food exists at that moment, so the snake follows a long safe route towards its own tail instead. Its body keeps moving, which usually opens a path to the food.
Why must the grid sides be even?
A closed route through every cell of a rectangular grid exists only if the grid has an even number of cells. The saver rounds both sides down to even numbers.
How long does a full board take?
Many thousands of steps. A 12 × 20 board took roughly 8,000 to 13,000 steps in test games, and larger grids take longer, so at default speed one game can last several minutes.
Is anything random?
The food position and, once the snake has gone a long time without eating, occasional random choices among safe moves to break loops. The search and safety rules are deterministic.
Why show the path?
To make the algorithm visible. The faint line is the breadth-first route to the food, or the route to the tail while stalling, or the next stretch of the cycle.
Does it slow down large grids?
No. Each step is a few breadth-first searches over at most 2,400 cells, and the saver caps the work per frame.

← All Arcade screensavers