xo-core 0.2.0-alpha

A fast, reusable, and well-documented game engine for Tic-Tac-Toe (Noughts and Crosses) in Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
use crate::types::{Cell, GameState, MoveError, Player};

/// The core Tic-Tac-Toe game engine.
///
/// This struct manages the board, enforces rules, and provides
/// an unbeatable AI opponent using the Minimax algorithm if enabled.
///
/// # Board Representation
/// The board is stored internally as a flat array of 9 cells.  
/// Indices map to positions like this:
///
/// ```text
///  0 | 1 | 2
/// -----------
///  3 | 4 | 5
/// -----------
///  6 | 7 | 8
/// ```
///
/// # Game Modes
/// - **Human vs Human:** Both players call [`make_move`] manually.
/// - **Human vs AI:** calls [`make_move`], then queries
///   [`get_best_move`] to find the AI’s move and applies it with [`make_move`].
///
/// # Examples
///
/// ## Human vs Human
/// ```
/// use xo_core::{GameEngine, Player};
///
/// let mut game = GameEngine::with_ai(false);
///
/// game.make_move(0).unwrap(); // X plays top-left
/// game.make_move(4).unwrap(); // O plays center
///
/// assert_eq!(game.current_player, Player::X);
/// ```
///
/// ## Human vs AI
/// ```
/// use xo_core::GameEngine;
///
/// let mut game = GameEngine::with_ai(true);
///
/// game.make_move(0).unwrap(); // Human plays top-left
///
/// if let Some(ai_move) = game.get_best_move() {
///     game.make_move(ai_move).unwrap(); // Apply AI move
/// }
/// ```
pub struct GameEngine {
    board: [Cell; 9],
    /// The player whose turn it is.
    pub current_player: Player,
    /// Whether the AI is enabled.
    ///
    /// - `true`: Single-player vs AI
    /// - `false`: Human vs Human
    pub ai_enabled: bool,
}

impl GameEngine {
    /// Creates a new instance of the game engine with an empty board.
    ///
    /// The game always starts with `Player::X`.  
    /// By default, AI is **enabled**.
    ///
    /// # Example
    /// ```
    /// use xo_core::{GameEngine, Player, Cell};
    ///
    /// let game = GameEngine::new();
    /// assert_eq!(game.current_player, Player::X);
    /// assert!(game.get_board().iter().all(|&c| c == Cell::Empty));
    /// assert!(game.ai_enabled);
    /// ```
    pub fn new() -> Self {
        Self {
            board: [Cell::Empty; 9],
            current_player: Player::X,
            ai_enabled: true,
        }
    }

    /// Creates a new instance of the game engine with an option to disable AI.
    ///
    /// # Parameters
    /// - `ai_enabled`:  
    ///   - `true`: Single-player vs AI  
    ///   - `false`: Human vs Human
    ///
    /// # Example
    /// ```
    /// use xo_core::GameEngine;
    ///
    /// let game = GameEngine::with_ai(false);
    /// assert!(!game.ai_enabled);
    /// ```
    pub fn with_ai(ai_enabled: bool) -> Self {
        Self {
            board: [Cell::Empty; 9],
            current_player: Player::X,
            ai_enabled,
        }
    }

    /// Returns a reference to the current board.
    pub fn get_board(&self) -> &[Cell; 9] {
        &self.board
    }

    /// Attempts to make a move for the current player at the given board index.
    ///
    /// # Parameters
    /// - `index`: The 0-based cell index (0–8).
    ///
    /// # Returns
    /// - `Ok(())` if the move was made successfully.
    /// - `Err(MoveError)` if the move is invalid:
    ///   - `MoveError::OutOfBounds` if `index >= 9`
    ///   - `MoveError::CellOccupied` if the cell already has a mark
    ///
    /// # Example
    /// ```
    /// use xo_core::{GameEngine, MoveError};
    ///
    /// let mut game = GameEngine::new();
    /// assert!(game.make_move(0).is_ok());
    /// assert_eq!(game.make_move(0), Err(MoveError::CellOccupied));
    /// ```
    pub fn make_move(&mut self, index: usize) -> Result<(), MoveError> {
        // First, check if the index is within the valid range of the board.
        if index >= 9 {
            return Err(MoveError::OutOfBounds);
        }

        // Then, check if the cell is already occupied.
        if self.board[index] != Cell::Empty {
            return Err(MoveError::CellOccupied);
        }

        // Place the current player's mark on the board.
        match self.current_player {
            Player::X => self.board[index] = Cell::X,
            Player::O => self.board[index] = Cell::O,
        }

        // Switch to the other player for the next turn.
        self.current_player = self.current_player.opponent();
        Ok(())
    }

