niklasf / python-chess

A chess library for Python, with move generation and validation, PGN parsing and writing, Polyglot opening book reading, Gaviota tablebase probing, Syzygy tablebase probing, and UCI/XBoard engine communication
https://python-chess.readthedocs.io/en/latest/
GNU General Public License v3.0
2.4k stars 521 forks source link

Endgame tables #1105

Closed st4ck3 closed 1 week ago

st4ck3 commented 1 week ago

Hi,

Congratulations for the great library!

I'am having a problem with the endgame tablebases and a specific FEN, corresponding to an endgame with 6 men. Here is my code and the FEN:

import chess import chess.engine import chess.svg import chess.pgn import chess.syzygy

fen = "8/8/5p2/1k3K1B/6P1/4r3/8/8 w - - 0 54" board = chess.Board(fen)

tablebase_dir = "c:/Users/.../.../.../Syzygy/6" tb = chess.syzygy.open_tablebase(tablebase_dir)

print(tb.probe_wdl(board))

I get the following error:

File C:\Python...\python-3.12.3.amd64\Lib\site-packages\chess\syzygy.py:1570 in probe_wdl_table table = typing.cast(WdlTable, self.wdl[key])

KeyError: 'KBPvKR'

Nevertheless, if I call the function calc_key(board) I get the correct key: 'KBPvKRP'

Any help would be greatly appreciated.

Best regards,

st4ck3 commented 1 week ago

tb.add_directory("c:/Users/.../Syzygy/3-4-5")

Adding this line solves the issue.

If I understand correctly, the algorithm check all legal moves. And the error message was due to the unavailability of 5 men table, because the capture of a black pawn was a legal move checked by the algorithm.

niklasf commented 1 week ago

Hi. It's a bit more efficient/complicated than all legal moves, but yes, if tables are missing the method is documented to raise KeyError.

So doing something like

try:
    wdl = tb.probe_wdl(board)
    # ...
except KeyError:
    # ...

is normal.