A complete implementation of the classic Three Men's Morris board game with an AI opponent using minimax algorithm with alpha-beta pruning. Features both a web-based GUI and command-line interface.
Three Men's Morris is a strategy board game played on a 3×3 grid with diagonal connections. Players take turns placing and then moving their pieces to form a line of three pieces (horizontally, vertically, or diagonally).
- Placement Phase: Players alternate placing their 3 pieces on empty positions
- Movement Phase: Once all pieces are placed, players move their pieces to adjacent empty positions
- Victory: First player to form a line of 3 pieces wins
- Adjacent Movement: Pieces can only move to connected positions (including diagonals through the center)
- Web-based GUI: Modern, responsive interface with visual board representation
- AI Opponent: Minimax algorithm with alpha-beta pruning and configurable difficulty
- Command-line Interface: Terminal-based gameplay option
- Game Controls: Undo moves, restart games, and step-by-step gameplay
- Real-time Updates: Live game state tracking and victory detection
- Cross-platform: Works on Windows, macOS, and Linux
- Python 3.7+
- FastAPI
- uvicorn (for web server)
-
Clone or download the project files:
# Ensure you have these files: # - main.py (game logic) # - app.py (web server) # - index.html (web interface) # - styles.css (styling) # - script.js (frontend logic)
-
Install dependencies:
pip install fastapi uvicorn
-
Start the web server:
uvicorn app:app --reload
-
Open your browser and navigate to:
http://localhost:8000 -
Play the game:
- Click "New Game" to start
- Click on empty positions to place pieces during placement phase
- Click on your pieces then click destination during movement phase
- Use "AI Move" button to let the computer play
- Adjust AI difficulty with the search depth slider (1-6)
Run the game directly in your terminal:
python main.pyFollow the on-screen prompts to:
- Start new games
- Make manual moves
- Let the AI play
- Undo moves
- Restart games
The AI uses the Minimax algorithm with alpha-beta pruning:
-
Evaluation Function:
- +100,000 for winning positions
- -100,000 for losing positions
- +1 for each potential winning line (2 pieces + 1 empty)
- -1 for each opponent's potential winning line
-
Search Depth: Configurable from 1-6 levels
- Depth 1-2: Easy (fast, basic moves)
- Depth 3-4: Medium (good strategy)
- Depth 5-6: Hard (strong play, slower)
-
Optimization: Alpha-beta pruning significantly reduces search space
three-mens-morris/
├── main.py # Core game logic and CLI
├── app.py # FastAPI web server
├── index.html # Web interface HTML
├── styles.css # Web interface styling
├── script.js # Frontend JavaScript logic
└── README.md # This file
Gameclass (main.py): Core game logic, board state, move validation- FastAPI server (
app.py): REST API endpoints for web interface - Web UI: Modern, responsive interface with real-time updates
- AI Engine: Minimax with alpha-beta pruning for optimal play
- New Game: Start a fresh game
- Undo Move: Revert the last move
- Restart: Reset current game
- AI Move: Let computer play current turn
- Search Depth: Adjust AI difficulty (1-6)
- Manual Action: Make your own move
- Computer Action: Let AI play with custom depth
- Undo Move: Revert last move
- Restart Game: Start over
- Quit: Exit the game
- Control the Center: The middle position (1,1) connects to all other positions
- Block Opponent Lines: Prevent your opponent from forming lines of 2
- Create Multiple Threats: Set up situations where you can win on the next move
- Think Ahead: Consider your opponent's possible responses
- Endgame Planning: In movement phase, mobility becomes crucial
The web server provides these REST API endpoints:
GET /start- Start a new gameGET /undo- Undo the last moveGET /get_board_state- Get current game statePOST /place- Place a piece (placement phase)POST /move- Move a piece (movement phase)GET /get_computer_move?depth=N- Get AI move suggestionGET /get_adjacent_positions?x=X&y=Y- Get valid moves from position
-
"Module not found" error:
pip install fastapi uvicorn
-
Web interface not loading:
- Ensure the server is running (
uvicorn app:app --reload) - Check that you're accessing
http://localhost:8000 - Verify all files (HTML, CSS, JS) are in the same directory
- Ensure the server is running (
-
AI taking too long:
- Reduce search depth to 3 or lower
- Higher depths (5-6) require more computation time
-
Port already in use:
uvicorn app:app --reload --port 8001
Feel free to enhance this project! Some ideas:
- Add different board sizes (6 Men's Morris, 9 Men's Morris)
- Implement online multiplayer
- Add move history visualization
- Create mobile app version
- Improve AI evaluation function
This project is open source and available under the MIT License.
- Classic Three Men's Morris game rules
- Minimax algorithm with alpha-beta pruning
- Modern web technologies for the interface
Enjoy playing Three Men's Morris! 🎮