[d]8/8/8/3p3r/8/5kp1/8/6K1 b - - 0 1
The way I implemented this is that before I go into Negamax loop I check if I am in endgame and have less than 6 pieces left on the table (I use 3,4 and 5 syzygy). If so, it goes into below code to find the position in the tablebase (this example is for a winning position):
Code: Select all
# Find position in tablebase
with chess.syzygy.open_tablebase('syzygy') as tablebase:
best_dtz = tablebase.get_dtz(board)
best_wdl = tablebase.get_wdl(board)
# If finding a winning position in the tablebases
if best_wdl in (1, 2):
# Find all possible moves and try them to see if it lowers original best_dtz
valid_moves = gamestate.get_valid_moves()
best_move = valid_moves[0]
for move in valid_moves:
# Make the move and convert to fen to use the Python-chess module
piece_captured = gamestate.board[move[1]]
gamestate.make_move(move[0], move[1])
fen = fh.gamestate_to_fen(gamestate)
board = chess.Board(fen)
with chess.syzygy.open_tablebase('syzygy') as tablebase:
new_dtz = tablebase.get_dtz(board)
new_wdl = tablebase.get_wdl(board)
# If winning capture or pawn move, play that immediately. Else see if one can lower dtz.
if new_wdl in (-1, -2):
if gamestate.piece_moved == 'p' or piece_captured != '--':
gamestate.unmake_move()
return move, 1e9
else:
if abs(new_dtz) <= abs(best_dtz):
best_dtz = new_dtz
best_move = move
return best_move, 1e9
Can I somehow get the DTM instead, or how do you guys solve this?