This commit is contained in:
parent
ece42e982c
commit
a07416dce8
|
@ -466,8 +466,8 @@ impl GameState {
|
|||
|
||||
fn get_possible_moves(&self, color: &Color) -> HashSet<Move> {
|
||||
let mut possible_moves = HashSet::new();
|
||||
for (pos, piece) in self.board {
|
||||
if piece.color == color {
|
||||
for (pos, piece) in &self.board {
|
||||
if &piece.color == color {
|
||||
possible_moves.extend(piece.get_possible_moves(&pos, &self));
|
||||
}
|
||||
}
|
||||
|
@ -477,26 +477,41 @@ impl GameState {
|
|||
// returns outcome of game if it is over, otherwise returns None
|
||||
fn get_outcome(&self) -> Option<GameOutcome> {
|
||||
// checkmate
|
||||
if self.player_in_check(&Color::White) && self.get_possible_moves(&Color::White).is_empty() && self.allowed_turn == Color::White {
|
||||
Some(GameOutcome::BlackWin)
|
||||
} else if self.player_in_check(&Color::Black) && self.get_possible_moves(&Color::Black).is_empty() && self.allowed_turn == Color::Black {
|
||||
Some(GameOutcome::WhiteWin)
|
||||
if self.player_in_check(&Color::White)
|
||||
&& self.get_possible_moves(&Color::White).is_empty()
|
||||
&& self.allowed_turn == Color::White
|
||||
{
|
||||
return Some(GameOutcome::BlackWin);
|
||||
} else if self.player_in_check(&Color::Black)
|
||||
&& self.get_possible_moves(&Color::Black).is_empty()
|
||||
&& self.allowed_turn == Color::Black
|
||||
{
|
||||
return Some(GameOutcome::WhiteWin);
|
||||
}
|
||||
|
||||
if !self.player_in_check(&Color::White) && self.get_possible_moves(&Color::White).is_empty() && self.allowed_turn == Color::White {
|
||||
Some(GameOutcome::Draw)
|
||||
} else if !self.player_in_check(&Color::Black) && self.get_possible_moves(&Color::Black).is_empty() && self.allowed_turn == Color::Black {
|
||||
Some(GameOutcome::Draw)
|
||||
if !self.player_in_check(&Color::White)
|
||||
&& self.get_possible_moves(&Color::White).is_empty()
|
||||
&& self.allowed_turn == Color::White
|
||||
{
|
||||
return Some(GameOutcome::Draw);
|
||||
} else if !self.player_in_check(&Color::Black)
|
||||
&& self.get_possible_moves(&Color::Black).is_empty()
|
||||
&& self.allowed_turn == Color::Black
|
||||
{
|
||||
return Some(GameOutcome::Draw);
|
||||
}
|
||||
|
||||
// draw by insufficient material - occurs if both sides only have king, or king and minor piece
|
||||
let is_insufficient = True;
|
||||
let mut is_insufficient = true;
|
||||
if self.board.len() <= 4 {
|
||||
let mut white_pieces = 0;
|
||||
let mut black_pieces = 0;
|
||||
for (_, piece) in self.board {
|
||||
if piece.kind == PieceKind::Rook || piece.kind == PieceKind::Queen || piece.kind == PieceKind::Pawn {
|
||||
is_insufficient = False;
|
||||
for (_, piece) in &self.board {
|
||||
if piece.kind == PieceKind::Rook
|
||||
|| piece.kind == PieceKind::Queen
|
||||
|| piece.kind == PieceKind::Pawn
|
||||
{
|
||||
is_insufficient = false;
|
||||
} else {
|
||||
if piece.color == Color::White {
|
||||
white_pieces += 1;
|
||||
|
@ -506,11 +521,11 @@ impl GameState {
|
|||
}
|
||||
}
|
||||
if white_pieces > 2 || black_pieces > 2 {
|
||||
is_insufficient = False;
|
||||
is_insufficient = false;
|
||||
}
|
||||
}
|
||||
if is_insufficient {
|
||||
Some(GameOutcome::Draw)
|
||||
return Some(GameOutcome::Draw);
|
||||
}
|
||||
|
||||
None
|
||||
|
|
Loading…
Reference in a new issue