    /// Returns the current state of the game.
    ///
    /// Possible values:
    /// - `GameState::InProgress`
    /// - `GameState::Tie`
    /// - `GameState::Won(Player::X)`
    /// - `GameState::Won(Player::O)`
    pub fn check_state(&self) -> GameState {
        Self::check_board_state(&self, self.board)
    }

    /// Returns `true` if the game is finished (either win or draw).
    pub fn is_over(&self) -> bool {
        !matches!(self.check_state(), GameState::InProgress)
    }

    /// Calculates the best move for the current player using Minimax with pruning.
    ///
    /// Returns:
    /// - `Some(index)` for the best move when AI is enabled.
    /// - `None` if the game is over or AI is disabled.
    ///
    /// # Example
    /// ```
    /// use xo_core::GameEngine;
    ///
    /// let game = GameEngine::with_ai(false);
    /// assert_eq!(game.get_best_move(), None); // AI disabled
    /// ```
    /// # Example with AI
    /// ```
    /// use xo_core::GameEngine;
    ///
    /// let mut game = GameEngine::new();
    /// game.make_move(0).unwrap(); // X
    /// game.make_move(4).unwrap(); // O
    /// game.make_move(1).unwrap(); // X
    ///
    /// // O should block X's winning move at index 2
    /// assert_eq!(game.get_best_move(), Some(2));
    /// ```
    ///
    pub fn get_best_move(&self) -> Option<usize> {
        // If the game is over, no move can be made.
        // furthermore, if AI is disabled, return None.
        if !self.ai_enabled || self.is_over() {
            return None;
        }

        let mut best_score = -i32::MAX;
        let mut best_move: Option<usize> = None;

        // The current player is the maximizing player for the Minimax algorithm.
        let maximizing_player = self.current_player;

        // Iterate through each cell on the board.
        for i in 0..9 {
            // Only consider empty cells as potential moves.
            if self.board[i] == Cell::Empty {
                // Create a temporary clone of the board to simulate the move.
                let mut temp_board = self.board;
                match maximizing_player {
                    Player::X => temp_board[i] = Cell::X,
                    Player::O => temp_board[i] = Cell::O,
                }

                // Recursively call the minimax function to evaluate the score of this move.
                let score = self.minimax_with_pruning(
                    temp_board,
                    maximizing_player.opponent(),
                    -i32::MAX,
                    i32::MAX,
                );

                // If this move's score is better than the current best score,
                // update the best score and the best move index.
                if score > best_score {
                    best_score = score;
                    best_move = Some(i);
                }
            }
        }
        best_move
    }

    /// The Minimax algorithm with Alpha-Beta pruning, implemented recursively.
    ///
    /// This is a private helper method that evaluates the game tree to find the
    /// best possible move.
    /// - `board`: The current state of the game board.
    /// - `player`: The player whose turn it is to evaluate.
    /// - `alpha`: The best score for the maximizing player.
    /// - `beta`: The best score for the minimizing player.
    ///
    /// Returns an integer score for the current board state.
    fn minimax_with_pruning(
        &self,
        board: [Cell; 9],
        player: Player,
        mut alpha: i32,
        mut beta: i32,
    ) -> i32 {
        // Check the state of the board and return a score if the game is over.
        let state = self.check_board_state(board);
        match state {
            GameState::Win(winner) => {
                // Return a positive score for a win, negative for a loss.
                // The score is large to represent a definite win/loss.
                return if winner == self.current_player {
                    10
                } else {
                    -10
                };
            }
            GameState::Tie => return 0,
            GameState::InProgress => {}
        }

        // Find all available moves (empty cells).
        let available_moves: Vec<usize> = board
            .iter()
            .enumerate()
            .filter_map(
                |(i, &cell)| {
                    if cell == Cell::Empty { Some(i) } else { None }
                },
            )
            .collect();

        // If there are no available moves, it's a tie.
        if available_moves.is_empty() {
            return 0;
        }

        // The current player is either the maximizing or minimizing player in this subtree.
        let current_player_is_maximizing = player == self.current_player;

        if current_player_is_maximizing {
            let mut max_eval = -i32::MAX;
            for &move_index in &available_moves {
                // Simulate the move.
                let mut temp_board = board;
                match player {
                    Player::X => temp_board[move_index] = Cell::X,
                    Player::O => temp_board[move_index] = Cell::O,
                }

                // Recursively call minimax for the opponent.
                let eval = self.minimax_with_pruning(temp_board, player.opponent(), alpha, beta);

                // Update the maximum score.
                max_eval = max_eval.max(eval);

                // Update alpha for the maximizing player.
                alpha = alpha.max(eval);

                // Alpha-beta pruning condition.
                if beta <= alpha {
                    break;
                }
            }
            max_eval
        } else {
            let mut min_eval = i32::MAX;
            for &move_index in &available_moves {
                // Simulate the move.
                let mut temp_board = board;
                match player {
                    Player::X => temp_board[move_index] = Cell::X,
                    Player::O => temp_board[move_index] = Cell::O,
                }

                // Recursively call minimax for the opponent.
                let eval = self.minimax_with_pruning(temp_board, player.opponent(), alpha, beta);

                // Update the minimum score.
                min_eval = min_eval.min(eval);

                // Update beta for the minimizing player.
                beta = beta.min(eval);

                // Alpha-beta pruning condition.
                if beta <= alpha {
                    break;
                }
            }
            min_eval
        }
    }

