Refactor en-passant updates in 'ChessBoard'
All checks were successful
ci/woodpecker/push/check Pipeline was successful

This commit is contained in:
Bruno BELANYI 2024-04-06 20:53:22 +01:00
parent 06b5751702
commit 602847e573

View file

@ -177,6 +177,19 @@ impl ChessBoard {
self.hash ^= zobrist::castling_rights(self.castle_rights); self.hash ^= zobrist::castling_rights(self.castle_rights);
} }
fn update_en_passant(&mut self, en_passant: Option<Square>) {
self.hash ^= self
.en_passant()
.map(Square::file)
.map(zobrist::en_passant)
.unwrap_or(0);
self.hash ^= en_passant
.map(Square::file)
.map(zobrist::en_passant)
.unwrap_or(0);
self.en_passant = en_passant;
}
/// Play the given [Move], return a copy of the board with the resulting state. /// Play the given [Move], return a copy of the board with the resulting state.
#[inline(always)] #[inline(always)]
pub fn play_move(&self, chess_move: Move) -> Self { pub fn play_move(&self, chess_move: Move) -> Self {
@ -223,18 +236,7 @@ impl ChessBoard {
} else { } else {
None None
}; };
if self.en_passant() != en_passant { self.update_en_passant(en_passant);
self.hash ^= self
.en_passant()
.map(Square::file)
.map(zobrist::en_passant)
.unwrap_or(0);
self.hash ^= en_passant
.map(Square::file)
.map(zobrist::en_passant)
.unwrap_or(0);
self.en_passant = en_passant;
}
self.update_castling(self.current_player(), move_piece, chess_move.start().file()); self.update_castling(self.current_player(), move_piece, chess_move.start().file());
if let Some(piece) = captured_piece { if let Some(piece) = captured_piece {
self.xor(opponent, piece, chess_move.destination()); self.xor(opponent, piece, chess_move.destination());
@ -262,19 +264,7 @@ impl ChessBoard {
self.hash ^= zobrist::castling_rights(previous.castle_rights); self.hash ^= zobrist::castling_rights(previous.castle_rights);
self.castle_rights = previous.castle_rights; self.castle_rights = previous.castle_rights;
} }
if self.en_passant != previous.en_passant { self.update_en_passant(previous.en_passant);
self.hash ^= self
.en_passant()
.map(Square::file)
.map(zobrist::en_passant)
.unwrap_or(0);
self.hash ^= previous
.en_passant
.map(Square::file)
.map(zobrist::en_passant)
.unwrap_or(0);
self.en_passant = previous.en_passant;
}
self.half_move_clock = previous.half_move_clock; self.half_move_clock = previous.half_move_clock;
let move_piece = Piece::iter() let move_piece = Piece::iter()