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
2 changes: 1 addition & 1 deletion chess/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(king_mask) == 1 else None

def attacks_mask(self, square: Square) -> Bitboard:
bb_square = BB_SQUARES[square]
Expand Down
4 changes: 4 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1720,6 +1720,10 @@ 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)


class LegalMoveGeneratorTestCase(unittest.TestCase):

Expand Down