Validate en-passant square's rank in 'ChessBoard'
This commit is contained in:
parent
62c2be48c4
commit
ff7bea0508
|
@ -9,7 +9,7 @@ pub enum InvalidError {
|
|||
InvalidPawnPosition,
|
||||
/// Castling rights do not match up with the state of the board.
|
||||
InvalidCastlingRights,
|
||||
/// En-passant target square is not empty and behind an opponent's pawn.
|
||||
/// En-passant target square is not empty, behind an opponent's pawn, on the correct rank.
|
||||
InvalidEnPassant,
|
||||
/// The two kings are next to each other.
|
||||
NeighbouringKings,
|
||||
|
@ -33,7 +33,7 @@ impl std::fmt::Display for InvalidError {
|
|||
"Castling rights do not match up with the state of the board."
|
||||
}
|
||||
Self::InvalidEnPassant => {
|
||||
"En-passant target square is not empty and behind an opponent's pawn."
|
||||
"En-passant target square is not empty, behind an opponent's pawn, on the correct rank."
|
||||
}
|
||||
Self::NeighbouringKings => "The two kings are next to each other.",
|
||||
Self::OpponentInCheck => "The opponent is currently in check.",
|
||||
|
|
|
@ -297,12 +297,22 @@ impl ChessBoard {
|
|||
}
|
||||
}
|
||||
|
||||
// The current en-passant target square must be empty, right behind an opponent's pawn.
|
||||
// En-passant validation
|
||||
if let Some(square) = self.en_passant() {
|
||||
// Must be empty
|
||||
if !(self.combined_occupancy() & square).is_empty() {
|
||||
return Err(InvalidError::InvalidEnPassant);
|
||||
}
|
||||
let opponent_pawns = self.occupancy(Piece::Pawn, !self.current_player());
|
||||
|
||||
let opponent = !self.current_player();
|
||||
|
||||
// Must be on the opponent's third rank
|
||||
if (square & opponent.third_rank().into_bitboard()).is_empty() {
|
||||
return Err(InvalidError::InvalidEnPassant);
|
||||
}
|
||||
|
||||
// Must be behind a pawn
|
||||
let opponent_pawns = self.occupancy(Piece::Pawn, opponent);
|
||||
let double_pushed_pawn = self
|
||||
.current_player()
|
||||
.backward_direction()
|
||||
|
|
Loading…
Reference in a new issue