BattleSnake Bot – CyberSnake
What Inspired Us
We wanted to build a BattleSnake bot that plays aggressively instead of defensively.
The idea was simple: hunt food first to grow, then use size and board position to eliminate opponents.
We were also interested in combining classic pathfinding and spatial algorithms with Monte Carlo Tree Search (MCTS) to create a bot that can make strong decisions within the 500 ms move limit.
Algorithms used include:
- A* for shortest path food targeting
- Flood Fill for space evaluation and trap detection
- Voronoi partitioning for territory control
- Monte Carlo Tree Search (MCTS) for probabilistic move simulation
How We Built It
We implemented a Python Flask API server that receives the Battlesnake game state every turn and returns the best move.
Pipeline Overview
Parse & Analyze
- Parse the board state from the Battlesnake engine.
- Compute:
- Obstacles
- Danger squares (where equal or larger enemy heads can move)
- Kill squares (where we win head-on collisions)
Mathematically, we treat board state as a grid:
$$ B = {(x,y) \mid 0 \le x < W,\; 0 \le y < H} $$
Where:
- $W$ = board width
- $H$ = board height
Pathfinding
We use multiple algorithms to evaluate movement options:
- A* → find shortest path to food
- Flood Fill → evaluate reachable space and detect traps
- Voronoi → estimate territory control against other snakes
Example evaluation scoring:
$$ Score_{move} = w_f \cdot Food + w_s \cdot Space + w_t \cdot Territory + w_a \cdot Aggression $$
Strategy Layer
Each move is scored using heuristics such as:
- Food urgency
- Available space
- Territory control
- Aggressive hunting when larger than opponents
Core principle:
Bigger snake = head-on wins + more board control
MCTS Refinement
After heuristic scoring, we run short Monte Carlo simulations.
MCTS evaluates possible future board states:
$$ MoveScore = 0.6 \cdot Heuristic + 0.4 \cdot MCTS $$
This balances fast heuristic evaluation with simulation-based decision making.
Deployment
The bot is deployed as a containerized service.
Stack
- Python
- Flask API
- Docker
- Gunicorn
This allows the snake to run as a scalable HTTP service during matches.
Challenges We Faced
Time Budget
Battlesnake enforces a 500 ms decision limit, so we had to:
- Optimize heuristics
- Limit MCTS playouts
- Avoid expensive computations
Enemy Prediction
Opponent snakes are unpredictable.
We implemented simple assumptions such as:
- Hungry snakes move toward food
- Larger snakes act aggressively
MCTS simulations help explore possible enemy responses.
Aggression vs Safety
Our goal was to hunt and kill, but still avoid:
- Danger squares
- Edge traps
- Low-space moves
We used phase-based weighting:
- Early game → growth
- Mid game → territory
- Late game → aggression
- Desperation → survival
Trap Detection
We implemented time-aware flood fill so the snake's own tail is treated as temporarily passable, preventing corner traps.
What We Learned
- How to combine heuristic evaluation with Monte Carlo Tree Search for real-time game AI.
- Using Voronoi-style territory control for spatial reasoning.
- Designing a modular architecture:
Log in or sign up for Devpost to join the conversation.