Я создаю простую программу для шахмат, и я столкнулся с проблемой предположительно python пропуска кода. Запись программы: find_side ()
Вывод на консоль:
Enter your team(1-black 2-white):1
<PlayResult at 0x3ec1dc0 (move=e2e4, ponder=d7d5, info={}, draw_offered=False, resigned=False)>
Enter your enemies move:
В соответствии с выводом на консоль, движок случайным образом сгенерировал ход для белого игрока и сделал встречный ответ в раздумье. Но у меня есть вход для этого, похоже, что python выполняет result_engine
раньше, чем пользовательский ввод. Кроме того, есть еще одна проблема. Двигатель полностью игнорирует линию chess.engine.turn = turn
. Я использую stockfi sh 11 в качестве движка и import chess
в качестве связи между кодом python и движком с универсальным шахматным движком. Код:
import chess
import chess.engine
import time
import os
def logic_white():
engine = chess.engine.SimpleEngine.popen_uci("C:\\Users\\Admin\\Desktop\\sf.exe")
board = chess.Board()
turn = True # True - white False - black
while True:
chess.engine.turn = turn # This isn't working
result_engine = engine.play(board,chess.engine.Limit(time=0.1))
print(result_engine)
res = input("Enter your enemie's move: ")
move = chess.Move.from_uci(res)
board.push(move)
turn = not turn
time.sleep(0.5)
def logic_black():
engine = chess.engine.SimpleEngine.popen_uci("C:\\Users\\Admin\\Desktop\\sf.exe")
board = chess.Board()
turn = True # True - white False - black
while True:
chess.engine.turn = turn # This isn't working
res = input("Enter your enemie's move: ")
move = chess.Move.from_uci(res) #Inputting the enemy move and putting in on the virtual board
board.push(move)
result_engine = engine.play(board,chess.engine.Limit(time=0.1)) #Calculating best move to respond
print(result_engine)
board.push(result_engine) #Push the engine's move to the virtual board
turn = not turn # Inverting turn, so turns can change from black to white, etc.
time.sleep(0.5)
def find_side():
if(input("Enter your team(1-black 2-white):")) == 1:
logic_black()
else:
logic_white()