Python 蒂克塔克托-如果有赢家,我如何停止比赛,
这就是程序接收数据的方式Python 蒂克塔克托-如果有赢家,我如何停止比赛,,python,tic-tac-toe,Python,Tic Tac Toe,这就是程序接收数据的方式 x_data = [] def xuser_input(): while True: x = As("Player X, Please enter a number to place: ",int) if (x > 10) or (x < 0): print("Input must be Bigger than 0 and Smaller than 9"
x_data = []
def xuser_input():
while True:
x = As("Player X, Please enter a number to place: ",int)
if (x > 10) or (x < 0):
print("Input must be Bigger than 0 and Smaller than 9")
continue
try:
board[x] = "X"
x_data.append(x)
except (IndexError):
print("Invalid input")
continue
t_Board()
break
这将停止游戏,如果这个条件得到满足,这是获胜的公式
def stops_board():
if (board[0] == board[1] == board[2]) or (board[3] == board[4] == board[5]) or (
board[6] == board[7] == board[8]) or (board[0] == board[3] == board[6]) or (
board[1] == board[4] == board[7]) or (board[2] == board[5] == board[8]) or (
board[0] == board[4] ==
board[8]) or (board[2] == board[4] == board[6]):
return False
现在,我就是这样询问数据输入并检查是否有一个成功的解决方案
while True:
xuser_input()
stops_board()
yuser_input()
stops_board()
在我看来,
stops\u board()
如果游戏应该停止,应该返回True;如果游戏应该继续,应该返回False。对的如果是这样,您可以使用:
while True:
xuser_input()
if stops_board(): break
yuser_input()
if stops_board(): break
您应该首先了解以下步骤:
您尝试过中断吗?您必须从
xuser\u input()调用stops\u board()
用户输入后广义地说,您通过添加return
语句对函数执行提前停止,并通过添加break
语句对for/while循环执行相同的操作。我尝试在函数停止板中中断,但它无法工作,因为其中没有while循环。哦,是的,我应该停止调用_xuser_输入板内部请让我尝试等待否如果获胜的解决方案(停止_板),它将停止游戏并返回False满足,因此将停止While-True循环。至少这是我认为的,无法工作。但是你的建议看起来可以工作,让我试试看。我刚刚得到它,你是对的,所以我将stops_board中的返回False改为返回True,从而使break语句工作,因此它工作了
while True:
xuser_input()
if stops_board(): break
yuser_input()
if stops_board(): break
def stops_board():
if (board[0] == board[1] == board[2]) or (board[3] == board[4] == board[5]) or (
board[6] == board[7] == board[8]) or (board[0] == board[3] == board[6]) or (
board[1] == board[4] == board[7]) or (board[2] == board[5] == board[8]) or (
board[0] == board[4] == board[8]) or (board[2] == board[4] == board[6]):
return True
while True:
xuser_input()
if stops_board(): break
yuser_input()
if stops_board(): break