python - 如何获取 python-chess 模块中所有合法移动的列表?
问题描述
我正在使用 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
如何像使用列表一样选择移动?那么,我将如何将选择用作移动?
解决方案
该board.legal_moves
对象是一个生成器,或者更具体地说是一个LegalMoveGenerator
. 您可以迭代该对象,并且每次迭代都会产生一些东西。您可以将其转换为列表,list(board.legal_moves)
然后正常索引。
import chess
board = chess.Board()
legal_moves = list(board.legal_moves)
legal_moves[0] # Move.from_uci('g1h3')
推荐阅读
- python - 如何在调整 tkinter 窗口大小时避免列或行大小?
- sql - PSQL - 查找 Postgres 数据库中表名以 temp 关键字开头或结尾的所有模式的表数
- matlab - MATLAB Wlan 工具箱 - 排队模型
- c - 如何将字段值写入另一个字段 - C
- mysql - Code Ignitor - 在登录/会话负载下 MySQL CPU 峰值和致命 MySQL 死锁
- bash - 如何在 Hyperledger Fabric 中检索当前链码版本?
- javascript - 我如何解决多个承诺,因为它们解决了?
- xml - 如何从 xsl 文件中转义关键字“pos”
- javascript - 使用 RXJS 取消嵌套订阅
- c# - 使用 c# unity解码 GStreamer jpeg 流