我使用的是python国际象棋模块。在网站上,它显示您可以使用以下命令检查移动是否合法
import chess
board = chess.Board()
move = input("Enter a chess move: ")
if move in board.legal_moves:
# Some code to do if the move is a legal move然而,我希望能够从board.legal_moves那里获得进展。当我尝试这样做时:
print(board.legal_moves[0])这将返回以下错误:
TypeError: 'LegalMoveGenerator' object is not subscriptable如何像使用列表一样选择移动?那么,我如何使用选择作为移动?
发布于 2020-05-29 09:06:08
从生成器生成列表。
legal_moves = list(board.legal_moves)法律行动现在成了一个清单。
print(legal_moves)
[Move.from_uci('g1h3'), Move.from_uci('g1f3'), Move.from_uci('b1c3'),
Move.from_uci('b1a3'), Move.from_uci('h2h3'), Move.from_uci('g2g3'),
Move.from_uci('f2f3'), Move.from_uci('e2e3'), Move.from_uci('d2d3'),
Move.from_uci('c2c3'), Move.from_uci('b2b3'), Move.from_uci('a2a3'),
Move.from_uci('h2h4'), Move.from_uci('g2g4'), Move.from_uci('f2f4'),
Move.from_uci('e2e4'), Move.from_uci('d2d4'), Move.from_uci('c2c4'),
Move.from_uci('b2b4'), Move.from_uci('a2a4')]发布于 2020-05-29 09:05:05
board.legal_moves对象是一个generator,或者更具体地说是一个LegalMoveGenerator。您可以迭代该对象,它将在每次迭代中产生一些结果。您可以使用list(board.legal_moves)将其转换为列表,然后像往常一样对其进行索引。
import chess
board = chess.Board()
legal_moves = list(board.legal_moves)
legal_moves[0] # Move.from_uci('g1h3')https://stackoverflow.com/questions/62076938
复制相似问题