A complete implementation of the classic Dots and Boxes game with both a web interface and command-line version, featuring an AI opponent powered by the minimax algorithm with alpha-beta pruning.
Dots and Boxes is a paper-and-pencil game where players take turns drawing lines between dots to form boxes. When a player completes a box (all four sides), they score a point and get another turn. The player with the most boxes wins!
- Web Interface: Modern, responsive UI with smooth animations
- Command Line Interface: Terminal-based gameplay for classic experience
- AI Opponent: Intelligent computer player using minimax algorithm
- Configurable Board Size: Play on boards from 2x2 to 8x8
- Move History: Undo moves to explore different strategies
- Real-time Updates: Live score tracking and game state visualization
- Multiple Difficulty Levels: Adjustable AI search depth (1-6 levels)
-
Install Dependencies
pip install fastapi uvicorn
-
Start the Server
uvicorn app:app --reload
-
Open Your Browser Navigate to
http://localhost:8000
python main.pydots-and-boxes-solver/
├── app.py # FastAPI web server
├── main.py # Core game logic and CLI interface
├── index.html # Web interface HTML
├── styles.css # Web interface styling
├── script.js # Web interface JavaScript
└── README.md # This file
- Setup: Choose board dimensions (height and width)
- Start: Click "New Game" to begin
- Make Moves: Click on the gray lines to place them
- Score: Complete boxes by drawing the fourth side
- Win: Player with the most boxes when all lines are drawn wins
- New Game: Start with custom board dimensions
- Computer Move: Let the AI make a move (adjustable difficulty)
- Undo Move: Take back the last move
- Restart Game: Reset with same board size
- Quit Game: Return to setup screen
Follow the menu prompts to:
- Create new games with custom dimensions
- Make manual moves by specifying coordinates
- Request computer moves with custom search depth
- Undo moves and restart games
The AI opponent uses a sophisticated minimax algorithm with:
- Alpha-Beta Pruning: Optimized search for better performance
- Position Evaluation: Smart scoring based on:
- Completed squares (±10 points)
- Squares ready to capture (±5 points penalty/bonus)
- Configurable Depth: Choose AI thinking depth (1-6 levels)
- Strategic Play: Considers both offensive and defensive moves
- Depth 1-2: Beginner (fast, basic strategy)
- Depth 3-4: Intermediate (balanced play)
- Depth 5-6: Expert (deep analysis, slower but stronger)
- FastAPI: Modern web framework for the API
- Game Logic: Complete dots and boxes implementation
- AI Algorithm: Minimax with alpha-beta pruning
- Move Validation: Comprehensive rule checking
- State Management: Full game state tracking and history
- Responsive Design: Works on desktop and mobile
- Real-time Updates: Live game state synchronization
- Interactive Board: Click-to-play interface
- Visual Feedback: Smooth animations and hover effects
- Modern Styling: Clean, professional appearance
POST /create- Create new gamePOST /make_move- Make a movePOST /switch- Switch playersGET /game_state- Get current stateGET /get_best_move- Get AI movePOST /undo- Undo last move
- Minimum: 2x2 (simple games)
- Maximum: 8x8 (complex games)
- Recommended: 3x3 to 5x5 for best experience
Adjust the search depth in the web interface or when prompted in CLI:
- Higher depth = stronger play but slower response
- Lower depth = faster response but simpler strategy
- Python 3.7+
- FastAPI
- Uvicorn (for web server)
# Install dependencies
pip install fastapi uvicorn
# Start development server with auto-reload
uvicorn app:app --reload --host 0.0.0.0 --port 8000
# Or run CLI version
python main.py- Game Class: Core game logic in
main.py - FastAPI Routes: Web API in
app.py - Frontend: HTML/CSS/JS for web interface
- Minimax AI: Advanced algorithm for computer moves
- Players alternate drawing lines between adjacent dots
- When a player completes a box (all 4 sides), they:
- Score 1 point
- Get another turn
- Game ends when all possible lines are drawn
- Player with the most boxes wins
- Ties are possible when players have equal boxes
- Server won't start: Check if port 8000 is available
- Moves not registering: Ensure you're clicking on gray lines, not filled ones
- AI taking too long: Reduce search depth for faster moves
- Board not displaying: Check browser console for JavaScript errors
- Chrome/Chromium (recommended)
- Firefox
- Safari
- Edge
Potential improvements:
- Online multiplayer support
- Tournament mode
- Move analysis and hints
- Game replay system
- Advanced AI personalities
- Sound effects and animations
This project is open source and available under the MIT License.
Contributions are welcome! Feel free to:
- Report bugs
- Suggest features
- Submit pull requests
- Improve documentation
Enjoy playing Dots and Boxes! 🎯