diff --git a/chess/__init__.py b/chess/__init__.py index b74d710f..2b623a9e 100644 --- a/chess/__init__.py +++ b/chess/__init__.py @@ -2462,6 +2462,7 @@ def push(self, move: Move) -> None: from_bb = BB_SQUARES[move.from_square] to_bb = BB_SQUARES[move.to_square] + effective_promoted = self._effective_promoted() promoted = bool(self.promoted & from_bb) piece_type = self._remove_piece_at(move.from_square) assert piece_type is not None, f"push() expects move to be pseudo-legal, but got {move} in {self.board_fen()}" @@ -2470,12 +2471,12 @@ def push(self, move: Move) -> None: # Update castling rights. self.castling_rights &= ~to_bb & ~from_bb - if piece_type == KING and not self._effective_promoted() & from_bb: + if piece_type == KING and not effective_promoted & from_bb: if self.turn == WHITE: self.castling_rights &= ~BB_RANK_1 else: self.castling_rights &= ~BB_RANK_8 - elif captured_piece_type == KING and not self._effective_promoted() & to_bb: + elif captured_piece_type == KING and not effective_promoted & to_bb: if self.turn == WHITE and square_rank(move.to_square) == RANK_8: self.castling_rights &= ~BB_RANK_8 elif self.turn == BLACK and square_rank(move.to_square) == RANK_1: diff --git a/test.py b/test.py index ac906638..6dda7a25 100755 --- a/test.py +++ b/test.py @@ -4469,6 +4469,16 @@ def test_king_promotions(self): self.assertEqual(board.san(d1K), "d1=K") self.assertEqual(board.parse_san("d1=K"), d1K) + def test_promoted_king_move_preserves_castling_rights(self): + board = chess.variant.SuicideBoard("6k1/8/8/8/8/8/1K~6/R3K2R w KQ - 0 1") + move = chess.Move.from_uci("b2b3") + self.assertTrue(board.is_valid()) + self.assertIn(move, board.legal_moves) + + board.push(move) + + self.assertEqual(board.castling_xfen(), "KQ") + class AtomicTestCase(unittest.TestCase):