A web-based implementation of the classic Othello (Reversi) board game with AI opponent using minimax algorithm with alpha-beta pruning.
- Interactive Web Interface: Clean, modern UI built with HTML, CSS, and JavaScript
- Smart AI Opponent: Minimax algorithm with alpha-beta pruning for challenging gameplay
- Adjustable AI Difficulty: Configure AI search depth from 1 to any desired level
- Game Management: Start new games, undo moves, and track scores
- Real-time Updates: Live board state updates and move validation
- Responsive Design: Works on desktop and mobile devices
Othello is played on an 8×8 board with black and white pieces. Players take turns placing pieces on the board:
- Objective: Have the most pieces of your color when the board is full or no moves are possible
- Valid Moves: You can only place a piece where it will flip at least one opponent's piece
- Flipping: When you place a piece, all opponent pieces in straight lines (horizontal, vertical, diagonal) between your new piece and another of your pieces are flipped to your color
- Turn Skipping: If you have no valid moves, your turn is skipped
- Game End: The game ends when the board is full or neither player can make a move
- Python 3.8 or higher
- pip (Python package manager)
-
Clone or download the project files
git clone <repository-url> cd othello-game
-
Install required dependencies
pip install fastapi uvicorn
-
Start the web server
uvicorn app:app --reload
-
Open your browser and navigate to:
http://localhost:8000
othello-game/
├── app.py # FastAPI web server and API endpoints
├── main.py # Core game logic and AI implementation
├── index.html # Main web interface
├── styles.css # Custom styling for the game board
├── script.js # Frontend JavaScript for game interaction
└── README.md # This file
- Starting a Game: Click "Start New Game" from the main menu
- Making Moves: Click on any valid square on the board to place your piece
- Computer Moves: Click "Computer Move" to let the AI make its move
- Adjusting AI Difficulty: Use the "AI Depth" input to set the search depth (higher = stronger but slower)
- Undo Moves: Click "Undo Move" to revert the last move
- Game Controls: Use the control buttons to start new games or quit
You can also run the game directly in the console:
python main.pyThis provides a text-based interface with the same game features.
The AI uses the minimax algorithm with alpha-beta pruning to determine the best moves:
- Evaluation Function: Difference in piece count between maximizing and minimizing players
- Search Depth: Configurable depth limit (default: 4-8 moves ahead)
- Optimization: Alpha-beta pruning reduces the search space for faster computation
- Strategy: The AI considers all possible moves and their consequences to the specified depth
- Depth 1-2: Beginner (very fast, basic strategy)
- Depth 3-4: Intermediate (good balance of speed and strategy)
- Depth 5-6: Advanced (strong play, moderate computation time)
- Depth 7+: Expert (very strong play, longer computation time)
The FastAPI backend provides the following endpoints:
GET /- Serve the main web interfacePOST /start- Start a new gameGET /get_current_state- Get the current game statePOST /add- Make a player movePOST /computer_move- Execute AI moveGET /undo- Undo the last moveGET /is_game_over- Check if the game is finishedGET /get_winner- Get the game winnerGET /get_valid_moves- Get all valid moves for current player
The Game class manages:
- Board state (8×8 grid)
- Player turns and scoring
- Move validation and execution
- Game history for undo functionality
- Win condition checking
The AI class implements:
- Minimax algorithm with alpha-beta pruning
- Position evaluation
- Move selection and execution
- Configurable search depth
The web interface uses:
- HTML5 for structure
- Tailwind CSS for styling
- Vanilla JavaScript for interactivity
- Fetch API for server communication
uvicorn app:app --reload --host 0.0.0.0 --port 8000You can modify the AI behavior by:
- Changing the evaluation function in
Game.score_board() - Adjusting the search depth in the web interface or console
- Implementing additional pruning techniques in the minimax algorithm
The modular design makes it easy to add features like:
- Different AI difficulty presets
- Game statistics tracking
- Tournament mode
- Custom board sizes
- Advanced evaluation functions
- Typical response times:
- Depth 4: < 1 second
- Depth 6: 1-5 seconds
- Depth 8: 5-30 seconds
- Memory usage: Minimal (< 50MB)
- Browser compatibility: Modern browsers with JavaScript enabled
- Server won't start: Ensure FastAPI and uvicorn are installed
- Slow AI moves: Reduce the AI depth setting
- Interface not loading: Check that you're accessing
http://localhost:8000 - Move validation errors: Ensure you're clicking on valid squares (green highlights)
Run with debug logging:
uvicorn app:app --reload --log-level debugThis project is open source and available under the MIT License.
Contributions are welcome! Please feel free to submit pull requests or open issues for bugs and feature requests.
- Classic Othello game rules
- Minimax algorithm implementation
- Web interface design with Tailwind CSS