Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.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_Feedback - Fatal编程技术网

Python 为什么在游戏结束之前我不能运行程序

Python 为什么在游戏结束之前我不能运行程序,python,tic-tac-toe,feedback,Python,Tic Tac Toe,Feedback,任何人都可以告诉我我的代码有什么问题 我试着运行它,我得到了第一个回合,然后程序停止 我试着在python教程中运行它,看他们一场接一场地玩 理解。 我想不出是什么问题 请睁开我的眼睛,指引我去寻找 如何解决这个问题 可能是循环造成了问题,或者我有太多的函数 那些函数调用其他函数 我是一个初学者,我喜欢阅读和学习我需要提高的东西 def winner_x(board): i = 0 # i = index x = ord('X') x1 = 0 x2 = 0

任何人都可以告诉我我的代码有什么问题

我试着运行它,我得到了第一个回合,然后程序停止 我试着在python教程中运行它,看他们一场接一场地玩 理解。 我想不出是什么问题 请睁开我的眼睛,指引我去寻找 如何解决这个问题

可能是循环造成了问题,或者我有太多的函数 那些函数调用其他函数

我是一个初学者,我喜欢阅读和学习我需要提高的东西

def winner_x(board):
    i = 0  # i = index
    x = ord('X')
    x1 = 0
    x2 = 0
    x3 = 0
    while i <= 2:  # loop to scan the lists
        if board[i].count('X') == 3:
             return True
        elif ord((board[i][0])) == x:
            x1 += 1
            if x1 == 3:
               return True
        elif ord((board[i][1])) == x:
            x2 += 1
            if x2 == 3:
                return True
        elif ord((board[i][2])) == x:
            x3 += 1
            if x3 == 3:
               return True
        if ord((board[0][0])) == x and ord(board[1][1]) == x and ord(board[2][2]) == x:
               return True
        if ord((board[0][2])) == x and ord(board[1][1]) == x and ord(board[2][0]) == x:
               return True
        i += 1



    def winner_o(board):
       i = 0  # i = index
       o = ord('O')
       o1 = 0
       o2 = 0
       o3 = 0
       while i <= 2:  # loop to scan the lists
           if board[i].count('X') == 3:
              return True
           elif ord((board[i][0])) == o:
              o1 += 1
              if o1 == 3:
                 return True
           elif ord((board[i][1])) == o:
              o2 += 1
              if o2 == 3:
                 return True
           elif ord((board[i][2])) == o:
               o3 += 1
               if o3 == 3:
                  return True
           if ord((board[0][0])) == o and ord(board[1][1]) == o and ord(board[2][2]) == o:
                 return True
           if ord((board[0][2])) == o and ord(board[1][1]) == o and ord(board[2][0]) == o:
                 return True
           i += 1



def draw(board):
    if winner_o(board) != True and winner_x(board) != True or board.count('-') == 9:
       return True



def checkboard(board):
   if winner_x(board) == True:
      return "The Winner Is X"
   elif winner_o(board) == True:
      return "The Winner Is O"
   elif draw(board) == True:
      return "Draw"



def check_player(board, player1): #function to place X/O and check if its possible
check = True
line = '-'
while check == True:
    c1 = int(input("Player " + player1 + " Choose line (0-2,0-2):"))
    c2 = int(input("Player " + player1 + " Choose column (0-2,0-2):"))
    if 0 <= c1 <= 2 and 0 <= c2 <= 2:
        if board[c1][c2] == line:
            board[c1][c2] = player1
            check = False
        else:
            check = True
    else:
        check = True
        return board


def tic_tac_toe(board, player):# 
        counter = 0
        while counter <= 9:
            while draw(board) == True:
                if counter % 2 == 0: # when its true the player O get start
                    player = 'O'
                    check_player(board, player)# check if the cell is empty or avilable
                    print(board[0])
                    print(board[1])
                    print(board[2])
                    counter += 1
                elif counter % 3 == 0: # when its True player X can play is turn
                    player = 'X'
                    check_player(board, player)
                    print(board[0])
                    print(board[1])
                    print(board[2])
                    counter += 1

                checkboard(board) #check the board if we have win or draw    

board = [['-', '-', '-'], ['-', '-', '-'], ['-', '-', '-']]
player = input("Please Choose X/O: ")

tic_tac_toe(board, player)
def winner_x(板):
i=0#i=index
x=ord('x')
x1=0
x2=0
x3=0

而在tic\u tac\u toe功能中,i 你有

但是你需要

 elif counter % 2 == 1: # when its True player X can play is turn

在您开始的第一行“def”之后,您是否需要缩进?请更正缩进;目前来看,这段代码不是有效的Python。或者,事实上,只要
else:
也可以。
 elif counter % 2 == 1: # when its True player X can play is turn