Add final state

This commit is contained in:
hodasemi 2023-05-16 10:00:24 +02:00
parent 01deca2c96
commit 25b064e681
2 changed files with 35 additions and 1 deletions

View file

@ -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::<Vec<&Stone>>()
.len()
<= 3
{
*state = GameState::Won(PlayerColor::Black);
return Ok(());
}
if self
.black_stones
.lock()
.unwrap()
.iter()
.filter(|stone| stone.state != StoneState::Dead)
.collect::<Vec<&Stone>>()
.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(),

View file

@ -166,6 +166,7 @@ impl SimpleAI {
})?;
}
GameState::Waiting => unreachable!(),
GameState::Won(_) => (),
}
if found_a_mill {