1mod game_engine;
48mod types;
49
50pub use game_engine::GameEngine;
51pub use types::{Cell, GameState, MoveError, Player};
52
53#[cfg(test)]
54mod tests {
55 use super::*;
56
57 #[test]
58 fn x_wins_top_row() {
59 let mut game = GameEngine::new();
60 game.make_move(0).unwrap(); game.make_move(3).unwrap(); game.make_move(1).unwrap(); game.make_move(4).unwrap(); game.make_move(2).unwrap(); assert_eq!(game.check_state(), GameState::Win(Player::X));
66 }
67
68 #[test]
69 fn o_wins_diagonal() {
70 let mut game = GameEngine::new();
71 game.make_move(0).unwrap(); game.make_move(2).unwrap(); game.make_move(1).unwrap(); game.make_move(4).unwrap(); game.make_move(3).unwrap(); game.make_move(6).unwrap(); assert_eq!(game.check_state(), GameState::Win(Player::O));
78 }
79
80 #[test]
81 fn tie_game() {
82 let mut game = GameEngine::new();
83 let moves = [0, 1, 2, 4, 3, 5, 7, 6, 8];
84 for &i in &moves {
85 game.make_move(i).unwrap();
86 }
87 assert_eq!(game.check_state(), GameState::Tie);
88 }
89
90 #[test]
91 fn cannot_play_out_of_bounds() {
92 let mut game = GameEngine::new();
93 assert_eq!(game.make_move(9), Err(MoveError::OutOfBounds));
94 assert_eq!(game.make_move(99), Err(MoveError::OutOfBounds));
95 }
96
97 #[test]
98 fn cannot_play_on_occupied_cell() {
99 let mut game = GameEngine::new();
100 game.make_move(0).unwrap();
101 assert_eq!(game.make_move(0), Err(MoveError::CellOccupied));
102 }
103
104 #[test]
105 fn board_updates_correctly() {
106 let mut game = GameEngine::new();
107 game.make_move(0).unwrap();
108 let board = game.get_board();
109 assert_eq!(board[0], Cell::X);
110 }
111
112 #[test]
113 fn ai_blocks_win() {
114 let mut game = GameEngine::new();
115 game.make_move(0).unwrap(); game.make_move(4).unwrap(); game.make_move(1).unwrap(); assert_eq!(game.get_best_move(), Some(2));
120 }
121
122 #[test]
123 fn ai_chooses_winning_move() {
124 let mut game = GameEngine::new();
125 game.make_move(0).unwrap(); game.make_move(4).unwrap(); game.make_move(2).unwrap(); game.make_move(1).unwrap(); game.make_move(3).unwrap(); assert_eq!(game.get_best_move(), Some(7));
132 }
133}