Skip to content

Commit 76cbe98

Browse files
authored
Multiple kings per color (was_into_check and more affected) (#1179)
* fixed multiple kings for was_into_check * fixed king() behavior on multiple kings minus prev commit * add test cases (very little) * using the precomputed king mask * 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)
1 parent 11399c6 commit 76cbe98

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

chess/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -916,7 +916,7 @@ def king(self, color: Color) -> Optional[Square]:
916916
considered.
917917
"""
918918
king_mask = self.occupied_co[color] & self.kings & ~self.promoted
919-
return msb(king_mask) if king_mask else None
919+
return msb(king_mask) if king_mask and popcount(king_mask) == 1 else None
920920

921921
def attacks_mask(self, square: Square) -> Bitboard:
922922
bb_square = BB_SQUARES[square]

test.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1720,6 +1720,10 @@ def test_impossible_check_due_to_en_passant(self):
17201720
self.assertFalse(board.has_legal_en_passant())
17211721
self.assertEqual(len(list(board.legal_moves)), 2)
17221722

1723+
def test_multiple_kings(self):
1724+
board = chess.Board("KKKK1kkk/8/8/8/8/8/8/8 w - - 0 1")
1725+
self.assertEqual(board.king(chess.WHITE), None)
1726+
17231727

17241728
class LegalMoveGeneratorTestCase(unittest.TestCase):
17251729

0 commit comments

Comments
 (0)