hi!, i made this tic tac toe (not 100% completed yet), i have tried to make the AI more challenging but it's harder than i though, making the code runs a specefic if statement without running another if statemet with same conditions(especially in AIISX func), so i tried tp trick the program to do what i want (sadly not!) anyway here is the code please I WOULD VERY MUCH APPRECIATE ANY SUGGESTIONS OR TIPS thank you soo much! love
import random # the tic tac toe project, (for LEARNING purposes) x = "X" o = "O" board = list(range(10)) gameover = False passage = True problemo = False 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 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 if first: win1a = board[0] == o and board[1] == o and board[2] == o win2a = board[3] == o and board[4] == o and board[5] == o win3a = board[6] == o and board[7] == o and board[8] == o win4a = board[0] == o and board[3] == o and board[6] == o win5a = board[1] == o and board[4] == o and board[7] == o win6a = board[2] == o and board[5] == o and board[8] == o win7a = board[0] == o and board[4] == o and board[8] == o win8a = board[2] == o and board[4] == o and board[6] == o 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 (win1a or win2a or win3a or win4a or win5a or win6a or win7a or win8a) is True: print("AI won!") gameover = True return gameover else: 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 win1oa = board[0] == x and board[1] == x and board[2] == x win2oa = board[3] == x and board[4] == x and board[5] == x win3oa = board[6] == x and board[7] == x and board[8] == x win4oa = board[0] == x and board[3] == x and board[6] == x win5oa = board[1] == x and board[4] == x and board[7] == x win6oa = board[2] == x and board[5] == x and board[8] == x win7oa = board[0] == x and board[4] == x and board[8] == x win8oa = board[2] == x and board[4] == x and board[6] == x 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 if (win1oa or win2oa or win3oa or win4oa or win5oa or win6oa or win7oa or win8oa) is True: print("AI won!") gameover = True return gameover def trickster(): global passage if board[2] == o or board[6] == o: passage = False return passage def AIisO(): global board global problemo global first if first: theboard() 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: problemo = True return print("There is already a sign there!") and problemo else: if board[userplay - 1] != x and board[userplay - 1] != o: board[userplay - 1] = o else: problemo = True return print("There is already a sign there!") and problemo if problemo is False: cheking = True while cheking: for i in board: oldver = board.copy() sumxr = [0] sumx = sum(1 for i in board if i == x or i == o) sumxr.append(sumx) board[random.randint(0, 8)] = o sumxr1 = [0] sumx1 = sum(1 for i in board if i == x or i == o) sumxr1.append(sumx1) if sumxr == sumxr1: board = oldver else: cheking = False return cheking def AIisX(): global board global passage if first is False: board[8] = x theboard() 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") if (passage is True and userplay == 5) or (passage is True and board[4] == o): board[0] = x if (passage is True and (userplay == 2 or userplay == 4 or userplay == 6 or userplay == 8)) or \ (passage is True and (board[1] == o or board[3] == o or board[5] == o or board[8] == o)): cheking = True while cheking: for i in board: oldver = board.copy() sumxr = [0] sumx = sum(1 for i in board if i == x or i == o) sumxr.append(sumx) board[random.randint(0, 8)] = x sumxr1 = [0] sumx1 = sum(1 for i in board if i == x or i == o) sumxr1.append(sumx1) if sumxr == sumxr1: board = oldver else: cheking = False passage = True return cheking and passage if userplay == 3 or board[2] == o: board[6] = x if userplay == 4 or board[3] == o: board[7] = x if userplay == 8 or board[7] == o: board[3] = x if userplay == 2 or board[1] == o: board[3] = x if userplay == 6 or board[5] == o: board[3] = x if userplay == 7 or board[6] == o: board[2] = x if userplay == 4 or board[3] == o: board[1] = x if userplay == 8 or board[7] == o: board[1] = x if userplay == 2 or board[1] == o: board[5] = x if userplay == 6 or board[5] == o: board[1] = x if (userplay == 1 or userplay == 2 or userplay == 3 or userplay == 4 or userplay == 6 or userplay == 7 or userplay == 8 or userplay == 9) \ or (board[0] == o or board[1] == o or board[2] == o or board[3] == o or board[5] == o or board[6] == o): cheking = True while cheking: for i in board: oldver = board.copy() sumxr = [0] sumx = sum(1 for i in board if i == x or i == o) sumxr.append(sumx) board[random.randint(0, 8)] = x sumxr1 = [0] sumx1 = sum(1 for i in board if i == x or i == o) sumxr1.append(sumx1) if sumxr == sumxr1: board = oldver else: cheking = False passage = False return cheking and passage def game(): print("welcome to tic-tac-toe") chosexo() while True: if gameover: break if first is True: AIisO() win() if first is False: trickster() AIisX() win() game()i know i really suck in clean coding but im working on it in the long run, thanks!