Skip to content

How do I import and and export a game in PGN after playing a move? #879

Answered by niklasf
Roy-Orbison asked this question in Q&A
Discussion options

You must be logged in to vote

Extend mainline with a single move

To edit a game, you can use methods like GameNode.add_main_variation():

game = chess.pgn.Game()
node = game.end()

result = engine.play(node.board(), chess.engine.Limit(time=2.0))
node = node.add_main_variation(result.move)
# node now again points to the end of the game

Convert to and from board

If the game is completely linear with no side variations, you can also just work with chess.Board only.

board = game.end().board()  # Create board with all moves of an existing game

result = engine.play(node.board(), chess.engine.Limit(time=2.0))
board.push(resul.move)

# ...

print(board.root().variation_san(board.move_stack))  # Print move stack in SAN directly

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by Roy-Orbison
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants