diff --git a/src/game.rs b/src/game.rs index 903fe7d..8408ec4 100644 --- a/src/game.rs +++ b/src/game.rs @@ -39,6 +39,7 @@ pub enum GameState { Placing, Removing, Main, + Won(PlayerColor), } #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)] @@ -453,6 +454,7 @@ impl MillGame { match *state { GameState::Placing => { + // check if all stones are placed if !self .black_stones .lock() @@ -473,6 +475,7 @@ impl MillGame { self.current_player.lock().unwrap().swap(); } GameState::Removing => { + // check if all stones are placed, then decide whether main or placing is next state if !self .black_stones .lock() @@ -496,9 +499,39 @@ impl MillGame { self.current_player.lock().unwrap().swap(); } GameState::Main => { + // check if each player has enough stones to play + if self + .white_stones + .lock() + .unwrap() + .iter() + .filter(|stone| stone.state != StoneState::Dead) + .collect::>() + .len() + <= 3 + { + *state = GameState::Won(PlayerColor::Black); + return Ok(()); + } + + if self + .black_stones + .lock() + .unwrap() + .iter() + .filter(|stone| stone.state != StoneState::Dead) + .collect::>() + .len() + <= 3 + { + *state = GameState::Won(PlayerColor::White); + return Ok(()); + } + self.current_player.lock().unwrap().swap(); } GameState::Waiting => unreachable!(), + GameState::Won(_) => (), } } @@ -1059,7 +1092,7 @@ impl EngineObject for MillGame { Ok(()) })?; } - GameState::Waiting => (), + GameState::Waiting | GameState::Won(_) => (), } } MouseButton::Middle => self.camera_controls.lock().unwrap().hold(), diff --git a/src/simple_ai.rs b/src/simple_ai.rs index cdd84a7..8e15f0c 100644 --- a/src/simple_ai.rs +++ b/src/simple_ai.rs @@ -166,6 +166,7 @@ impl SimpleAI { })?; } GameState::Waiting => unreachable!(), + GameState::Won(_) => (), } if found_a_mill {