Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 15 additions & 21 deletions src/board.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/board/makemove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion src/board/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl Board {

board.update_threats();
board.update_hash_keys();
board.update_en_passant();
board.validate_en_passant();

Ok(board)
}
Expand Down
Loading