Check kings' position in 'ChessBoard::is_valid'

This commit is contained in:
Bruno BELANYI 2022-07-29 19:27:17 +02:00
parent 6976c60fe6
commit 08ce3787df

View file

@ -1,4 +1,7 @@
use crate::fen::{FenError, FromFen};
use crate::{
fen::{FenError, FromFen},
movegen,
};
use super::{Bitboard, CastleRights, Color, File, Move, Piece, Rank, Square};
@ -262,8 +265,15 @@ impl ChessBoard {
}
}
// Check that kings don't touch each other.
let white_king = self.piece_occupancy(Piece::King) & self.color_occupancy(Color::White);
let black_king = self.piece_occupancy(Piece::King) & self.color_occupancy(Color::Black);
// Unwrap is fine, we already checked that there is exactly one king of each color
if !(movegen::king_moves(white_king.try_into().unwrap()) & black_king).is_empty() {
return false;
}
// FIXME: check for opponent being in check.
// FIXME: check for kings touching.
true
}
@ -614,6 +624,34 @@ mod test {
assert!(!position.is_valid());
}
#[test]
fn invalid_kings_next_to_each_other() {
let position = ChessBoard {
piece_occupancy: [
// King
Square::E2 | Square::E3,
// Queen
Bitboard::EMPTY,
// Rook
Bitboard::EMPTY,
// Bishop
Bitboard::EMPTY,
// Knight
Bitboard::EMPTY,
// Pawn
Bitboard::EMPTY,
],
color_occupancy: [Square::E2.into_bitboard(), Square::E3.into_bitboard()],
combined_occupancy: Square::E2 | Square::E3,
castle_rights: [CastleRights::NoSide; 2],
en_passant: None,
half_move_clock: 0,
total_plies: 0,
side: Color::White,
};
assert!(!position.is_valid());
}
#[test]
fn fen_default_position() {
let default_position = ChessBoard::default();