cache winner in Move

This commit is contained in:
2025-02-08 23:13:12 -05:00
parent 2302f3be35
commit f5d44ca9f5
4 changed files with 22 additions and 16 deletions

View File

@@ -201,17 +201,12 @@ impl Board {
}
pub fn game_winner(&self, turn: Piece) -> Option<Piece> {
let (white_score, black_score) = self.get_score();
let max_score = BOARD_SIZE * BOARD_SIZE;
let combined_score = black_score + white_score;
if max_score != combined_score {
if self.possible_moves(turn).count() > 0 {
// player can still make a move, there is no winner
return None;
}
// if current player cannot make a move
if self.possible_moves(turn).count() > 0 {
return 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