i have a service, that gets a game from database and adding moves to it. also, it edits current pgn (string of moves).Is there any functions, that can get given string of moves and parse moves from it?
I was trying this, but had a problem
board = chess.Board()
if game.moves!="":
for move in game.moves.split():
board.push(chess.Move.from_uci(move))
And after trying to append first move to a new game i've got this issue:
board.push(chess.Move.from_uci(move)) ^^^^^^^^^^^^^^^^^^^^^^^^^ raise InvalidMoveError(f"expected uci string to be of length 4 or 5: {uci!r}") chess.InvalidMoveError: expected uci string to be of length 4 or 5: '1.'
game.moves
is exactly, so we can help you out.game.moves.split()
? It looks like the problem might begame.moves
is in the format"1. e4 e5 2. Nf3 Nc6"
etc, so you will need to skip the segments that just have the move number. This should be pretty simple to implement withenumerate
andindex % 3