Skip to content

Commit f96a87c

Browse files
committed
Check that board matches tablebase variant (#929)
1 parent 5fc7298 commit f96a87c

2 files changed

Lines changed: 16 additions & 8 deletions

File tree

chess/syzygy.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1585,6 +1585,15 @@ def probe_wdl_table(self, board: chess.Board) -> int:
15851585
return table.probe_wdl_table(board)
15861586

15871587
def probe_ab(self, board: chess.Board, alpha: int, beta: int, threats: bool = False) -> Tuple[int, int]:
1588+
# Check preconditions.
1589+
if board.uci_variant != self.variant.uci_variant:
1590+
raise KeyError(f"tablebase has been opened for {self.variant.uci_variant}, probed with: {board.uci_variant}")
1591+
if board.castling_rights:
1592+
raise KeyError(f"syzygy tables do not contain positions with castling rights: {board.fen()}")
1593+
if chess.popcount(board.occupied) > TBPIECES:
1594+
raise KeyError(f"syzygy tables support up to {TBPIECES} pieces, not {chess.popcount(board.occupied)}: {board.fen()}")
1595+
1596+
# Special case: Variant with compulsory captures.
15881597
if self.variant.captures_compulsory:
15891598
if board.is_variant_win():
15901599
return 2, 2
@@ -1701,14 +1710,6 @@ def probe_wdl(self, board: chess.Board) -> int:
17011710
17021711
Note that probing corrupted table files is undefined behavior.
17031712
"""
1704-
# Positions with castling rights are not in the tablebase.
1705-
if board.castling_rights:
1706-
raise KeyError(f"syzygy tables do not contain positions with castling rights: {board.fen()}")
1707-
1708-
# Validate piece count.
1709-
if chess.popcount(board.occupied) > TBPIECES:
1710-
raise KeyError(f"syzygy tables support up to {TBPIECES} pieces, not {chess.popcount(board.occupied)}: {board.fen()}")
1711-
17121713
# Probe.
17131714
v, _ = self.probe_ab(board, -2, 2)
17141715

test.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3892,6 +3892,13 @@ def test_suicide_stats(self):
38923892
self.assertAlmostEqual(dtz, solution["dtz"], delta=1,
38933893
msg=f"Expected dtz {solution['dtz']}, got {dtz} (in l. {l + 1}, fen: {board.fen()})")
38943894

3895+
def test_antichess_kvk(self):
3896+
kvk = chess.variant.AntichessBoard("8/8/8/8/8/3K4/8/4k3 w - - 0 30")
3897+
3898+
tables = chess.syzygy.Tablebase()
3899+
with self.assertRaises(KeyError):
3900+
tables.probe_dtz(kvk)
3901+
38953902

38963903
class NativeGaviotaTestCase(unittest.TestCase):
38973904

0 commit comments

Comments
 (0)