Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions chess/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()}"
Expand All @@ -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:
Expand Down
10 changes: 10 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Expand Down