    /// A helper function to check the state of a given board.
    /// This is used internally by the Minimax algorithm.
    fn check_board_state(&self, board: [Cell; 9]) -> GameState {
        // Define all possible winning combinations (rows, columns, diagonals).
        let winning_combinations = [
            // Rows
            [0, 1, 2],
            [3, 4, 5],
            [6, 7, 8],
            // Columns
            [0, 3, 6],
            [1, 4, 7],
            [2, 5, 8],
            // Diagonals
            [0, 4, 8],
            [2, 4, 6],
        ];

        // Iterate through each winning combination to check for a win.
        for combination in &winning_combinations {
            let cell_1 = board[combination[0]];
            let cell_2 = board[combination[1]];
            let cell_3 = board[combination[2]];

            // If the cells are not empty and all three are the same, we have a winner.
            if cell_1 != Cell::Empty && cell_1 == cell_2 && cell_2 == cell_3 {
                // Determine the winning player based on the cell's state.
                return match cell_1 {
                    Cell::X => GameState::Win(Player::X),
                    Cell::O => GameState::Win(Player::O),
                    _ => unreachable!(),
                };
            }
        }

        // If no winner is found, check if the board is full.
        if !board.iter().any(|&cell| cell == Cell::Empty) {
            return GameState::Tie;
        }

        // If neither a win nor a tie, the game is still ongoing.
        GameState::InProgress
    }
}

#[cfg(test)]
mod tests {
    use super::*; // Import everything from this module

    #[test]
    fn x_can_win() {
        let mut game = GameEngine::new();
        game.make_move(0).unwrap(); // X
        game.make_move(3).unwrap(); // O
        game.make_move(1).unwrap(); // X
        game.make_move(4).unwrap(); // O
        game.make_move(2).unwrap(); // X wins
        assert_eq!(game.check_state(), GameState::Win(Player::X));
    }

    #[test]
    fn tie_game() {
        let mut game = GameEngine::new();
        let moves = [0, 1, 2, 4, 3, 5, 7, 6, 8];
        for &i in &moves {
            game.make_move(i).unwrap();
        }
        assert_eq!(game.check_state(), GameState::Tie);
    }

    #[test]
    fn invalid_move_out_of_bounds() {
        let mut game = GameEngine::new();
        assert_eq!(game.make_move(9), Err(MoveError::OutOfBounds));
    }

    #[test]
    fn invalid_move_occupied() {
        let mut game = GameEngine::new();
        game.make_move(0).unwrap();
        assert_eq!(game.make_move(0), Err(MoveError::CellOccupied));
    }

    #[test]
    fn minimax_ai_blocks_win() {
        let mut game = GameEngine::new();
        game.make_move(0).unwrap(); // X
        game.make_move(4).unwrap(); // O
        game.make_move(1).unwrap(); // X
        // O (AI) should now block X at 2
        assert_eq!(game.get_best_move(), Some(2));
    }

    #[test]
    fn human_vs_human_mode() {
        let game = GameEngine::with_ai(false);
        assert_eq!(game.get_best_move(), None); // AI disabled
    }
}