From 3fa56b36a4c28dcde04e16846c61bb6dc2b05db8 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Fri, 12 Aug 2022 16:17:51 +0200 Subject: [PATCH] Check for invalid pawns in 'ChessBoard::is_valid' --- src/board/chess_board.rs | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/board/chess_board.rs b/src/board/chess_board.rs index e08b3ee..573ecfa 100644 --- a/src/board/chess_board.rs +++ b/src/board/chess_board.rs @@ -238,6 +238,14 @@ impl ChessBoard { } } + // Check that pawns aren't in first/last rank. + if !(self.piece_occupancy(Piece::Pawn) + & (Rank::First.into_bitboard() | Rank::Eighth.into_bitboard())) + .is_empty() + { + return false; + } + // Verify that rooks and kings that are allowed to castle have not been moved. for color in Color::iter() { let castle_rights = self.castle_rights(color); @@ -730,6 +738,34 @@ mod test { assert!(!position.is_valid()); } + #[test] + fn invalid_pawn_on_first_rank() { + let position = ChessBoard { + piece_occupancy: [ + // King + Square::H1 | Square::H8, + // Queen + Bitboard::EMPTY, + // Rook + Bitboard::EMPTY, + // Bishop + Bitboard::EMPTY, + // Knight + Bitboard::EMPTY, + // Pawn + Square::A1.into_bitboard(), + ], + color_occupancy: [Square::A1 | Square::H1, Square::H8.into_bitboard()], + combined_occupancy: Square::A1 | 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 {