Docs


Snake

A snake learns to chase food around a grid without hitting the walls or its own body. The genetic algorithm evolves a tiny neural network from wandering into walls to growing long.

Last updated October 20, 2026

Go to the live page Open the Snake demo

The Snake demo is the classic grid game. A snake moves one cell at a time around a walled board, eats the food, and grows a little longer with every bite. The genetic algorithm evolves a small neural network to steer it. The first snakes wander straight into a wall. After a few generations they head for the food, and avoiding their own growing body becomes the hard part.

It lives alongside the other live-agent games (Flappy Bird, the Self-Driving Car, Dino Runner, Breakout, Pong, Gem Collector and Road Crossing) under the same Live Agent demo. Use the tabs at the top of the page to switch between them.

The Problem

The board is 15 by 15 cells. The snake starts three cells long in the middle, facing right, and one piece of food sits on a random free cell. Hitting a wall or the snake's own body ends the run.

Steering is relative to the way the snake is facing: turn left, go straight, or turn right. That means the snake can never reverse into itself in a single move. The network reads nine inputs: three danger flags (is the cell ahead, to the left or to the right blocked), the current heading, and whether the food is in front of or behind the head and to its left or right. One hidden layer of eight neurons feeds the three steering outputs, and the largest one wins.

There is also a one-step safety check. If the network's favourite move would run into a wall or the body on the very next step and a safe move exists, the safe move is taken instead. The decision panel marks any move that was blocked this way, so you can see when the snake didn't follow its first choice.

Fitness rewards each piece of food with a big bonus, gives a little for every step toward the food and takes a little away for every step away from it. Each step also costs a tiny amount, so circling has a price. A crash costs a penalty. A run also ends after 100 moves without eating, or after 675 moves in total, so a snake that loops forever can't pile up points.

How a Run Looks

Click Start. The current best network plays a game live, with the three steering scores shown each step. The chart on the right tracks the best score for each cycle.

The first generations wander aimlessly. Survival climbs as the network learns to avoid danger, and once it figures out the food the snake starts getting longer and longer.

Playing It Yourself

Switch the mode toggle from Agent to You, or press M, to take over. Steer with the arrow keys or the on-screen pad. The arrows are absolute directions, and pressing the direction straight behind the snake does nothing. With no key pressed the snake keeps going straight. The same 100-moves-without-food rule applies to you. Press P to pause or resume and Esc to reset. Switch back to Agent at any time and the evolved network picks up from where you left off.

Settings Worth Trying

Open Settings to tune the genetic algorithm for the next run. These only affect Agent mode:

  • Population size. More snakes per generation means more variety to select from, at the cost of a slower cycle. The default is 30.
  • Generations per cycle. How much evolving happens between each live game you watch, from 1 to 15. The default is 10.
  • Mutation rate. How much the networks are randomly perturbed each generation. A little keeps the search exploring; too much stops it from settling on a good policy.

What the Achievement Rewards

Signed-in users unlock the Snake charmer achievement the first time an evolved snake eats at least eight pieces of food in one of the games you watch in Agent mode. Your own games in You mode don't count toward it.

Snake records and shares like the other demos. See Sharing and recording for how to capture a clip of a long snake.

For more on how the demos catalog works, see What the Demos Do.

Thanks for the feedback!