如何在python-chess上使用合法的移动函数?
运行此代码并输入e2e4。事实证明,移动是无效的。
import asyncio
import chess
import chess.engine
engine = chess.engine.SimpleEngine.popen_uci("stockfish.exe")
board = chess.Board()
while not board.is_game_over():
legal_count = board.legal_moves.count()
move_list = list(board.legal_moves) # Find legal moves
print (move_list)
print(board)
mover = input("Indica tu movimiento: ")
if mover not in move_list:
print ("vuelve a introducir una movida válida")
mover = input("otra movida: ")
board.push_xboard(mover)
result = engine.play(board, chess.engine.Limit(time=0.1))
board.push(result.move)
print(board)
engine.quit()当我浏览清单时我发现..。有这样的价值:
[Move.from_uci('g1h3'), Move.from_uci('g1f3')那么,我不是从legal_moves获得用户的合法移动吗??
或者假设我必须以这种方式输入值?我什么都不了解。
我想要的是让用户通过控制台"e2e4“输入值,并验证移动是否可能。(顺便说一句,我需要的是"e2e4",而不是"e4“或"Nf3")。
谢谢。
发布于 2021-10-26 21:58:52
将用户输入转换为python-chess移动格式。
mover = input("Indica tu movimiento: ")
mover = chess.Move.from_uci(mover) # add this line然后使用:
board.push(mover)而不是:
board.push_xboard(mover) # incorrect由于stockfish引擎不是xboard引擎,所以它是uci引擎。
输出:
python so_chess.py
[Move.from_uci('g1h3'), Move.from_uci('g1f3'), Move.from_uci('b1c3'), Move.from_uci('b1a3'), Move.from_uci('h2h3'), Move.from_uci('g2g3'), Move.from_uci('f2f3'), Move.from_uci('e2e3'), Move.from_uci('d2d3'), Move.from_uci('c2c3'), Move.from_uci('b2b3'), Move.from_uci('a2a3'), Move.from_uci('h2h4'), Move.from_uci('g2g4'), Move.from_uci('f2f4'), Move.from_uci('e2e4'), Move.from_uci('d2d4'), Move.from_uci('c2c4'), Move.from_uci('b2b4'), Move.from_uci('a2a4')]
r n b q k b n r
p p p p p p p p
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
P P P P P P P P
R N B Q K B N R
Indica tu movimiento: e2e4
r n b q k b n r
p p . p p p p p
. . . . . . . .
. . p . . . . .
. . . . P . . .
. . . . . . . .
P P P P . P P P
R N B Q K B N R
[Move.from_uci('g1h3'), Move.from_uci('g1f3'), Move.from_uci('g1e2'), Move.from_uci('f1a6'), Move.from_uci('f1b5'), Move.from_uci('f1c4'), Move.from_uci('f1d3'), Move.from_uci('f1e2'), Move.from_uci('e1e2'), Move.from_uci('d1h5'), Move.from_uci('d1g4'), Move.from_uci('d1f3'), Move.from_uci('d1e2'), Move.from_uci('b1c3'), Move.from_uci('b1a3'), Move.from_uci('e4e5'), Move.from_uci('h2h3'), Move.from_uci('g2g3'), Move.from_uci('f2f3'), Move.from_uci('d2d3'), Move.from_uci('c2c3'), Move.from_uci('b2b3'), Move.from_uci('a2a3'), Move.from_uci('h2h4'), Move.from_uci('g2g4'), Move.from_uci('f2f4'), Move.from_uci('d2d4'), Move.from_uci('c2c4'), Move.from_uci('b2b4'), Move.from_uci('a2a4')]
r n b q k b n r
p p . p p p p p
. . . . . . . .
. . p . . . . .
. . . . P . . .
. . . . . . . .
P P P P . P P P
R N B Q K B N R
Indica tu movimiento: g1f3
r n b q k b n r
p p . . p p p p
. . . p . . . .
. . p . . . . .
. . . . P . . .
. . . . . N . .
P P P P . P P P
R N B Q K B . Rhttps://stackoverflow.com/questions/66557923
复制相似问题