A beautiful, modern Tic Tac Toe game where you can challenge an AI opponent powered by the minimax algorithm.
- ๐จ Beautiful UI: Modern design with TailwindCSS and smooth animations
- ๐ค Smart AI: Minimax algorithm ensures the AI plays optimally
- ๐ Score Tracking: Persistent score tracking using localStorage
- ๐ฏ Responsive Design: Works perfectly on desktop and mobile
- โก Real-time Gameplay: Instant feedback and smooth interactions
- ๐ Game Reset: Easy game reset functionality
- You are X (red), the AI is O (blue)
- Click on any empty cell to make your move
- The AI will automatically respond with its move
- First to get three in a row (horizontally, vertically, or diagonally) wins!
- If the board fills up with no winner, it's a draw
- Python 3.7 or higher
- Flask
-
Clone or download this repository
-
Navigate to the project directory:
cd XO
-
Install Flask if you don't have it:
pip install flask
python -m flask run --debug
python app.py
The application will start on http://127.0.0.1:5000
XO/
โโโ app.py # Main Flask application
โโโ templates/
โ โโโ index.html # Frontend HTML template
โโโ static/
โ โโโ main.js # Game logic and UI interactions
โโโ utils/
โ โโโ __init__.py
โ โโโ game_instructions.py # Core game logic (winner checking, draw detection)
โ โโโ minimax.py # Minimax algorithm implementation
โ โโโ find_best_move.py # AI move selection
โโโ tests/
โ โโโ test_xo_game.py # Unit tests for game functions
โโโ README.md # This file
- Returns the main game page with an empty board
- Body:
{"board": [[...], [...], [...]]}
- Response: Game state after AI move
- Example:
{ "game_status": "ongoing", "result": "ONGOING", "board": [["X", " ", " "], [" ", "O", " "], [" ", " ", " "]] }
- Resets the game to initial state
- Response: Empty board and reset game status
The AI uses the minimax algorithm to evaluate all possible future game states and choose the optimal move. This ensures the AI plays perfectly and will never lose (at worst, it will draw).
- AI Win: +1 point
- Human Win: +1 point
- Draw: +1 point
- Scores are saved in browser localStorage
- Class-based Architecture: Clean, maintainable code structure
- Async/Await: Modern JavaScript for API communication
- Local Storage: Persistent score tracking
- Responsive Design: Mobile-friendly interface
- Animations: Smooth transitions and feedback
- Error Handling: Graceful error management and user feedback
- RESTful API: Clean API design
- Error Handling: Comprehensive error management
- Game State Management: Robust board state handling
- Minimax AI: Optimal move calculation
cd tests
python test_xo_game.py
The codebase is designed to be easily extensible:
- Game logic is in
utils/game_instructions.py
- AI logic is in
utils/minimax.py
andutils/find_best_move.py
- Frontend logic is in
static/main.js
- UI styling uses TailwindCSS classes
Try entering the Konami Code (โโโโโโโโBA) while playing! ๐ฎ
- Chrome/Edge: Full support
- Firefox: Full support
- Safari: Full support
- Mobile browsers: Full support
Feel free to fork this project and submit pull requests for any improvements!
This project is open source and available under the MIT License.