1

I've been playing around with python-chess, I am loading a PGN file (A), reading the game from it. I then make a move, which creates a second updated PGN file (B). I read the last move from B and want to make that same move in A, and make a comment to that move with the date.

last_move = new_game.end()
last_move_san = last_move.san()
old_last = game.end()
old_last_san = old_last.san()
if last_move_san != old_last_san:
    game.end().board().push_san(last_move_san)
    game.end().comment = datetime.strftime(tdate, "%m/%d")
f_exporter = chess.pgn.FileExporter(temp_pgn)
game.accept(f_exporter)

The final PGN file shows the game as it was originally, without the move from B. The docs for board() say that it just produces a copy and doesn't alter the actual game. What's the right way to add a move to a game?

ravl1084
  • 291
  • 4
  • 11
  • Looks like the intent is that you create a new game. You can initialize it with a board at the specified position and add changes to the new game from there. – pvg Feb 17 '17 at 19:58

1 Answers1

0

I finally figured it out:

    last_move = new_game.end()
    last_move_san = last_move.san()
    old_last = game.end()
    old_last_san = old_last.san()
    if last_move_san != old_last_san:
        new_move = game.end().board().push_san(last_move_san)
        game.end().add_main_variation(new_move, comment = datetime.strftime(tdate, "%m/%d"))

GameNode.add_main_variation() altered the game the way I needed.

ravl1084
  • 291
  • 4
  • 11