I want to sequentially print the moves (one string per move at a time) from a game I read (using the python-chess library) from a text file.
So, say I have a pgn file with a game that has the following moves...
1. f3 e5 2. g4 Qh4#
... I would like to iterate through the moves and print them one by one (using a for loop or similar), showing
f3
e5
g4
Qh4
I found the documentation for python-chess here: https://python-chess.readthedocs.io/en/latest/
From the documentation I understand that
- I would need to create an instance of a visitor that can traverse the game nodes in PGN order using the accept method
- that the san methon would give me the string for the move that led to the current node
But I find this kind of documentation hard to read and would be greatly helped with examples.
What I managed to do is read a game from a pgn file and print all the moves in one go (as opposed to one by one) using the variation method.
import chess.pgn
pgn = open('I:\myfile.pgn')
my_game = chess.pgn.read_game(pgn)
print(my_game.variation(0))