rewrite basically done
This commit is contained in:
19
src/board.rs
19
src/board.rs
@@ -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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user