rewrite basically done

This commit is contained in:
2025-02-17 15:02:15 -05:00
parent 87f58e5e40
commit 7049039a77
4 changed files with 54 additions and 43 deletions

View File

@@ -59,6 +59,13 @@ fn gen_adj_lookup() -> PosMap<ChainCollection> {
.collect()
}
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
pub enum Winner {
Player(Piece),
Tie,
None,
}
lazy_static! {
/// Precompute all possible chains for each position on the board
pub static ref ADJ_LOOKUP: PosMap<ChainCollection> = gen_adj_lookup();
@@ -263,22 +270,20 @@ impl Board {
}
/// Returns the winner of the board (if any)
pub fn game_winner(&self, turn: Piece) -> Option<Piece> {
pub fn game_winner(&self, turn: Piece) -> Winner {
// Wikipedia: `Players take alternate turns. If one player cannot make a valid move, play passes back to the other player. The game ends when the grid has filled up or if neither player can make a valid move.`
if self.possible_moves(turn).next().is_some() || self.possible_moves(!turn).next().is_some()
{
// player can still make a move, there is no winner
return None;
return Winner::None;
}
let (white_score, black_score) = self.get_score();
match white_score.cmp(&black_score) {
Ordering::Greater => Some(Piece::White), // White win
Ordering::Less => Some(Piece::Black), // Black win
// TODO! this will end up being parsed the same as a "no winner", it should be a seperate type
Ordering::Equal => None, // Tie
Ordering::Greater => Winner::Player(Piece::White), // White win
Ordering::Less => Winner::Player(Piece::Black), // Black win
Ordering::Equal => Winner::Tie,
}
}
}