Make 'ChessBoard::xor' apply square-wise

This will make it easier to add Zobrist hashing afterwards.
This commit is contained in:
Bruno BELANYI 2024-04-06 17:44:27 +01:00
parent 85ac65408f
commit b2560aa183

View file

@ -123,13 +123,12 @@ impl ChessBoard {
self.compute_checkers(self.current_player()) self.compute_checkers(self.current_player())
} }
/// Quickly do and undo a move on the [Bitboard]s that are part of the [ChessBoard] state. Does /// Quickly add/remove a piece on the [Bitboard]s that are part of the [ChessBoard] state.
/// not account for all non-revertible changes such as en-passant state or half-move clock.
#[inline(always)] #[inline(always)]
fn xor(&mut self, color: Color, piece: Piece, start_end: Bitboard) { fn xor(&mut self, color: Color, piece: Piece, square: Square) {
*self.piece_occupancy_mut(piece) ^= start_end; *self.piece_occupancy_mut(piece) ^= square;
*self.color_occupancy_mut(color) ^= start_end; *self.color_occupancy_mut(color) ^= square;
self.combined_occupancy ^= start_end; self.combined_occupancy ^= square;
} }
/// 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.
@ -186,9 +185,7 @@ impl ChessBoard {
_ => *castle_rights, _ => *castle_rights,
}; };
if let Some(piece) = captured_piece { if let Some(piece) = captured_piece {
*self.piece_occupancy_mut(piece) ^= chess_move.destination(); self.xor(opponent, piece, chess_move.destination());
*self.color_occupancy_mut(opponent) ^= chess_move.destination();
self.combined_occupancy ^= chess_move.destination();
// If a rook is captured, it loses its castling rights // If a rook is captured, it loses its castling rights
let castle_rights = self.castle_rights_mut(opponent); let castle_rights = self.castle_rights_mut(opponent);
*castle_rights = match (piece, chess_move.destination().file()) { *castle_rights = match (piece, chess_move.destination().file()) {
@ -199,11 +196,8 @@ impl ChessBoard {
} }
// Revertible state modification // Revertible state modification
self.xor( self.xor(self.current_player(), move_piece, chess_move.start());
self.current_player(), self.xor(self.current_player(), move_piece, chess_move.destination());
move_piece,
chess_move.start() | chess_move.destination(),
);
self.total_plies += 1; self.total_plies += 1;
self.side = !self.side; self.side = !self.side;
@ -225,19 +219,13 @@ impl ChessBoard {
.unwrap(); .unwrap();
if let Some(piece) = previous.captured_piece { if let Some(piece) = previous.captured_piece {
*self.piece_occupancy_mut(piece) ^= chess_move.destination();
// The capture affected the *current* player, from our post-move POV // The capture affected the *current* player, from our post-move POV
*self.color_occupancy_mut(self.current_player()) ^= chess_move.destination(); self.xor(self.current_player(), piece, chess_move.destination());
self.combined_occupancy ^= chess_move.destination();
} }
// Restore revertible state // Restore revertible state
self.xor( self.xor(!self.current_player(), move_piece, chess_move.destination());
// The move was applied at the turn *before* the current player self.xor(!self.current_player(), move_piece, chess_move.start());
!self.current_player(),
move_piece,
chess_move.start() | chess_move.destination(),
);
self.total_plies -= 1; self.total_plies -= 1;
self.side = !self.side; self.side = !self.side;
} }