简体   繁体   English

使用 python-chess 库打印每次移动的剩余时钟时间

[英]Printing remaining clock time per move with the python-chess library

I want to sequentially print the remaining clock time (one string per move at a time) from a game I read (using the python-chess library) from a text file.我想从我从文本文件中读取(使用 python-chess 库)的游戏中顺序打印剩余的时钟时间(每次移动一个字符串)。

So, say I have a pgn file with a game that has the following moves...所以,假设我有一个带有以下动作的游戏的 pgn 文件......

  1. f3 { [%clk 0:05:00] } 1... e5 { [%clk 0:05:00] } 2. g4 { [%clk 0:04:49] } 2... Qh4# { [%clk 0:04:48] } f3 { [%clk 0:05:00] } 1... e5 { [%clk 0:05:00] } 2. g4 { [%clk 0:04:49] } 2... Qh4# { [ %clk 0:04:48] }

... I would like to iterate through the moves and print the corresponding clock time one by one (using a for loop or similar), showing ...我想遍历这些动作并一一打印相应的时钟时间(使用 for 循环或类似方法),显示

0:05:00 0:05:00

0:05:00 0:05:00

0:04:49 0:04:49

0:04:48 0:04:48

I know how to iterate through the moves of a game from: Printing individual moves with the python-chess library我知道如何迭代游戏的动作: 使用 python-chess 库打印单个动作

I changed the code from that answer to我将代码从该答案更改为


pgn = open("test.pgn")
game = chess.pgn.read_game(pgn)

board = game.board()

for move in game.mainline_moves():
    print(game.variation(move))
    board.push(move)

But that did not work.但这没有用。 In the first iteration it did print the complete variation (all moves and clocktime all in one line).在第一次迭代中,它确实打印了完整的变化(所有移动和时钟时间都在一行中)。 In the second iteration it raised an error:在第二次迭代中,它引发了一个错误:

"KeyError: Move.from_uci('e7e5')" “关键错误:Move.from_uci('e7e5')”

Hints:提示:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM