|
| 1 | +pub use super::board::{Board, Stone, Point}; |
| 2 | + |
| 3 | +/// Represents the result of a Go game. |
| 4 | +#[derive(Debug, Clone, PartialEq, Eq)] |
| 5 | +pub enum GameResult { |
| 6 | + Ongoing, |
| 7 | + Resigned(Stone), |
| 8 | + Finished { black_score: usize, white_score: usize }, |
| 9 | +} |
| 10 | + |
| 11 | +/// Represents the game state and logic for a game of Go. |
| 12 | +#[derive(Debug)] |
| 13 | +pub struct Game { |
| 14 | + pub board: Board, |
| 15 | + pub to_move: Stone, |
| 16 | + pub result: GameResult, |
| 17 | +} |
| 18 | + |
| 19 | +impl Game { |
| 20 | + /// Creates a new Go game with the specified board size. |
| 21 | + /// |
| 22 | + /// # Examples |
| 23 | + /// ``` |
| 24 | + /// use puzzle_engine::go::game::Game; |
| 25 | + /// let game = Game::new(19); |
| 26 | + /// assert_eq!(game.board.size, 19); |
| 27 | + /// ``` |
| 28 | + pub fn new(size: usize) -> Self { |
| 29 | + Self { |
| 30 | + board: Board::new(size), |
| 31 | + to_move: Stone::Black, |
| 32 | + result: GameResult::Ongoing, |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + /// Attempts to play a move. Returns an error if the move is illegal. |
| 37 | + /// |
| 38 | + /// # Examples |
| 39 | + /// ``` |
| 40 | + /// use puzzle_engine::go::game::{Game,Point, Stone}; |
| 41 | + /// let mut game = Game::new(9); |
| 42 | + /// game.play(Point::new(3, 3)).unwrap(); |
| 43 | + /// ``` |
| 44 | + pub fn play(&mut self, point: Point) -> Result<(), &'static str> { |
| 45 | + if self.result != GameResult::Ongoing { |
| 46 | + return Err("Game is already over"); |
| 47 | + } |
| 48 | + |
| 49 | + self.board.place_stone(point, self.to_move)?; |
| 50 | + self.to_move = match self.to_move { |
| 51 | + Stone::Black => Stone::White, |
| 52 | + Stone::White => Stone::Black, |
| 53 | + }; |
| 54 | + Ok(()) |
| 55 | + } |
| 56 | + |
| 57 | + /// Forfeits the game for the current player. |
| 58 | + /// |
| 59 | + /// # Examples |
| 60 | + /// ``` |
| 61 | + /// use puzzle_engine::go::game::{Game, Stone}; |
| 62 | + /// let mut game = Game::new(9); |
| 63 | + /// game.resign(); |
| 64 | + /// assert_eq!(game.result, puzzle_engine::go::game::GameResult::Resigned(Stone::Black)); |
| 65 | + /// ``` |
| 66 | + pub fn resign(&mut self) { |
| 67 | + self.result = GameResult::Resigned(self.to_move); |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +#[cfg(test)] |
| 72 | +mod tests { |
| 73 | + use super::*; |
| 74 | + |
| 75 | + #[test] |
| 76 | + fn test_game_initial_state() { |
| 77 | + let game = Game::new(13); |
| 78 | + assert_eq!(game.board.size, 13); |
| 79 | + assert_eq!(game.to_move, Stone::Black); |
| 80 | + assert_eq!(game.result, GameResult::Ongoing); |
| 81 | + } |
| 82 | + |
| 83 | + #[test] |
| 84 | + fn test_play_alternates_turns() { |
| 85 | + let mut game = Game::new(9); |
| 86 | + assert_eq!(game.to_move, Stone::Black); |
| 87 | + game.play(Point::new(1, 1)).unwrap(); |
| 88 | + assert_eq!(game.to_move, Stone::White); |
| 89 | + } |
| 90 | + |
| 91 | + #[test] |
| 92 | + fn test_resign_ends_game() { |
| 93 | + let mut game = Game::new(9); |
| 94 | + game.resign(); |
| 95 | + assert_eq!(game.result, GameResult::Resigned(Stone::Black)); |
| 96 | + } |
| 97 | + |
| 98 | + #[test] |
| 99 | + fn test_cannot_play_after_resign() { |
| 100 | + let mut game = Game::new(9); |
| 101 | + game.resign(); |
| 102 | + let result = game.play(Point::new(2, 2)); |
| 103 | + assert_eq!(result, Err("Game is already over")); |
| 104 | + } |
| 105 | +} |
0 commit comments