A command-line Tic Tac Toe (noughts and crosses) game in Python with artificial intelligence at three difficulty levels.
- Game mode: Human player vs AI
- Three difficulty levels:
- Easy: Random moves
- Medium: Blocks player's threats
- Hard: Minimax algorithm for perfect play
- CLI Interface: Clean and intuitive terminal
- Input validation: Robust error handling
- Replayability: Ability to play multiple games
- Python 3.6+
- Git (optional)
# Clone the repository
git clone <repository-url>
cd tic-tac-toe
# Create virtual environment
python3 -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install dependencies (none for base version)
pip install -r requirements.txt
# Launch the game
python main.py
- Launch the game:
python main.py
- Choose difficulty: 1 (Easy), 2 (Medium), 3 (Hard)
- Make your move: Enter coordinates in
row,column
format (ex:0,1
) - You are X, AI is O
- Replay: Choose to replay after each game
# Unit tests
python -m unittest tests.test_board tests.test_ai
# Quick validation test
python test_game.py
tic-tac-toe/
├── main.py # Main entry point
├── test_game.py # Quick validation tests
├── requirements.txt # Python dependencies
├── README.md # Documentation
├── MEMO/ # Development documentation
│ ├── context.md # Context and objectives
│ ├── decisions.md # Technical decisions
│ ├── errors_log.md # Error history
│ └── progress.md # Progress status
├── src/ # Source code
│ ├── __init__.py # Python package
│ ├── board.py # Game board management
│ ├── ai.py # Artificial intelligence
│ └── game.py # Main game logic
├── tests/ # Unit tests
│ ├── test_board.py # Board tests
│ └── test_ai.py # AI tests
└── docs/ # Technical documentation
-
Board
: 3x3 board state management- Move validation
- Win detection
- Board display
-
AI
: Artificial intelligence with three levels- Easy:
random.choice()
on empty cells - Medium: Winning/blocking move search
- Hard: Minimax algorithm with alpha-beta pruning
- Easy:
-
TicTacToeGame
: Game orchestrator- Turn management
- CLI user interface
- Main game loop
The hard AI uses the Minimax algorithm with optimization:
- Maximum depth: 9 (complete board)
- Scores: +10 (AI win), -10 (player win), 0 (draw)
- Optimization: Preference for quick wins
- Test coverage: > 80%
- Cyclomatic complexity: < 10 per function
- AI response time: < 50ms (hard)
- Code size: < 500 lines total
- Fork the project
- Create a feature branch (
git checkout -b feature/AmazingFeature
) - Commit changes (
git commit -m 'Add some AmazingFeature'
) - Push to the branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
- Inspired by classic Tic Tac Toe games
- Developed with TDD (Test-Driven Development) approach
- Modular architecture for easy extension
🎉 Have fun!