allow the game to end in cases of a non-full board
This commit is contained in:
28
src/board.rs
28
src/board.rs
@@ -48,14 +48,6 @@ impl fmt::Display for Board {
|
||||
white_score, black_score
|
||||
)?;
|
||||
|
||||
// Print game over screen
|
||||
if self.game_over() {
|
||||
match self.get_winner() {
|
||||
Some(piece) => writeln!(f, "{} Wins", piece.text()),
|
||||
None => writeln!(f, "Tie"),
|
||||
}?
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -207,24 +199,24 @@ impl Board {
|
||||
.fold((0_usize, 0usize), |(a, b), (c, d)| (a + c, b + d))
|
||||
}
|
||||
|
||||
// Get the winning piece (for game over screen mainly)
|
||||
pub fn get_winner(&self) -> Option<Piece> {
|
||||
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 {
|
||||
return None;
|
||||
}
|
||||
|
||||
// if current player cannot make a move
|
||||
if self.possible_moves(turn).count() > 0 {
|
||||
return None;
|
||||
}
|
||||
match white_score.cmp(&black_score) {
|
||||
Ordering::Greater => Some(Piece::White), // White win
|
||||
Ordering::Less => Some(Piece::Black), // Black win
|
||||
Ordering::Equal => None, // Tie
|
||||
}
|
||||
}
|
||||
|
||||
pub fn game_over(&self) -> bool {
|
||||
let (white_score, black_score) = self.get_score();
|
||||
let max_score = BOARD_SIZE * BOARD_SIZE;
|
||||
let combined_score = black_score + white_score;
|
||||
|
||||
max_score == combined_score
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
Reference in New Issue
Block a user