Compare commits

...

2 commits

Author SHA1 Message Date
Bruno BELANYI 8be7105ac5 Refactor castling-rights handling in 'play_move'
All checks were successful
ci/woodpecker/push/check Pipeline was successful
2024-04-13 23:03:11 +01:00
Bruno BELANYI d729d63c75 Add 'CastleRights::iter' 2024-04-06 19:49:46 +01:00
2 changed files with 28 additions and 13 deletions

View file

@ -17,6 +17,18 @@ impl CastleRights {
/// The number of [CastleRights] variants.
pub const NUM_VARIANTS: usize = 4;
const ALL: [Self; Self::NUM_VARIANTS] = [
Self::NoSide,
Self::KingSide,
Self::QueenSide,
Self::BothSides,
];
/// Iterate over all castle-rights variants.
pub fn iter() -> impl Iterator<Item = Self> {
Self::ALL.iter().cloned()
}
/// Convert from a castle rights index into a [CastleRights] type.
///
/// # Panics

View file

@ -131,6 +131,20 @@ impl ChessBoard {
self.combined_occupancy ^= square;
}
/// Compute the change of [CastleRights] from moving/taking a piece.
fn update_castling(&mut self, color: Color, piece: Piece, file: File) {
let original = self.castle_rights(color);
let new_rights = match (piece, file) {
(Piece::Rook, File::A) => original.without_queen_side(),
(Piece::Rook, File::H) => original.without_king_side(),
(Piece::King, _) => CastleRights::NoSide,
_ => return,
};
if new_rights != original {
*self.castle_rights_mut(color) = new_rights;
}
}
/// Play the given [Move], return a copy of the board with the resulting state.
#[inline(always)]
pub fn play_move(&self, chess_move: Move) -> Self {
@ -177,22 +191,11 @@ impl ChessBoard {
} else {
self.en_passant = None;
}
let castle_rights = self.castle_rights_mut(self.current_player());
*castle_rights = match (move_piece, chess_move.start().file()) {
(Piece::Rook, File::A) => castle_rights.without_queen_side(),
(Piece::Rook, File::H) => castle_rights.without_king_side(),
(Piece::King, _) => CastleRights::NoSide,
_ => *castle_rights,
};
self.update_castling(self.current_player(), move_piece, chess_move.start().file());
if let Some(piece) = captured_piece {
self.xor(opponent, piece, chess_move.destination());
// If a rook is captured, it loses its castling rights
let castle_rights = self.castle_rights_mut(opponent);
*castle_rights = match (piece, chess_move.destination().file()) {
(Piece::Rook, File::A) => castle_rights.without_queen_side(),
(Piece::Rook, File::H) => castle_rights.without_king_side(),
_ => *castle_rights,
};
self.update_castling(opponent, piece, chess_move.destination().file());
}
// Revertible state modification