英文:
chess program and check for legal moves doesn't work and always returns that the move is illegal even if it is
问题
在 if move in legal_moves:
这个部分存在问题,无论输入的字符串是什么(如 e4、e2e4、klsfjds),这个 if 语句始终返回 false。
因为 board.legal_moves
是当前允许的合法移动的生成器,legal_moves = list(board.legal_moves)
应该创建一个包含所有合法移动的列表,并且 if move in legal_moves:
应该检查移动是否在列表中。
英文:
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")
the issue is at if move in legal_moves:
, the if statement always returns false no matter the string (e4, e2e4, klsfjds)
since board.legal_moves is a generator of the current legal moves allowed, legal_moves = list(board.legal_moves)
should create a list of all legal moves, and if move in legal_moves:
should check if the move is in the list
答案1
得分: 1
你尝试过在 board.legal_moves
中使用 chess.Move.from_uci("move")
吗?
来源:https://python-chess.readthedocs.io/en/latest/
英文:
Have you tried using chess.Move.from_uci("move") in board.legal_moves
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论