From 46ff51552aaa59746e72ba9a013fe46f8cae4dbd Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Fri, 12 Aug 2022 16:17:03 +0200 Subject: [PATCH] Check all piece counts in 'ChessBoard::is_valid' --- src/board/chess_board.rs | 57 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 55 insertions(+), 2 deletions(-) diff --git a/src/board/chess_board.rs b/src/board/chess_board.rs index 573ecfa..838769a 100644 --- a/src/board/chess_board.rs +++ b/src/board/chess_board.rs @@ -231,9 +231,23 @@ impl ChessBoard { return false; } - // Have exactly one king of each color. for color in Color::iter() { - if self.occupancy(Piece::King, color).count() != 1 { + for piece in Piece::iter() { + // Check that we have the expected number of piecese. + let count = self.occupancy(piece, color).count(); + let possible = match piece { + Piece::King => count == 1, + Piece::Pawn => count <= 8, + Piece::Queen => count <= 9, + _ => count <= 10, + }; + if !possible { + return false; + } + } + + // Check that don't have too many pieces in total + if self.color_occupancy(color).count() > 16 { return false; } } @@ -766,6 +780,45 @@ mod test { assert!(!position.is_valid()); } + #[test] + fn invalid_too_many_pieces() { + let position = ChessBoard { + piece_occupancy: [ + // King + Square::H1 | Square::H8, + // Queen + Bitboard::EMPTY, + // Rook + Bitboard::EMPTY, + // Bishop + Bitboard::EMPTY, + // Knight + Bitboard::EMPTY, + // Pawn + File::B.into_bitboard() + | File::C.into_bitboard() + | File::D.into_bitboard() + | File::E.into_bitboard(), + ], + color_occupancy: [ + File::B.into_bitboard() | File::C.into_bitboard() | Square::H1, + File::D.into_bitboard() | File::E.into_bitboard() | Square::H8, + ], + combined_occupancy: File::B.into_bitboard() + | File::C.into_bitboard() + | File::D.into_bitboard() + | File::E.into_bitboard() + | Square::H1 + | Square::H8, + castle_rights: [CastleRights::NoSide; 2], + en_passant: None, + half_move_clock: 0, + total_plies: 0, + side: Color::White, + }; + assert!(!position.is_valid()); + } + #[test] fn checkers() { let position = ChessBoard {