Skip to content

Commit 06de70e

Browse files
committed
Fix checkmating ep capture in chess.gaviota.PythonTablebase
1 parent dd4d9c1 commit 06de70e

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

chess/gaviota.py

+10-7
Original file line numberDiff line numberDiff line change
@@ -1493,8 +1493,8 @@ def probe_dtm(self, board: chess.Board) -> int:
14931493
Probes for depth to mate information.
14941494
14951495
The absolute value is the number of half-moves until forced mate
1496-
(or ``0`` in drawn positions). The value is positive if the
1497-
side to move is winning, otherwise it is negative.
1496+
(or ``0`` in drawn or checkmated positions). The value is positive if
1497+
the side to move is winning, otherwise it is negative or 0.
14981498
14991499
In the example position, white to move will get mated in 10 half-moves:
15001500
@@ -1532,11 +1532,14 @@ def probe_dtm(self, board: chess.Board) -> int:
15321532
try:
15331533
board.push(move)
15341534

1535-
child_dtm = -self._probe_dtm_no_ep(board)
1536-
if child_dtm > 0:
1537-
child_dtm += 1
1538-
elif child_dtm < 0:
1539-
child_dtm -= 1
1535+
if board.is_checkmate():
1536+
child_dtm = 1
1537+
else:
1538+
child_dtm = -self._probe_dtm_no_ep(board)
1539+
if child_dtm > 0:
1540+
child_dtm += 1
1541+
elif child_dtm < 0:
1542+
child_dtm -= 1
15401543

15411544
dtm = min(dtm, child_dtm) if dtm * child_dtm > 0 else max(dtm, child_dtm)
15421545
finally:

test.py

+10
Original file line numberDiff line numberDiff line change
@@ -4353,6 +4353,16 @@ def test_ep_is_best(self):
43534353
board = chess.Board("8/8/7k/8/1pP5/7K/8/8 b - c3 0 1")
43544354
self.assertEqual(self.tablebase.probe_dtm(board), 19)
43554355

4356+
@catchAndSkip(chess.gaviota.MissingTableError, "need KQPvKP.gtb.cp4")
4357+
def test_ep_is_mate(self):
4358+
# The resulting mate.
4359+
board = chess.Board("5Q2/7k/6P1/5K2/8/8/8/8 b - - 0 1")
4360+
self.assertEqual(self.tablebase.probe_dtm(board), 0)
4361+
4362+
# Ep leads to the previously tested mate position.
4363+
board = chess.Board("5Q2/7k/8/5KpP/8/8/8/8 w - g6 0 1")
4364+
self.assertEqual(self.tablebase.probe_dtm(board), 1)
4365+
43564366

43574367
class SvgTestCase(unittest.TestCase):
43584368

0 commit comments

Comments
 (0)