Feb-02-2019, 04:29 PM
hello, i am making a tic tac toe game for learning purposes
thank you!
here is the full code:
def win(): global gameover gameover = False if first is True: win1 = board[0] == x and board[1] == x and board[2] == x win2 = board[3] == x and board[4] == x and board[5] == x win3 = board[6] == x and board[7] == x and board[8] == x win4 = board[0] == x and board[3] == x and board[6] == x win5 = board[1] == x and board[4] == x and board[7] == x win6 = board[2] == x and board[5] == x and board[8] == x win7 = board[0] == x and board[4] == x and board[8] == x win8 = board[2] == x and board[4] == x and board[6] == x if (win1 or win2 or win3 or win4 or win5 or win6 or win7 or win8) is True: print("You won!") gameover = True return gameover if first is not True: wino1 = board[0] == o and board[1] == o and board[2] == o wino2 = board[3] == o and board[4] == o and board[5] == o wino3 = board[6] == o and board[7] == o and board[8] == o wino4 = board[0] == o and board[3] == o and board[6] == o wino5 = board[1] == o and board[4] == o and board[7] == o wino6 = board[2] == o and board[5] == o and board[8] == o wino7 = board[0] == o and board[4] == o and board[8] == o wino8 = board[2] == o and board[4] == o and board[6] == o if (wino1 or wino2 or wino3 or wino4 or wino5 or wino6 or wino7 or wino8) is True: print("You won!") gameover = True return gameover def game(): print("welcome to tic-tac-toe") chosexo() while True: if gameover is True: break if first is True: win() appenders() theboard() if first is False: win() theboard() appenders()
Error:welcome to tic-tac-toe
choose X or O: x
You start first
Process finished with exit code 0
the break statement triggers even if the conditions are not met (if gameover is True)thank you!
here is the full code:
import random # the tic tac toe project, (for LEARNING purposes) # AI overtakes your signs # when you make a fault while loop continues (AI makes another move) x = "X" o = "O" board = list(range(10)) def chosexo(): global first xoro = input("choose X or O: ").lower() first = False if xoro == "x": first = True # player starts first print("You start first") return first if xoro == "o": first = False # player starts second print("AI starts first") return first else: raise Exception("Please choose X or O") def appenders(): global userplay userplay = int(input("pick from 1 to 9 to draw the shape you chose")) if 1 <= userplay <= 9: if first is True: if board[userplay - 1] != x and board[userplay - 1] != o: board[userplay - 1] = x else: return print("There is already a sign there!") else: if board[userplay - 1] != x and board[userplay - 1] != o: board[userplay - 1] = o else: return print("There is already a sign there!") else: print("Please pick a number from 1 to 9") def theboard(): print("|------------------|----------------|-------------------|") print("| 1 | 2 | 3 |") print("|", board[0], "|", board[1], '|', board[2], '|') print("|------------------|----------------|-------------------|") print("| 4 | 5 | 6 |") print("|", board[3], "|", board[4], '|', board[5], '|') print("|------------------|----------------|-------------------|") print("| 7 | 8 | 9 |") print("|", board[6], "|", board[7], '|', board[8], '|') print("|------------------|----------------|-------------------|") def win(): global gameover gameover = False if first is True: win1 = board[0] == x and board[1] == x and board[2] == x win2 = board[3] == x and board[4] == x and board[5] == x win3 = board[6] == x and board[7] == x and board[8] == x win4 = board[0] == x and board[3] == x and board[6] == x win5 = board[1] == x and board[4] == x and board[7] == x win6 = board[2] == x and board[5] == x and board[8] == x win7 = board[0] == x and board[4] == x and board[8] == x win8 = board[2] == x and board[4] == x and board[6] == x if (win1 or win2 or win3 or win4 or win5 or win6 or win7 or win8) is True: print("You won!") gameover = True return gameover if first is not True: wino1 = board[0] == o and board[1] == o and board[2] == o wino2 = board[3] == o and board[4] == o and board[5] == o wino3 = board[6] == o and board[7] == o and board[8] == o wino4 = board[0] == o and board[3] == o and board[6] == o wino5 = board[1] == o and board[4] == o and board[7] == o wino6 = board[2] == o and board[5] == o and board[8] == o wino7 = board[0] == o and board[4] == o and board[8] == o wino8 = board[2] == o and board[4] == o and board[6] == o if (wino1 or wino2 or wino3 or wino4 or wino5 or wino6 or wino7 or wino8) is True: print("You won!") gameover = True return gameover def game(): print("welcome to tic-tac-toe") chosexo() while True: if gameover is True: break if first is True: win() appenders() theboard() if first is False: win() theboard() appenders() try: game() except: Exception('Something went wrong!')