[英]Issue with python skipping code in simple chess program
我正在為國際象棋創建一個簡單的程序,我遇到了一個據稱是 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
。 此外,還有一個問題。 Engine 完全忽略chess.engine.turn = turn
線。 我使用stockfish 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()
Python 的輸入函數返回一個字符串,因此它永遠不會等於整數 1。因此,代碼將始終進入 else 塊。 要解決此問題,請將輸入轉換為整數或將其與“1”進行比較。
def find_side():
if int(input("Enter your team(1-black 2-white):")) == 1:
logic_black()
else:
logic_white()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.