Projects Mar 2024 Solo
Snake AI
Side-by-side comparison of A*, GBFS, and UCS search algorithms in a live snake game; benchmarked across three maze configurations with multi-processing.
- Python
- Pygame
- Multiprocessing
Three agents, three search algorithms, one board. Each agent runs in its own process so the comparison is live and simultaneous, with all agents sharing the same random seed for food placement.
Algorithms
A* (A-star)
Priority combines actual path cost and a Manhattan distance heuristic to the food. Balances exploration with exploitation: avoids unnecessary detours while not being reckless about path cost.
Greedy Best-First Search (GBFS)
Priority is the heuristic only; path cost is ignored. Moves aggressively toward food, which is fast in open space but produces suboptimal routes when obstacles create longer detours.
Uniform Cost Search (UCS)
Priority is path cost only, with no heuristic. Guaranteed to find the shortest path but explores significantly more nodes than A*, especially in maze configurations.
Body-aware pathfinding
At each node expansion, every algorithm projects the snake’s body segments forward to the hypothetical next position before evaluating it. This lets agents navigate in tight spaces close to the tail without collisions. The benefit compounds at higher snake lengths, where a naive planner would treat its own body as a permanent wall.
Benchmarks
No-hurdles maze (target: 500)
| Algorithm | Time |
|---|---|
| A* | 1:33.17 |
| GBFS | 1:33.79 |
| UCS | 1:35.73 |
Small grid 30x30 (target: 300)
| Algorithm | Time |
|---|---|
| A* | 37.41 s |
| GBFS | 38.61 s |
| UCS | 38.58 s |
Standard 60x60 (target: 500)
| Algorithm | Time |
|---|---|
| A* | 2:44.53 |
| GBFS | 3:13.83 |
| UCS | 2:48.54 |
Infinite mode: 60x60, last to stop wins
8:56
A* (1st)
Length 111
7:34
GBFS (2nd)
Length 91
3:36
UCS (3rd)
Length 58
A* wins every configuration. The heuristic guidance becomes increasingly valuable at higher snake lengths, where dead-end avoidance outweighs the speed advantage GBFS has in open space.