A strategic game theory solver for the classic Colonel Blotto problem, featuring both a web interface and command-line interface.
Colonel Blotto is a classic game theory problem where two players simultaneously distribute their troops across multiple battlefields. Each player wins a battlefield if they allocate more troops to it than their opponent. The player who wins the most battlefields wins the overall game.
- Web Interface: Modern, responsive web application with real-time solving
- Command Line Interface: Terminal-based solver for quick calculations
- Robust Strategy Finding: Finds the most robust pure strategy using minimax principles
- Comprehensive Analysis: Calculates win/draw/lose probabilities against all possible opponent strategies
- Permutation Handling: Considers all possible arrangements of troop distributions
colonel-blotto-solver/
├── app.py # FastAPI web server
├── main.py # Core game logic and CLI interface
├── index.html # Web interface
├── styles.css # Custom styling
├── script.js # Frontend JavaScript
└── README.md # This file
- Python 3.7+
- pip package manager
-
Clone or download the project files
-
Install dependencies:
pip install fastapi uvicorn
-
Run the application:
uvicorn app:app --reload
-
Access the web interface at
http://localhost:8000
- Open your browser and navigate to
http://localhost:8000 - Enter the number of troops and fields
- Click "Solve Puzzle" to find the optimal strategy
- View the recommended troop distribution
Run the standalone solver:
python main.pyFollow the prompts to:
- Enter number of troops
- Enter number of fields
- View the optimal strategy and probability analysis
The solver uses a minimax approach to find the most robust pure strategy:
- Generate Combinations: Creates all possible ways to distribute troops across fields
- Permutation Analysis: Considers all arrangements of each combination
- Battle Simulation: Simulates battles between all player and opponent permutations
- Robustness Evaluation: Finds the strategy that maximizes the minimum (win_prob - lose_prob) difference
- Strategy Selection: Returns the most robust strategy against worst-case opponents
For each potential strategy, the solver:
- Tests it against every possible opponent strategy
- Calculates win/draw/lose probabilities
- Finds the worst-case scenario (minimum win-lose difference)
- Selects the strategy with the best worst-case performance
Input: 5 troops, 3 fields
Output: [2, 2, 1] - Deploy 2 troops to first field, 2 to second field, 1 to third field
This means the optimal strategy is to distribute your 5 troops as 2-2-1 across the three battlefields.
GET /- Serves the web interfacePOST /new_game?troops={n}&fields={m}- Initialize a new gameGET /solve_blotto- Solve the current game and return optimal strategy
The solver returns:
[
[2, 2, 1], // Optimal troop distribution
0.6667, // Win probability
0.1667, // Draw probability
0.1667, // Lose probability
0.5000 // Minimum win-lose difference
]- Game: Main class handling troop combinations and battle simulation
- get_combos(): Recursively generates all valid troop distributions
- calculate_outcome(): Determines winner of individual battles
- solve_blotto(): Implements the minimax strategy selection
- FastAPI: Modern Python web framework for the API
- Vanilla JavaScript: Frontend interaction and API calls
- Tailwind CSS: Utility-first CSS framework for styling
- Fork the repository
- Create a feature branch
- Make your changes
- Test thoroughly
- Submit a pull request
This project is open source and available under the MIT License.
Colonel Blotto is a fundamental problem in game theory that demonstrates:
- Zero-sum games: One player's gain equals another's loss
- Resource allocation: Optimal distribution of limited resources
- Strategic thinking: Anticipating opponent moves
- Minimax principle: Maximizing minimum expected payoff
The solver finds pure strategies rather than mixed strategies, making it practical for real-world applications where you need a single, concrete plan of action.
- Focuses on pure strategies only (not mixed strategies)
- Assumes symmetric games (both players have same number of troops)
- Computational complexity grows exponentially with troops and fields
- Best suited for small to medium-sized games (≤20 troops, ≤10 fields)
- Support for asymmetric games (different troop counts)
- Mixed strategy Nash equilibrium solver
- Performance optimizations for larger games
- Historical game analysis and learning
- Multi-player variant support