Modify castling rights after rook capture

This commit is contained in:
Bruno BELANYI 2024-04-05 23:35:46 +01:00
parent 1c8a101689
commit 9507432bd3

View file

@ -185,6 +185,13 @@ impl ChessBoard {
*self.piece_occupancy_mut(piece) ^= chess_move.destination(); *self.piece_occupancy_mut(piece) ^= chess_move.destination();
*self.color_occupancy_mut(opponent) ^= chess_move.destination(); *self.color_occupancy_mut(opponent) ^= chess_move.destination();
self.combined_occupancy ^= chess_move.destination(); self.combined_occupancy ^= 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,
};
} }
// Revertible state modification // Revertible state modification
@ -768,6 +775,17 @@ mod test {
); );
} }
#[test]
fn do_move_capture_changes_castling() {
let mut position = ChessBoard::from_fen("r3k2r/8/8/8/8/8/8/R3K2R w KQkq - 0 1").unwrap();
let expected = ChessBoard::from_fen("r3k2R/8/8/8/8/8/8/R3K3 b Qq - 0 1").unwrap();
let capture = Move::new(Square::H1, Square::H8, None);
position.do_move(capture);
assert_eq!(position, expected);
}
#[test] #[test]
fn do_move_and_undo() { fn do_move_and_undo() {
// Start from default position // Start from default position