mirror of
https://github.com/szymon-jozef/checkers.git
synced 2026-10-27 19:20:44 +01:00
feat: add checking if player has lost
This commit is contained in:
@@ -3,10 +3,14 @@ use crate::logic::{
|
|||||||
board::Board,
|
board::Board,
|
||||||
pawn::{CapturePath, MovePath},
|
pawn::{CapturePath, MovePath},
|
||||||
},
|
},
|
||||||
math::position::Position,
|
|
||||||
player::Player,
|
player::Player,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
pub enum GameResult {
|
||||||
|
Lost(uuid::Uuid),
|
||||||
|
Draw,
|
||||||
|
}
|
||||||
|
|
||||||
struct PlayerList {
|
struct PlayerList {
|
||||||
pub players: [Player; 2],
|
pub players: [Player; 2],
|
||||||
current_turn: usize,
|
current_turn: usize,
|
||||||
@@ -29,6 +33,10 @@ impl PlayerList {
|
|||||||
pub fn get_current_turn(&self) -> &Player {
|
pub fn get_current_turn(&self) -> &Player {
|
||||||
&self.players[self.current_turn]
|
&self.players[self.current_turn]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn get_passive_player(&self) -> &Player {
|
||||||
|
&self.players[(self.current_turn + 1) % self.size]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct GameMaster {
|
pub struct GameMaster {
|
||||||
@@ -69,4 +77,31 @@ impl GameMaster {
|
|||||||
.flatten()
|
.flatten()
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn is_player_lost(&self, player: &Player) -> bool {
|
||||||
|
let player_pawns = self.board.get_player_pawns_positions(player);
|
||||||
|
|
||||||
|
player_pawns.len() == 0
|
||||||
|
|| player_pawns
|
||||||
|
.iter()
|
||||||
|
.all(|pos| self.board.get_available_moves(*pos, player).is_none())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_game_result(&self) -> Option<GameResult> {
|
||||||
|
let active: &Player = self.players.get_current_turn();
|
||||||
|
let passive: &Player = self.players.get_passive_player();
|
||||||
|
|
||||||
|
let active_lost: bool = self.is_player_lost(active);
|
||||||
|
let passive_lost: bool = self.is_player_lost(passive);
|
||||||
|
|
||||||
|
if active_lost && passive_lost {
|
||||||
|
Some(GameResult::Draw)
|
||||||
|
} else if active_lost {
|
||||||
|
Some(GameResult::Lost(active.id))
|
||||||
|
} else if passive_lost {
|
||||||
|
Some(GameResult::Lost(passive.id))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user