diff --git a/src/board.rs b/src/board.rs index 7cd835051..77f282e10 100644 --- a/src/board.rs +++ b/src/board.rs @@ -504,36 +504,30 @@ impl Board { self.state.keys.toggle_castling(self.state.castling); } - fn is_en_passant_valid(&self) -> bool { + /// We verify is self.state.enpassant is valid, and remove it if it is not. + /// This must be called after pinners and checkers have been updated. + fn validate_en_passant(&mut self) { + if self.en_passant() == Square::None { + return; + } + let stm = self.side_to_move(); let king = self.king_square(stm); let pushed_pawn = self.en_passant() ^ 8; + let ep_occ = self.en_passant().to_bb() | (self.occupancies() ^ pushed_pawn.to_bb()); - let pawns = pawn_attacks(self.en_passant(), !stm) & self.colored_pieces(stm, PieceType::Pawn); + let attackers = pawn_attacks(self.en_passant(), !stm) & self.colored_pieces(stm, PieceType::Pawn); - for attacker in pawns { - let occ = self.en_passant().to_bb() | (self.occupancies() ^ pushed_pawn.to_bb() ^ attacker.to_bb()); - let king_attackers = occ & self.attackers_to(king, occ) & self.colors(!stm); + for attacker in attackers { + let occ = ep_occ ^ attacker.to_bb(); + let slide_attackers = (rook_attacks(king, occ) & self.pieces2(PieceType::Rook, PieceType::Queen)) + | (bishop_attacks(king, occ) & self.pieces2(PieceType::Bishop, PieceType::Queen)); - if king_attackers.is_empty() { - return true; + if (slide_attackers & self.colors(!stm)).is_empty() { + return; } } - false - } - - /// We verify is self.state.enpassant is valid, and remove it if it is not. - /// This must be called after pinners and checkers have been updated. - fn update_en_passant(&mut self) { - if self.en_passant() == Square::None { - return; - } - - if self.is_en_passant_valid() { - return; - } - self.state.keys.toggle_en_passant(self.en_passant()); self.state.en_passant = Square::None; } diff --git a/src/board/makemove.rs b/src/board/makemove.rs index 462c96928..2dabb1c4c 100644 --- a/src/board/makemove.rs +++ b/src/board/makemove.rs @@ -125,7 +125,7 @@ impl Board { self.state.keys.toggle_castling(self.state.castling); self.update_threats(); - self.update_en_passant(); + self.validate_en_passant(); self.state.repetition = 0; diff --git a/src/board/parser.rs b/src/board/parser.rs index 95ad519a8..816645fa8 100644 --- a/src/board/parser.rs +++ b/src/board/parser.rs @@ -59,7 +59,7 @@ impl Board { board.update_threats(); board.update_hash_keys(); - board.update_en_passant(); + board.validate_en_passant(); Ok(board) }