Closed CoderAryanAnand closed 3 years ago
@CoderAryanAnand To add variation step back by 1 node, use parent to go to the previous node.
for node in game.mainline():
move = node.move
board = node.board() # The move is already pushed here.
# Use the previous node when using mainline()
parent_node = node.parent
parent_board = parent_node.board() # The move is not yet pushed.
parent_node.add_line(get_best_var(parent_board.fen(), uci=True))
print(game)
Also quit the engine this way.
def get_best_var(fen, uci=False):
"""
Gets the best variation from uci in position
"""
engine = chess.engine.SimpleEngine.popen_uci(engine_file)
board = chess.Board(fen)
result = engine.analyse(board, chess.engine.Limit(time=0.1))
engine.quit()
if uci:
return result['pv']
else:
return board.variation_san(result['pv'])
@fsmosca Thank you very much (for both answers)! It worked for me. Have a nice day!
Hi Niklas, This time my problem is that the variation adds a move too late. Here is the code:
game.pgn:
It returns:
I'm using Stockfish 12, and even if I add all the others(
if delta > ...
), it always evaluates the position, but adds the move too late. Example of what I mean:1. d4[[It evaluates fom move 0 to d4 but makes the variation after d4]] d5 ( { Good move, but better was: } 1... Nf6 2. c4 e6 3. Nc3 Bb4 4. Bg5 h6 5. Bh4 c5 6. dxc5 Bxc5 7. Nf3 Nc6 8. e3 { 0.25 } )
Would you know how I could fix this (so that it adds the variation in the same position it evaluates)?PS: If I try:
It returns: