国际象棋程序和检查合法移动不起作用并且总是返回移动是非法的,即使它是

问题描述 投票:0回答:1
import chess
board = chess.Board()
print(board)
legal_moves = list(board.legal_moves)
while True:
  move = input ("type a legal move: \n")
  if move in legal_moves:
    board.push_san(move)
    legal_moves = list(board.legal_moves)
    print(board)
  else:
    print("Illegal move! Input a legal move")

问题在

if move in legal_moves:
,if语句总是返回false,不管字符串是什么(e4, e2e4, klsfjds)

因为 board.legal_moves 是当前允许的合法移动的生成器,

legal_moves = list(board.legal_moves)
应该创建所有合法移动的列表,并且
if move in legal_moves:
应该检查移动是否在列表中

python list generator chess
1个回答
1
投票

你试过使用

chess.Move.from_uci("move") in board.legal_moves

来源:https://python-chess.readthedocs.io/en/latest/

© www.soinside.com 2019 - 2024. All rights reserved.