Learn

How a neural network learns to play Snake

This is a guided tour of the ideas behind Snake AI — a browser-based neuroevolution lab where a genetic algorithm trains neural networks to play Snake. No machine-learning background is needed. When a term is introduced here, you can go straight to the lab and watch it happen.

▶ Open the lab

What "neuroevolution" means

There are two broad ways to teach a neural network. The familiar one, used by most deep-learning systems, is gradient descent: you show the network many labelled examples and nudge its weights to reduce error. Snake has no labelled "correct move" for every board, so this project uses the other approach — neuroevolution. Instead of teaching one network, you breed a whole population of them. The networks that happen to play better are more likely to become parents, and each new generation inherits and slightly varies what worked. Over many generations the population drifts toward strategies that eat more food and survive longer. It is Darwinian selection applied to network weights.

Nothing here is reinforcement learning, and nothing is NEAT. There is no reward-signal backpropagation and no evolving of the network's shape — you fix the shape and the genetic algorithm searches for good weights.

What the snake can see

A neural network only knows what you feed it. Each snake looks outward in eight directions — the four straight lines plus the four diagonals — and in every direction it measures three things: how far away the food is, how far away its own body is, and how far away the wall is. Eight directions times three signals gives 24 inputs, and those 24 numbers are the network's entire view of the world on each step.

By default those signals are graded by distance, so the snake senses not just whether something is ahead but roughly how near it is. Switch on binary vision and each signal collapses to a simple yes/no — a harder, more abstract problem that some strategies still solve. The output layer is always four values, one per move (up, down, left, right); the snake takes whichever move scores highest.

What the snake can see The snake casts eight rays from its head; a red ray reaches the food, orange rays reach its own body, and the rest reach the walls.
Eight rays cast from the head. Here one reaches the food (red) and two reach the snake's own body (orange); the rest reach the walls. Each ray reports a distance, so 8 directions × 3 things = 24 inputs.

The neural network in the middle

Between those 24 inputs and 4 outputs sit one or more hidden layers. Every connection between neurons has a weight, and the full list of weights is the network's "genome" — the thing evolution actually edits. A wider or deeper network can represent more elaborate strategies but has a longer genome, so it takes more generations to tune. You choose the hidden activation function (sigmoid, ReLU, tanh or leaky ReLU); it decides how strongly each neuron passes its signal on. Because the input and output counts are fixed by the game, changing the network shape starts a fresh population — the genome length changes, so old and new genomes are not comparable.

A neural network turning vision into a move 24 vision inputs feed a hidden layer that feeds four move outputs; green connections excite, red connections inhibit, and one lit path chooses "right". excites (+weight) inhibits (−weight) up down left right ✓ 24 vision inputs hidden layer 4 moves
The same view you get live in the lab: green links excite, red links inhibit, and the strongest path through the network picks the move — here, "right".

Fitness: what "good" means

Evolution needs a single number to rank snakes, and that number is fitness. The Classic fitness balances staying alive against eating food, so early snakes are rewarded just for not dying immediately — an important stepping stone before any of them can find food reliably. The Score-priority fitness leans hard toward food eaten and only lightly rewards survival, which pushes a population that has already learned to move safely into hunting more aggressively. Because a single game involves luck (where the food happens to appear), you can average several games per snake so that fitness reflects skill rather than a lucky board.

Building the next generation

One generation is one full round: every snake plays, each gets a fitness score, and then three genetic operators produce the next population.

Selection — choosing the parents

Rank-slice selection keeps the top fraction of the population and draws parents uniformly from that surviving slice. Tournament selection instead samples a small group at random and keeps the fittest of them; larger tournaments apply more pressure toward the very best snakes, which speeds convergence but can narrow exploration. A share of the strongest individuals is also preserved unchanged (elitism), so a good solution is never lost to bad luck in breeding.

Crossover — mixing two parents

Single-point crossover picks one break point and takes the genes before it from parent A and the rest from parent B. Uniform crossover decides each gene independently, coin-flip style, from either parent — a more thorough mixing that can combine features from both parents more finely.

Mutation — introducing new ideas

Without mutation a population can only recombine what it already has and will stall. Random-reset mutation replaces a gene with a brand-new random weight; Gaussian mutation nudges a gene by a small random amount, preserving learned structure while still exploring nearby. The mutation rate sets how many genes change; too little and progress stalls, too much and promising strategies get scrambled. A common pattern is to explore with more mutation early, then lower it once scores stabilise.

Why progress is bumpy

Watch the score-per-generation chart in the lab and you will see it climb overall but dip along the way. That is normal: crossover and mutation are exploratory, so any generation can produce weaker candidates, and a different food layout adds noise. What matters is the trend, not any single generation. When you find a population you like, save it to a file — it is the only copy, since nothing is stored on a server.

Everything runs in your browser

There is no training server. The entire simulation — the game, the neural networks, and the genetic algorithm — runs locally in WebAssembly, and evaluation is spread across a pool of Web Workers to use the cores your device already has. Your populations never leave your machine unless you choose to download a save file and share it yourself.

Try it yourself

Change one variable at a time and compare — the fastest way to build intuition:

▶ Start an experiment

Common questions

Is this reinforcement learning?

No. There is no reward signal and no backpropagation. A genetic algorithm scores complete networks and evolves their weights across generations.

Does it use NEAT?

No. NEAT evolves the network's topology as well as its weights. Here the shape is fixed and only the weights evolve.

How many inputs does the network have?

24: eight directions, each reporting the distance to food, to the snake's own body and to the wall. The four outputs are the possible moves.

How long does it take to see good snakes?

It depends on population size, network shape and mutation. You will usually see steady improvement within a few dozen generations, and with the default settings genuinely good snakes typically emerge around 400 generations. Larger networks take longer.

Can I reproduce someone else's run?

Yes. Enable the fixed food seed and share your experiment link; anyone who opens it starts from the same settings.

An unhandled error has occurred. Reload