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:
应该检查移动是否在列表中
你试过使用
chess.Move.from_uci("move") in board.legal_moves