1

I have tried creating a tic tac toe game using Python but it is not working. Can someone please take a look at it and tell me if I made a mistake?

When I run the game it only prints the board and then asks for a number for x. Once I type that, the game prints a board and closes.

def tic_tac_toe(): 

   end = False 
   win_combinations = ((0, 1, 2), (3, 4, 5), (6, 7, 8), (0, 3, 6), (1, 4, 7), (2, 5, 8), (0, 4, 8), (2, 4, 6))
   board = [1,2,3,4,5,6,7,8,9]
   def draw():
      print(board[0], board[1], board[2]) 
      print(board[3], board[4], board[5])
      print(board[6], board[7], board[8])
      print()
   def player_option_check():
      while True:
        try:
            arg1 = input("please enter number")
            arg1 = int(arg1)
            if arg1 in range(0,9):
                return arg1 - 1
            else:
                print ('not a number on he board')
        except:
            print('this is not a number re try')

   def player_X():

     choice = player_option_check()
     board[choice] = 'x'

   def player_y():

     choice = player_option_check()
     board[choice] = 'y'

   def check_winner_x ():
     for a in win_combinations:
         if board[a[0]] == board[a[1]] == board[a[2]] == 'o':
             print('player o wins')
             return True 
         if board[a[0]] == board[a[1]] == board[a[2]] == 'x':
             print('player x wins')
             return True 
     for a in range(9):
        if board[a] == "X" or board[a] == "O":
             count += 1
     if count == 9:
        print("The game ends in a Tie\n")
        return True
while not end:
    draw()
    end = check_winner_x
    if end == True:
        break
    else:
        player_X()
        draw()
        end = check_winner_x
        if end == True:
            break
        else:
            player_y()
            draw()
        if end==True:
            again_play=print(input("would u like to play again press (y/n)"))
            again_play == "y"
            tic_tac_toe()
        else:
            print('thanks for playing')
            break
tic_tac_toe()

So can you please help me find the mistake in my code. This is in Python 3.

Martin Evans
  • 45,791
  • 17
  • 81
  • 97
Rishi Shah
  • 11
  • 1
  • 2
    I am afraid, SO is not a free debugging service. Please read [What topics can I ask about here?](https://stackoverflow.com/help/on-topic), especially #1: "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers." – Mr. T Mar 10 '18 at 08:01
  • I suggest you add some more `print()` statements to help you identify where the code is not doing what you want. – Martin Evans Mar 10 '18 at 12:42

1 Answers1

0

I'm assuming it's cause you're not calling check_winner_x, just assigning it to the value end (maybe print(end) at the end of the loop to see it's value).

That means that when you get back to the while statement, end will register as "truthy" as it's no longer set to false and the loop will exit.

Try actually calling check_winner_x:

end = check_winner_x()

Not sure about anything else that might be buggy, but that's definitely a start.

SCB
  • 5,821
  • 1
  • 34
  • 43