|
| 1 | +# Tic-Tac-Toe AI Agent - Architecture Document |
| 2 | + |
| 3 | +## Project Definition |
| 4 | + |
| 5 | +This project implements a command-line Tic-Tac-Toe game where a human player competes against an AI opponent. The AI uses the Minimax algorithm with depth-first search to play optimally, ensuring it never loses and will either win or draw every game. |
| 6 | + |
| 7 | +### Goals |
| 8 | +- Create an interactive command-line Tic-Tac-Toe game |
| 9 | +- Implement an unbeatable AI using the Minimax algorithm |
| 10 | +- Provide a clean, modular architecture with clear separation of concerns |
| 11 | +- Ensure the code is well-documented, tested, and follows Rust best practices |
| 12 | +- Build a system that can be easily extended or modified |
| 13 | + |
| 14 | +## Components and Modules |
| 15 | + |
| 16 | +The project is architected using a layered, modular approach with clear separation of responsibilities: |
| 17 | + |
| 18 | +### Core Modules |
| 19 | + |
| 20 | +#### 1. **Board Module (`board.rs`)** |
| 21 | +**Responsibility**: Physical game state representation and basic operations |
| 22 | +- Represents the 3x3 game board as a 1D array for efficiency |
| 23 | +- Manages cell states (Empty, Occupied by Player X/O) |
| 24 | +- Handles move placement and position validation |
| 25 | +- Provides board querying methods (empty positions, full board check) |
| 26 | +- Implements clean display formatting |
| 27 | + |
| 28 | +#### 2. **Game Module (`game.rs`)** |
| 29 | +**Responsibility**: Game logic, rules, and state management |
| 30 | +- Manages overall game state (InProgress, Won, Draw) |
| 31 | +- Handles player turns and move validation |
| 32 | +- Implements win condition detection using pattern matching |
| 33 | +- Provides game flow control (reset, state transitions) |
| 34 | +- Acts as the central coordinator between other modules |
| 35 | + |
| 36 | +#### 3. **AI Module (`ai.rs`)** |
| 37 | +**Responsibility**: Intelligent opponent using Minimax algorithm |
| 38 | +- Implements the Minimax algorithm with depth-first search |
| 39 | +- Evaluates all possible game states recursively |
| 40 | +- Scores positions: +10 for AI win, -10 for AI loss, 0 for draw |
| 41 | +- Optimizes for winning quickly and losing slowly |
| 42 | +- Provides unbeatable gameplay that never loses |
| 43 | + |
| 44 | +#### 4. **UI Module (`ui.rs`)** |
| 45 | +**Responsibility**: User interaction and interface management |
| 46 | +- Handles command-line input/output operations |
| 47 | +- Manages game flow (start, play rounds, restart) |
| 48 | +- Provides user-friendly error messages and feedback |
| 49 | +- Displays board state and position guides |
| 50 | +- Coordinates between human input and AI responses |
| 51 | + |
| 52 | +#### 5. **Main Application (`main.rs`)** |
| 53 | +**Responsibility**: Entry point and module coordination |
| 54 | +- Orchestrates all modules |
| 55 | +- Initializes the game system |
| 56 | +- Provides the main execution flow |
| 57 | + |
| 58 | +### Architecture Justification |
| 59 | + |
| 60 | +This modular design follows several key principles: |
| 61 | + |
| 62 | +**1. Single Responsibility Principle**: Each module has one clear purpose |
| 63 | +- Board: Data representation |
| 64 | +- Game: Business logic |
| 65 | +- AI: Intelligence algorithms |
| 66 | +- UI: User interaction |
| 67 | + |
| 68 | +**2. Separation of Concerns**: Clear boundaries between layers |
| 69 | +- Data layer (Board) is independent of game rules |
| 70 | +- Game logic is separate from AI implementation |
| 71 | +- UI is decoupled from core game mechanics |
| 72 | + |
| 73 | +**3. Dependency Direction**: Clean dependency flow |
| 74 | +``` |
| 75 | +main.rs → ui.rs → game.rs → board.rs |
| 76 | + → ai.rs ↗ |
| 77 | +``` |
| 78 | + |
| 79 | +**4. Testability**: Each module can be unit tested independently |
| 80 | +- 11 tests for Board operations |
| 81 | +- 14 tests for Game logic |
| 82 | +- 6 tests for AI behavior |
| 83 | +- 4 integration tests for UI |
| 84 | + |
| 85 | +**5. Extensibility**: Easy to modify or extend |
| 86 | +- AI algorithm can be swapped without affecting other modules |
| 87 | +- UI can be replaced (web, GUI) without changing core logic |
| 88 | +- Game rules can be modified independently |
| 89 | + |
| 90 | +## Usage |
| 91 | + |
| 92 | +### Building and Running |
| 93 | + |
| 94 | +```bash |
| 95 | +# Navigate to project directory |
| 96 | +cd topics/tic-tac-toe |
| 97 | + |
| 98 | +# Build the project |
| 99 | +cargo build |
| 100 | + |
| 101 | +# Run the game |
| 102 | +cargo run |
| 103 | + |
| 104 | +# Run tests |
| 105 | +cargo test |
| 106 | +``` |
| 107 | + |
| 108 | +### Gameplay Experience |
| 109 | + |
| 110 | +#### 1. **Game Start** |
| 111 | +The game displays a welcome message and position guide: |
| 112 | +``` |
| 113 | +🎮 Welcome to Tic-Tac-Toe! 🎮 |
| 114 | +You are X, AI is O. |
| 115 | +Enter positions 1-9 corresponding to board positions: |
| 116 | +
|
| 117 | +Board positions: |
| 118 | + 1 | 2 | 3 |
| 119 | +----------- |
| 120 | + 4 | 5 | 6 |
| 121 | +----------- |
| 122 | + 7 | 8 | 9 |
| 123 | +``` |
| 124 | + |
| 125 | +#### 2. **Gameplay Flow** |
| 126 | +- Human player (X) always goes first |
| 127 | +- Enter positions 1-9 to place your mark |
| 128 | +- AI automatically responds with optimal moves |
| 129 | +- Game displays board state after each move |
| 130 | +- Clear feedback for invalid moves or occupied positions |
| 131 | + |
| 132 | +#### 3. **Example Game Session** |
| 133 | +``` |
| 134 | +🆕 Starting new game! |
| 135 | +
|
| 136 | + | | |
| 137 | +----------- |
| 138 | + | | |
| 139 | +----------- |
| 140 | + | | |
| 141 | +
|
| 142 | +Your move (1-9): 5 |
| 143 | +
|
| 144 | + | | |
| 145 | +----------- |
| 146 | + | X | |
| 147 | +----------- |
| 148 | + | | |
| 149 | +
|
| 150 | +🤖 AI is thinking... |
| 151 | +🤖 AI plays position 1 |
| 152 | +
|
| 153 | + O | | |
| 154 | +----------- |
| 155 | + | X | |
| 156 | +----------- |
| 157 | + | | |
| 158 | +
|
| 159 | +Your move (1-9): 9 |
| 160 | +``` |
| 161 | + |
| 162 | +#### 4. **Game Results** |
| 163 | +The game automatically detects and displays results: |
| 164 | +- **Human Win**: "🎉 Congratulations! Player X wins! 🎉" |
| 165 | +- **AI Win**: "🎉 Congratulations! Player O wins! 🎉" |
| 166 | +- **Draw**: "🤝 It's a draw! Well played both players! 🤝" |
| 167 | + |
| 168 | +#### 5. **Replay Option** |
| 169 | +After each game, players can choose to play again: |
| 170 | +``` |
| 171 | +🔄 Would you like to play again? (y/n): y |
| 172 | +``` |
| 173 | + |
| 174 | +### AI Behavior |
| 175 | + |
| 176 | +The AI implements optimal Minimax strategy: |
| 177 | +- **Defensive**: Automatically blocks human winning moves |
| 178 | +- **Offensive**: Takes immediate winning opportunities |
| 179 | +- **Strategic**: Chooses moves that maximize long-term advantage |
| 180 | +- **Unbeatable**: Mathematical guarantee of never losing |
| 181 | + |
| 182 | +### Development and Testing |
| 183 | + |
| 184 | +#### Running Tests |
| 185 | +```bash |
| 186 | +# Run all tests (35 total) |
| 187 | +cargo test |
| 188 | + |
| 189 | +# Run specific module tests |
| 190 | +cargo test board::tests |
| 191 | +cargo test game::tests |
| 192 | +cargo test ai::tests |
| 193 | +cargo test ui::tests |
| 194 | + |
| 195 | +# Run with verbose output |
| 196 | +cargo test -- --nocapture |
| 197 | +``` |
| 198 | + |
| 199 | +#### Code Quality |
| 200 | +```bash |
| 201 | +# Check for compilation issues |
| 202 | +cargo check |
| 203 | + |
| 204 | +# Format code |
| 205 | +cargo fmt |
| 206 | + |
| 207 | +# Run clippy for additional lints |
| 208 | +cargo clippy |
| 209 | +``` |
| 210 | + |
| 211 | +### Technical Specifications |
| 212 | + |
| 213 | +- **Language**: Rust (Edition 2024) |
| 214 | +- **Dependencies**: Standard library only |
| 215 | +- **Board Representation**: 1D array of 9 positions |
| 216 | +- **AI Algorithm**: Minimax with depth-first search |
| 217 | +- **Input Validation**: Comprehensive error handling |
| 218 | +- **Test Coverage**: 35 unit and integration tests |
| 219 | +- **Performance**: Instant AI response for all game states |
| 220 | + |
| 221 | +This architecture provides a robust, maintainable, and extensible foundation for the Tic-Tac-Toe game while demonstrating clean code principles and effective modular design. |
0 commit comments