Skip to content

Commit 2b2f149

Browse files
authored
Merge pull request #1184 from Litschi21/master
Add board.piece_count()
2 parents c0c5cb0 + a345dbd commit 2b2f149

4 files changed

Lines changed: 18 additions & 10 deletions

File tree

chess/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -842,6 +842,14 @@ def clear_board(self) -> None:
842842
"""
843843
self._clear_board()
844844

845+
def piece_count(self) -> int:
846+
"""
847+
Gets the number of pieces on the board.
848+
849+
Does not include Crazyhouse pockets.
850+
"""
851+
return popcount(self.occupied)
852+
845853
def pieces_mask(self, piece_type: PieceType, color: Color) -> Bitboard:
846854
if piece_type == PAWN:
847855
bb = self.pawns

chess/gaviota.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1519,8 +1519,8 @@ def probe_dtm(self, board: chess.Board) -> int:
15191519
raise KeyError(f"gaviota tables do not contain positions with castling rights: {board.fen()}")
15201520

15211521
# Supports only up to 5 pieces.
1522-
if chess.popcount(board.occupied) > 5:
1523-
raise KeyError(f"gaviota tables support up to 5 pieces, not {chess.popcount(board.occupied)}: {board.fen()}")
1522+
if board.piece_count() > 5:
1523+
raise KeyError(f"gaviota tables support up to 5 pieces, not {board.piece_count()}: {board.fen()}")
15241524

15251525
# KvK is a draw.
15261526
if board.occupied == board.kings:
@@ -1885,8 +1885,8 @@ def _probe_hard(self, board: chess.Board, wdl_only: bool = False) -> int:
18851885
if board.castling_rights:
18861886
raise KeyError(f"gaviota tables do not contain positions with castling rights: {board.fen()}")
18871887

1888-
if chess.popcount(board.occupied) > 5:
1889-
raise KeyError(f"gaviota tables support up to 5 pieces, not {chess.popcount(board.occupied)}: {board.fen()}")
1888+
if board.piece_count() > 5:
1889+
raise KeyError(f"gaviota tables support up to 5 pieces, not {board.piece_count()}: {board.fen()}")
18901890

18911891
stm = ctypes.c_uint(0 if board.turn == chess.WHITE else 1)
18921892
ep_square = ctypes.c_uint(board.ep_square if board.ep_square else 64)

chess/syzygy.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1548,8 +1548,8 @@ def probe_wdl_table(self, board: chess.Board) -> int:
15481548
try:
15491549
table = typing.cast(WdlTable, self.wdl[key])
15501550
except KeyError:
1551-
if chess.popcount(board.occupied) > TBPIECES:
1552-
raise KeyError(f"syzygy tables support up to {TBPIECES} pieces, not {chess.popcount(board.occupied)}: {board.fen()}")
1551+
if board.piece_count() > TBPIECES:
1552+
raise KeyError(f"syzygy tables support up to {TBPIECES} pieces, not {board.piece_count()}: {board.fen()}")
15531553
raise MissingTableError(f"did not find wdl table {key}")
15541554

15551555
self._bump_lru(table)
@@ -1567,8 +1567,8 @@ def probe_ab(self, board: chess.Board, alpha: int, beta: int, threats: bool = Fa
15671567
# positions that have more pieces than the maximum number of supported
15681568
# pieces. We artificially limit this to one additional level, to
15691569
# make sure search remains somewhat bounded.
1570-
if chess.popcount(board.occupied) > TBPIECES + 1:
1571-
raise KeyError(f"syzygy tables support up to {TBPIECES} pieces, not {chess.popcount(board.occupied)}: {board.fen()}")
1570+
if board.piece_count() > TBPIECES + 1:
1571+
raise KeyError(f"syzygy tables support up to {TBPIECES} pieces, not {board.piece_count()}: {board.fen()}")
15721572

15731573
# Special case: Variant with compulsory captures.
15741574
if self.variant.captures_compulsory:
@@ -1613,7 +1613,7 @@ def sprobe_ab(self, board: chess.Board, alpha: int, beta: int, threats: bool = F
16131613

16141614
threats_found = False
16151615

1616-
if threats or chess.popcount(board.occupied) >= 6:
1616+
if threats or board.piece_count() >= 6:
16171617
for threat in board.generate_legal_moves(~board.pawns):
16181618
board.push(threat)
16191619
try:

test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1220,7 +1220,7 @@ def test_clear(self):
12201220
self.assertFalse(board.ep_square)
12211221

12221222
self.assertFalse(board.piece_at(chess.E1))
1223-
self.assertEqual(chess.popcount(board.occupied), 0)
1223+
self.assertEqual(board.piece_count(), 0)
12241224

12251225
def test_threefold_repetition(self):
12261226
board = chess.Board()

0 commit comments

Comments
 (0)