简体   繁体   中英

Why I cant run the program until the game is over

anyone can tell me what wrong with my code.

I try to run it and I get the first turn, and then the program stop I try to run it in python tutor and see them play after play to understand. I can't figure out what is the problem please open my eyes and guide me to find how to solve this

maybe the loops make a problem or I have too many functions those call to other function

I am a beginner and I would love to read and learn what I need to improve.

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)

in the tic_tac_toe function you have

 elif counter % 3 == 0: # when its True player X can play is turn

but you need

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM