Snake AI
I built this snake game with two AIs: a pathfinder that plans a route to the food, and a neural network that learns by trial and error. You can watch them play, compare them in the arena or take over the controls.
Every cell filled. That's a perfect game.
Pathfinder
- Score
- 0
- Efficiency
- –
- Fitness
- 0.00
DQN
- Score
- 0
- Efficiency
- –
- Fitness
- 0.00
Pathfinder 0 : 0 DQN
Both boards get the same food in the same order.
The Pathfinder is playing with its tuned settings.
How it works
Pathfinder
Every move, a breadth-first search finds the shortest path to the food. Before taking it, the snake simulates eating and checks it could still reach its own tail. If not, it follows its tail until a safe route opens. If it goes too long without eating, it tries a riskier route.
DQN
I first trained this network in PyTorch on a Raspberry Pi 5 in January 2025. My first version, a plain DQN, climbed to a best score of 37 and then forgot what it had learned, so I rebuilt it as a Double DQN with a bigger replay memory and steadier exploration. That version, ported to JavaScript, is where the league starts.
The league
An evolution strategy tunes the Pathfinder's move weights. Population-based training adjusts the DQN settings, including learning rate and network size. Both play the same games, and a judge scores them on food collected and path efficiency and adjusts the weight of each measure. The DQN trains on Pathfinder moves, and its value estimates help guide the Pathfinder. Training runs in a background worker, using your graphics card when that's faster.