From 95383de0fbedfd5feb09fdc20a31793a4c0482af Mon Sep 17 00:00:00 2001 From: winapiadmin <138602885+winapiadmin@users.noreply.github.com> Date: Tue, 3 Feb 2026 22:11:20 +0700 Subject: [PATCH 1/5] fixed multiple kings for was_into_check --- chess/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chess/__init__.py b/chess/__init__.py index b51c31ca..661ddf27 100644 --- a/chess/__init__.py +++ b/chess/__init__.py @@ -2005,7 +2005,7 @@ def is_into_check(self, move: Move) -> bool: def was_into_check(self) -> bool: king = self.king(not self.turn) - return king is not None and self.is_attacked_by(self.turn, king) + return popcount(self.kings) == 1 and king is not None and self.is_attacked_by(self.turn, king) def is_pseudo_legal(self, move: Move) -> bool: # Null moves are not pseudo-legal. From 4b0c56dae357c9545d3218946faf14f85ddf5753 Mon Sep 17 00:00:00 2001 From: winapiadmin <138602885+winapiadmin@users.noreply.github.com> Date: Tue, 3 Feb 2026 22:15:06 +0700 Subject: [PATCH 2/5] fixed king() behavior on multiple kings minus prev commit --- chess/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/chess/__init__.py b/chess/__init__.py index 661ddf27..3151d9dd 100644 --- a/chess/__init__.py +++ b/chess/__init__.py @@ -916,7 +916,7 @@ def king(self, color: Color) -> Optional[Square]: considered. """ king_mask = self.occupied_co[color] & self.kings & ~self.promoted - return msb(king_mask) if king_mask else None + return msb(king_mask) if king_mask and popcount(self.kings) == 1 else None def attacks_mask(self, square: Square) -> Bitboard: bb_square = BB_SQUARES[square] @@ -2005,7 +2005,7 @@ def is_into_check(self, move: Move) -> bool: def was_into_check(self) -> bool: king = self.king(not self.turn) - return popcount(self.kings) == 1 and king is not None and self.is_attacked_by(self.turn, king) + return king is not None and self.is_attacked_by(self.turn, king) def is_pseudo_legal(self, move: Move) -> bool: # Null moves are not pseudo-legal. From c100a37191bae00dcaf2eb9156284485bdbf2bb2 Mon Sep 17 00:00:00 2001 From: winapiadmin <138602885+winapiadmin@users.noreply.github.com> Date: Tue, 3 Feb 2026 22:27:47 +0700 Subject: [PATCH 3/5] add test cases (very little) --- test.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test.py b/test.py index 2ebd357d..e15edf44 100755 --- a/test.py +++ b/test.py @@ -1720,6 +1720,18 @@ def test_impossible_check_due_to_en_passant(self): self.assertFalse(board.has_legal_en_passant()) self.assertEqual(len(list(board.legal_moves)), 2) + def test_multiple_kings(self): + board = chess.Board("KKKK1kkk/8/8/8/8/8/8/8 w - - 0 1") + self.assertEqual(board.king(chess.WHITE), None) + + # https://github.com/niklasf/python-chess/issues/1169#issuecomment-3838025276 + board = chess.Board(None) + board.set_piece_at(chess.A1, chess.Piece(chess.ROOK, chess.WHITE)) + board.set_piece_at(chess.A8, chess.Piece(chess.KING, chess.BLACK)) # attacked + board.set_piece_at(chess.H8, chess.Piece(chess.KING, chess.BLACK)) # safe + board.turn = chess.WHITE + self.assertTrue(board.was_into_check()) + class LegalMoveGeneratorTestCase(unittest.TestCase): From 08a815d2dd897760969c1aaf03e731a5f7ff1d09 Mon Sep 17 00:00:00 2001 From: winapiadmin <138602885+winapiadmin@users.noreply.github.com> Date: Tue, 3 Feb 2026 22:38:00 +0700 Subject: [PATCH 4/5] using the precomputed king mask --- chess/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chess/__init__.py b/chess/__init__.py index 3151d9dd..347f22ea 100644 --- a/chess/__init__.py +++ b/chess/__init__.py @@ -916,7 +916,7 @@ def king(self, color: Color) -> Optional[Square]: considered. """ king_mask = self.occupied_co[color] & self.kings & ~self.promoted - return msb(king_mask) if king_mask and popcount(self.kings) == 1 else None + return msb(king_mask) if king_mask and popcount(king_mask) == 1 else None def attacks_mask(self, square: Square) -> Bitboard: bb_square = BB_SQUARES[square] From 6e0cf056aa7fbfd6332dcec09e88dfceba94fb56 Mon Sep 17 00:00:00 2001 From: winapiadmin <138602885+winapiadmin@users.noreply.github.com> Date: Tue, 3 Feb 2026 22:44:10 +0700 Subject: [PATCH 5/5] removed testcase of was_into_check() on multiple kings... ... because it would cause the function to return False (because king() didn't detect any king because of decision) --- test.py | 8 -------- 1 file changed, 8 deletions(-) diff --git a/test.py b/test.py index e15edf44..4927a2b8 100755 --- a/test.py +++ b/test.py @@ -1724,14 +1724,6 @@ def test_multiple_kings(self): board = chess.Board("KKKK1kkk/8/8/8/8/8/8/8 w - - 0 1") self.assertEqual(board.king(chess.WHITE), None) - # https://github.com/niklasf/python-chess/issues/1169#issuecomment-3838025276 - board = chess.Board(None) - board.set_piece_at(chess.A1, chess.Piece(chess.ROOK, chess.WHITE)) - board.set_piece_at(chess.A8, chess.Piece(chess.KING, chess.BLACK)) # attacked - board.set_piece_at(chess.H8, chess.Piece(chess.KING, chess.BLACK)) # safe - board.turn = chess.WHITE - self.assertTrue(board.was_into_check()) - class LegalMoveGeneratorTestCase(unittest.TestCase):