TicTacToe-Minimax is a Python implementation of the classic Tic Tac Toe game featuring an unbeatable Minimax AI.
The bot evaluates all possible moves using the Minimax algorithm and chooses the optimal play to either win or draw.
This project is a lightweight educational example for learning:
- Minimax decision-making
- Game AI logic
- Recursive algorithms
- Turn-based game implementation in Python
- Fully playable Tic Tac Toe game in console
- Human vs AI (Player is 'O', AI is 'X')
- Minimax AI ensures the bot never loses
- Detects wins, draws, and prevents invalid moves
- Simple 3x3 grid UI in the console
- Dynamic input validation and user prompts
- Game Grid: Represented as a dictionary with positions 1-9
- Player Input: Human selects positions to place 'O'
- Computer AI: Uses Minimax function to determine optimal move
- Winner Check: Checks horizontal, vertical, and diagonal lines
- Draw Detection: Game ends if all spaces are filled with no winner
- Recursive Minimax: Explores all possible moves, maximizing AI's outcome while minimizing the player's chance to win
- Clone the repository
git clone https://github.com/HTANV/TicTacToe-Minimax.git
cd TicTacToe-Minimax- Run the game using Python 3.x
python TicTacToe-Minimax.py- Gameplay Instructions
- The computer goes first ('X')
- Enter positions 1-9 to place your mark ('O')
- Grid layout:
1 || 2 || 3
===========
4 || 5 || 6
===========
7 || 8 || 9
- The game announces winner or draw automatically
The Minimax algorithm is a recursive decision-making method:
- Maximizing Player: AI ('X') tries to get the highest score
- Minimizing Player: Human ('O') tries to reduce AI's score
- Evaluates all possible moves until terminal states (win, lose, draw)
- Chooses the optimal move to never lose
- Python 3.x
- No additional packages required
This project runs entirely in the console
- The AI is unbeatable using Minimax logic
- Designed for learning and demonstration of game AI
- Single file project — easy to modify or extend
- Can be extended for GUI using Tkinter, PyGame, or Unity Python integrations
Contributions welcome:
- Add GUI front-end
- Enhance AI with Alpha-Beta pruning
- Add multiple difficulty levels
- Expand grid size for NxN TicTacToe variants
Play Tic Tac Toe, beat the Minimax AI if you can!