Add 'in check' logic
This commit is contained in:
parent
99f826ef32
commit
4a2a4c81c5
|
@ -75,23 +75,38 @@ impl BoardState {
|
|||
}
|
||||
}
|
||||
|
||||
if (moving_color == Color::White && proposed.is_white_in_check())
|
||||
|| (moving_color == Color::Black && proposed.is_black_in_check())
|
||||
{
|
||||
if proposed.is_in_check(moving_color) {
|
||||
return;
|
||||
}
|
||||
|
||||
*self = proposed;
|
||||
}
|
||||
|
||||
// TODO: this
|
||||
fn is_black_in_check(&self) -> bool {
|
||||
false
|
||||
}
|
||||
fn is_in_check(&self, color: Color) -> bool {
|
||||
let mut tmp_board = self.clone();
|
||||
|
||||
// TODO: this
|
||||
fn is_white_in_check(&self) -> bool {
|
||||
false
|
||||
let king = Piece {
|
||||
kind: PieceKind::King,
|
||||
color: color.clone(),
|
||||
};
|
||||
|
||||
let king_opt = self
|
||||
.inner
|
||||
.iter()
|
||||
.find(|(_, piece_state)| piece_state.piece == king);
|
||||
|
||||
if let Some((king_coords, _)) = king_opt {
|
||||
self.inner
|
||||
.iter()
|
||||
.filter(|(_, piece_state)| piece_state.piece.color != color)
|
||||
.any(|(coords, piece_state)| {
|
||||
piece_state
|
||||
.clone()
|
||||
.can_move_to(coords, king_coords, &mut tmp_board)
|
||||
})
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue