2

这里我们成功解析了一个 PGN 游戏:

import chess, chess.svg, chess.pgn, io
game = chess.pgn.read_game(io.StringIO("1. e4 e5 2. Nf3 *"))
print(game)

不幸的是,board()仍然是初始位置。如何获得最终位置?

print(game.board())
# r n b q k b n r
# p p p p p p p p
# . . . . . . . .
# . . . . . . . .
# . . . . . . . .
# . . . . . . . .
# P P P P P P P P
# R N B Q K B N R
4

1 回答 1

3

python-chess 文档给出了以下示例:

>>> import chess.pgn
>>>
>>> pgn = open("data/pgn/kasparov-deep-blue-1997.pgn")
>>>
>>> first_game = chess.pgn.read_game(pgn)
>>> second_game = chess.pgn.read_game(pgn)
>>>
>>> first_game.headers["Event"] 
'IBM Man-Machine, New York USA'
>>>
>>> # Iterate through all moves and play them on a board.
>>> board = first_game.board()
>>> for move in first_game.mainline_moves(): 
...     board.push(move) 
...
>>> board 
Board('4r3/6P1/2p2P1k/1p6/pP2p1R1/P1B5/2P2K2/3r4 b - - 0 45') 

因此,您需要阅读(主线)移动mainline_moves并将它们推到板上(从 获取game.board()),然后打印该板。

game.end()或者,您可以使用该方法将游戏移动到主线的末尾。然后你可以打印游戏对象:

game.end().board()
于 2021-01-17T10:27:24.910 回答