chess program and check for legal moves doesn't work and always returns that the move is illegal even if it is

huangapple go评论77阅读模式
英文:

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

Source: https://python-chess.readthedocs.io/en/latest/

huangapple
  • 本文由 发表于 2023年5月17日 13:57:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76268944.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定