A feature-rich, command-line implementation of Tic Tac Toe (also known as Noughts and Crosses) built with Rust. Supports multiple players, AI opponents with varying difficulty levels, and customizable board sizes.
- Flexible Player Support: 2-4 players in a single game
- Game Modes: Human vs Human, Human vs AI, or AI vs AI
- Adjustable Board Size: Play on boards from 3x3 to 10x10
- AI Opponents: Three difficulty levels:
- Easy: Random moves
- Medium: 70% smart moves, 30% random
- Hard: Strategic play with win-seeking, blocking, and fork prevention
- Unique Emoji Symbols: 12 distinct animal emoji for player identification
- Clean CLI Interface: Formatted grid with row/column indicators
- Modular Architecture: Well-organized library code suitable for extension
- Rust and Cargo (install via rustup.rs)
git clone https://github.com/vikyw89/rust-tic-tac-toe.git
cd rust-tic-tac-toe
cargo build --releasecargo run
# or for release builds
cargo run --releaseThe game prompts for configuration:
- Board size (3-10, default: 3)
- Number of players (2-4, default: 2)
- For each player:
- Type: Human (1) or AI (2)
- Name (for humans) or auto-generated (for AI)
- AI difficulty (if AI player)
Enter row and column as 0-based coordinates:
Enter your move (row col): 1 1
Example board output:
0 1 2
-------------
0 | X | | O |
-------------
1 | | X | |
-------------
2 | | | |
-------------
rust-tic-tac-toe/
├── src/
│ ├── lib.rs # Library root and public exports
│ ├── game.rs # Game state and flow management
│ ├── game_board.rs # Board implementation and win detection
│ ├── player.rs # Player struct and logic
│ ├── ai.rs # AI decision-making logic
│ ├── ui.rs # Command-line interface
│ ├── types.rs # Shared types, enums, errors
│ └── score_board.rs # Player statistics tracking
├── recipes/
│ └── tic_tac_toe.rs # Main game executable
├── tests/
│ └── ai_performance.rs # AI performance benchmarks
├── Cargo.toml
└── README.md
Symbol(char) // Emoji character wrapper
GameStatus // InProgress, Win(winner), Draw
Difficulty // Easy, Medium, Hard
GameError // InvalidMove, MaxPlayersReached, GameOver, etc.
BoardError // InvalidPosition, CellOccupied, OutOfBounds
PlayerError // NotFound, InvalidDataGame::new()- Create game with board size and max playersadd_player()- Register players with unique symbolsmake_move()- Execute a move if validis_over()/winner()- Check game staterandomize_turn()- Random starting player
- NxN grid with configurable size
- Win detection for rows, columns, and diagonals
- Move validation and undo capability
- Position evaluation for AI
| Difficulty | Strategy |
|---|---|
| Easy | Random position selection |
| Medium | 70% strategic, 30% random |
| Hard | Prioritizes: center > wins > blocks > forks > corners |
cargo testRun AI performance benchmarks:
cargo test test_three_player_ai_performance -- --nocapturerand = "0.8.5"- Randomization for symbols, turns, and AI moves
MIT License - See LICENSE for details.
- Fork the repository
- Create a feature branch
- Commit your changes
- Push to the branch
- Open a Pull Request