From 9bf3c532018bb96792d60840b1c37cd9df94b927 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Sat, 19 Aug 2023 21:16:26 +0300 Subject: fix: do not toggle castling rights when not needed --- src/board/mod.rs | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) (limited to 'src/board/mod.rs') diff --git a/src/board/mod.rs b/src/board/mod.rs index 50aa1b9..39248eb 100644 --- a/src/board/mod.rs +++ b/src/board/mod.rs @@ -208,20 +208,33 @@ impl Board { let source_color = Color::from_piece(source_piece); match source_piece.without_color() { Piece::King => { - self.castling_rights[source_color as usize][CastlingSide::King as usize] = false; - self.castling_rights[source_color as usize][CastlingSide::Queen as usize] = false; - self.zobrist_toggle_castling_right(source_color, CastlingSide::King); - self.zobrist_toggle_castling_right(source_color, CastlingSide::Queen); + let king_side = &mut self.castling_rights[source_color as usize][CastlingSide::King as usize]; + if *king_side { + *king_side = false; + self.zobrist_toggle_castling_right(source_color, CastlingSide::King); + } + + let queen_side = &mut self.castling_rights[source_color as usize][CastlingSide::Queen as usize]; + if *queen_side { + *queen_side = false; + self.zobrist_toggle_castling_right(source_color, CastlingSide::Queen); + } }, Piece::Rook => { match mov.source.file() { 0 => { - self.castling_rights[source_color as usize][CastlingSide::Queen as usize] = false; - self.zobrist_toggle_castling_right(source_color, CastlingSide::Queen); + let queen_side = &mut self.castling_rights[source_color as usize][CastlingSide::Queen as usize]; + if *queen_side { + *queen_side = false; + self.zobrist_toggle_castling_right(source_color, CastlingSide::Queen); + } } 7 => { - self.castling_rights[source_color as usize][CastlingSide::King as usize] = false; - self.zobrist_toggle_castling_right(source_color, CastlingSide::King); + let king_side = &mut self.castling_rights[source_color as usize][CastlingSide::King as usize]; + if *king_side { + *king_side = false; + self.zobrist_toggle_castling_right(source_color, CastlingSide::King); + } } _ => {}, } -- cgit v1.2.3