Skip to content

Commit efb8b42

Browse files
Litschi21niklasf
authored andcommitted
feat: added piece_count function instead of chess.popcount(board.occupied)
1 parent c0c5cb0 commit efb8b42

5 files changed

Lines changed: 22 additions & 10 deletions

File tree

CHANGELOG.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
Changelog for python-chess
22
==========================
33

4+
New in unreleased (27th Mar 2026)
5+
---------------------------------
6+
7+
Bugfixes:
8+
* Fixed typo in README.rst.
9+
10+
Changes:
11+
* Added ``board.piece_count`` function.
12+
413
New in v1.11.2 (25th Feb 2025)
514
------------------------------
615

chess/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -841,6 +841,9 @@ def clear_board(self) -> None:
841841
:class:`~chess.Board` also clears the move stack.
842842
"""
843843
self._clear_board()
844+
845+
def piece_count(self) -> int:
846+
return popcount(self.occupied)
844847

845848
def pieces_mask(self, piece_type: PieceType, color: Color) -> Bitboard:
846849
if piece_type == PAWN:

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)