A web-based implementation of the classic Nine Men's Morris (Mills) board game with an AI opponent using minimax algorithm.
- Interactive Web Interface: Modern, responsive UI with visual game board
- AI Opponent: Intelligent computer player using minimax algorithm with alpha-beta pruning
- Game Phases: Complete implementation of placing, moving, and flying phases
- Mill Detection: Automatic detection of mills (three pieces in a row)
- Undo Functionality: Step back through game history
- Adjustable AI Difficulty: Configure minimax search depth (1-5 levels)
- Real-time Game State: Live updates of piece counts and game status
Nine Men's Morris is played in three phases:
- Players alternate placing pieces on empty intersections
- Each player has 9 pieces to place
- Form mills (3 pieces in a row) to remove opponent pieces
- Move pieces to adjacent empty intersections
- Continue forming mills to remove opponent pieces
- When a player has only 3 pieces left, they can "fly" to any empty spot
- Game ends when a player has fewer than 3 pieces or cannot move
- The other player wins
- Python 3.7+
- pip
-
Clone or download the project files
-
Install dependencies:
pip install -r requirements.txt
-
Run the application:
uvicorn app:app --reload
-
Open your browser and navigate to:
http://localhost:8000
mills-game/
├── app.py # FastAPI web server and API endpoints
├── main.py # Core game logic and AI implementation
├── index.html # Web interface HTML
├── styles.css # CSS styling for the game board
├── script.js # Client-side JavaScript logic
├── requirements.txt # Python dependencies
└── README.md # This file
main.py: Contains theGameclass with complete Mills game logic, minimax AI, and command-line interfaceapp.py: FastAPI server providing REST API endpoints for the web interface
index.html: Main web page with game board and controlsstyles.css: Modern styling with hover effects and responsive designscript.js: Handles user interactions, board updates, and API communication
| Endpoint | Method | Description |
|---|---|---|
/ |
GET | Serves the main HTML page |
/get_game_state |
GET | Returns current game state |
/start |
POST | Starts a new game |
/place |
POST | Places a piece during placing phase |
/move |
POST | Moves a piece during moving phase |
/remove_piece |
POST | Removes opponent piece after mill |
/undo |
POST | Undoes the last move |
/computer_move |
POST | Triggers AI move with specified depth |
/get_opponent_pieces |
GET | Gets removable opponent pieces |
The computer opponent uses:
- Minimax Algorithm: Evaluates game positions up to specified depth
- Alpha-Beta Pruning: Optimizes search performance
- Strategic Priorities:
- Form mills when possible
- Block opponent's potential mills
- Use minimax evaluation for optimal moves
- Piece count advantage: +10 points per piece difference
- Two-in-a-row formations: +5 points each
- Win/loss positions: ±1000 points
- Click "Restart Game" to begin
- Click empty intersections to place/move pieces
- Use "Computer Move" with desired difficulty (depth 1-5)
- "Undo Last Move" to step back in game history
Run python main.py for a terminal-based version with the same game logic.
- Placing Phase: Click empty spots to place pieces
- Moving Phase: Click your piece, then click destination
- Mill Formation: Automatically prompts to remove opponent piece
- Computer Difficulty: Adjust depth (1=easy, 5=hard)
- 7x7 grid with 24 valid positions
- Adjacency mapping for legal moves
- 16 possible mill lines (rows, columns, squares)
- Complete move history for undo functionality
- Separate tracking of pieces on board vs. pieces lost
- Phase detection (placing/moving/flying)
- Backend: FastAPI with Python
- Frontend: Vanilla JavaScript, HTML5, CSS3
- Styling: Custom CSS with modern design patterns
- Communication: RESTful API with JSON
To modify or extend the game:
- Game Logic: Edit
main.py- theGameclass contains all rules - AI Behavior: Modify the minimax implementation and evaluation function
- Web Interface: Update
script.jsfor new features,styles.cssfor appearance - API: Add new endpoints in
app.py
This project is proprietary software. All rights reserved under copyright.
Contributions welcome! Areas for improvement:
- Enhanced AI strategies
- Multiplayer support
- Game statistics tracking
- Mobile app version
- Tournament mode