Snake learns to snake, 2,000 corpses at a time
A Processing sketch that evolves neural-network snakes via genetic algorithm, with all the messy biological details intact.

What it does
This is a Snake solver built in Processing. Each snake carries a small feed-forward neural network (24 inputs, 2 hidden layers of 16 neurons, 4 outputs) that decides which way to slither. The inputs come from 8 “eyes”: in each direction the snake measures distance to food, its own tail, and the wall. No pathfinding, no hardcoded strategy — just weights and luck.
The interesting bit
The fitness function has a built-in death clock. Every snake gets 200 moves, earns 100 more per apple, caps at 500. This deliberately kills loopers — snakes that evolve to circle forever without scoring. It’s a neat little hack against a classic evolutionary trap, and the README is honest about why dips in the score graph can still mean fitness gains.
Key highlights
- Population of 2,000 snakes per generation, with crossover and mutation between parent brains
- Network architecture is now customizable (hidden layers and neuron counts)
- Saves model weights as CSV and exports a generation-by-generation score graph
- Includes a YouTube walkthrough if you want to watch before compiling
Caveats
- Requires Processing IDE to run; not a standalone executable or Python package
- The mutation rate and selection pressure details aren’t specified in the README
- Save/load is manual CSV handling, not a polished model format
Verdict
Good for someone who wants to see neuroevolution happen, not just read about it. Skip it if you’re looking for a pip-installable RL benchmark or SOTA Snake solver.