@@ -151,8 +151,6 @@ impl Position {
151151 let from = mv. from ( ) ;
152152 let to = mv. to ( ) ;
153153 let piece = self . piece_at ( from) ;
154- let captured = self . piece_at ( to) ;
155- let genuine_capture = captured != Piece :: none ( ) && mv. mtype ( ) != MoveType :: Castle ;
156154 let pt = piece. piece_type ( ) ;
157155
158156 debug_assert ! ( from != Square :: NONE ) ;
@@ -163,11 +161,22 @@ impl Position {
163161 self . remove_piecetype ( self . stm , pt, from) ;
164162
165163 // capture piece
166- if genuine_capture {
167- self . remove_piecetype ( !self . stm , captured. piece_type ( ) , to) ;
168- self . update_castling_rights ( from, to) ;
169- } else if piece. piece_type ( ) == PieceType :: King || piece. piece_type ( ) == PieceType :: Rook {
170- self . update_castling_rights ( from, to) ;
164+ if mv. mtype ( ) != MoveType :: Castle {
165+ let captured = self . piece_at ( to) ;
166+ if captured != Piece :: none ( ) {
167+ let cap_pt = captured. piece_type ( ) ;
168+ self . remove_piecetype ( !self . stm , cap_pt, to) ;
169+
170+ if cap_pt == PieceType :: Rook {
171+ self . update_castling_rights_color ( !self . stm , from, to) ;
172+ }
173+
174+ self . halfm = 0 ;
175+ }
176+ }
177+
178+ if pt == PieceType :: King || pt == PieceType :: Rook {
179+ self . update_castling_rights_color ( self . stm , from, to) ;
171180 }
172181
173182 if mv. mtype ( ) == MoveType :: Promotion {
@@ -224,7 +233,7 @@ impl Position {
224233 // update state
225234
226235 // Update halfmove clock
227- if genuine_capture || pt == PieceType :: Pawn {
236+ if pt == PieceType :: Pawn {
228237 self . halfm = 0 ;
229238 } else {
230239 self . halfm += 1 ;
@@ -568,25 +577,27 @@ impl Position {
568577 self . is_attacked ( self . king_sq ( c) , !c)
569578 }
570579
571- fn update_castling_rights ( & mut self , from : Square , to : Square ) {
572- // Remove castling rights if king or rook moves
573- if from == Square :: E1 || to == Square :: E1 {
574- self . castling_rights &= !CastlingRights :: WHITE ;
575- }
576- if from == Square :: E8 || to == Square :: E8 {
577- self . castling_rights &= !CastlingRights :: BLACK ;
578- }
579- if from == Square :: A1 || to == Square :: A1 {
580- self . castling_rights &= !CastlingRights :: WHITE_QUEEN_SIDE ;
581- }
582- if from == Square :: H1 || to == Square :: H1 {
583- self . castling_rights &= !CastlingRights :: WHITE_KING_SIDE ;
584- }
585- if from == Square :: A8 || to == Square :: A8 {
586- self . castling_rights &= !CastlingRights :: BLACK_QUEEN_SIDE ;
587- }
588- if from == Square :: H8 || to == Square :: H8 {
589- self . castling_rights &= !CastlingRights :: BLACK_KING_SIDE ;
580+ fn update_castling_rights_color ( & mut self , color : Color , from : Square , to : Square ) {
581+ if color == Color :: White {
582+ if from == Square :: E1 || to == Square :: E1 {
583+ self . castling_rights &= !CastlingRights :: WHITE ;
584+ }
585+ if from == Square :: A1 || to == Square :: A1 {
586+ self . castling_rights &= !CastlingRights :: WHITE_QUEEN_SIDE ;
587+ }
588+ if from == Square :: H1 || to == Square :: H1 {
589+ self . castling_rights &= !CastlingRights :: WHITE_KING_SIDE ;
590+ }
591+ } else {
592+ if from == Square :: E8 || to == Square :: E8 {
593+ self . castling_rights &= !CastlingRights :: BLACK ;
594+ }
595+ if from == Square :: A8 || to == Square :: A8 {
596+ self . castling_rights &= !CastlingRights :: BLACK_QUEEN_SIDE ;
597+ }
598+ if from == Square :: H8 || to == Square :: H8 {
599+ self . castling_rights &= !CastlingRights :: BLACK_KING_SIDE ;
600+ }
590601 }
591602 }
592603
0 commit comments