Interactive machine-learning playground
Build, evolve and inspect an AI that plays Snake
Snake AI is a hands-on introduction to neuroevolution. Create a population of simple neural networks, let a genetic algorithm improve it, and watch the strongest snake play—entirely in your browser.
Ready to experiment?
A little snake.
A lot to learn.
Start with instinct. Evolve a strategy. Watch a population of neural networks learn to play, one generation at a time.
The essentials
What makes the snakes improve?
You do not need a machine-learning background to start. These are the three ideas the lab makes visible. For a fuller walkthrough, read how a neural network learns to play Snake.
Genetic algorithm
A genetic algorithm improves a whole population through trial and selection. Networks that score well are more likely to become parents; their weights are mixed and slightly mutated to form the next generation.
Neural network
A neural network turns what the snake sees—food, walls and its body—into a move. Its adjustable connection weights shape those decisions; evolution searches for better weight combinations.
This lab
Set the network shape, population and evolution rules, then compare live games, fitness charts and network activity. Save a promising population, change one variable and continue the experiment.
Experiments worth trying
Start with the defaults, then change one variable at a time. Compare tournament and rank-slice selection, raise the mutation rate, increase the population, or enable a fixed food seed to make two runs directly comparable.
- Use a larger population for broader exploration.
- Lower mutation after scores begin to stabilise.
- Average several games per snake to reduce luck.
- Save promising populations and continue them later.
Frequently asked questions
Is this reinforcement learning?
No. The snakes do not learn through backpropagation or Q-learning. A genetic algorithm evaluates complete neural networks and evolves their weights over generations.
What is a generation?
A generation is one full round of evaluation for the population. After the snakes have played and received fitness scores, the best candidates are selected to create the next generation.
What is fitness?
Fitness is the score used to compare networks. The selected fitness function rewards useful behaviour such as eating food, surviving and making progress, rather than simply choosing moves at random.
Why do some generations score worse than the previous one?
Evolution is exploratory, not a straight line upward. Crossover and mutation can produce weaker candidates, and a different game can introduce luck. Watch the overall trend and preserve strong populations by saving them.
What do population size and mutation rate change?
A larger population explores more possible strategies each generation, usually at a higher computational cost. Mutation adds variation: too little can make progress stall, while too much can disrupt promising strategies.
What can the snake see?
The network receives information about food, walls and the snake's body in eight directions. You can switch to binary vision when you want it to detect presence instead of distance.
How can I make training run faster?
Use a smaller population or fewer games per snake, reduce the board size, and enable multi-threaded evaluation when your browser supports it. Faster runs are useful for quick experiments, but may explore fewer strategies.
Does this use NEAT?
No. NEAT evolves network topology as well as weights. Here you choose a fixed network shape and the genetic algorithm evolves its weights.
Does training happen on a server?
No. The simulation runs locally in WebAssembly and browser workers. Your populations remain on your device unless you choose to download a save file.
Can I save a trained population?
Yes. Pause the run and download the current population as JSON. You can load that file later and continue training or simply watch it play.
Where are my settings and saved populations stored?
Settings are remembered in this browser. Populations are only kept when you download them as a JSON file and remain on your device unless you choose to share that file yourself.