Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 蒂克塔克托-如果有赢家,我如何停止比赛,_Python_Tic Tac Toe - Fatal编程技术网

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

您应该首先了解以下步骤:

  • 随机初始化棋盘(设置棋盘列表、玩家姓名、玩家标志(X或O)
  • 设置while循环并执行以下操作
  • 要求球员提供一个有效的位置
  • 检查位置是否有效
  • 将棋盘索引设置为玩家的标志
  • 检查玩家是否获胜并打破循环
  • 检查电路板是否已填充,如果为真,则为平断
  • 更换播放机并转至步骤3

  • 您尝试过中断吗?您必须从
    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