×
☰ Sebsnetwork

TicTacToe - Python - 11KB

                
                    #Tested Version of python:
                    #Python  3.7.9
                    #Python  3.9.13
                    #Python  3.10.7
                    
                    
                    #By Seb  
                    #Last Updated : 25/03/2023 - 23:48 pm
                    
                    # 1 | 2 | 3
                    #-----------
                    # 4 | 5 | 6
                    #-----------
                    # 7 | 8 | 9 
                    
                    #Remeber that table to know where to place your  "x / o" or just pick option 3 at start menu for more explanation! <----
                    
                    #Imports
                    import time
                    import random
                    
                    #Variables
                    one = " "
                    two = " "
                    three = " "
                    four = " "
                    five = " "
                    six = " "
                    seven = " "
                    eight = " "
                    nine = " "
                    allowed = ["1","2","3","4","5","6","7","8","9"]
                    won = ""
                    tie = False
                    lost = ""
                    player1 = "steve"
                    player2 = "tephnaie"
                    playing = True
                    
                    #Clear console
                    def clearconsole():
                        print("\n" * 10)
                        print("\n" * 10)
                        print("\n" * 10)
                        print("\n" * 10)
                        print("\n" * 10)
                    
                    #check if X won
                    def x_check():
                        global one, two, three, four , five, six , seven, eight ,nine, won, tie, lost, playing
                        if one == "x" and four == "x" and seven == "x":
                            won = player1
                            playing = False
                        elif two == "x" and five == "x" and eight == "x":
                            won = player1
                            playing = False
                        elif three == "x" and six == "x" and nine == "x":
                            won = player1
                            playing = False
                        elif one == "x" and two == "x" and three == "x":
                            won = player1
                            playing = False
                        elif four == "x" and five == "x" and six == "x":
                            won = player1
                            playing = False
                        elif seven == "x" and eight == "x" and nine == "x":
                            won = player1
                            playing = False
                        elif one == "x" and five == "x" and nine == "x":
                            won = player1
                            playing = False
                        elif seven == "x" and five == "x" and three == "x":
                            won = player1
                            playing = False
                        else:
                            print("No winner")
                    
                    
                    #Checks if O has won
                    def o_check():
                        global one, two, three, four , five, six , seven, eight ,nine, won, tie, lost, playing
                        if one == "o" and four == "o" and seven == "o":
                            won = player2
                            playing = False
                        elif two == "o" and five == "o" and eight == "o":
                            won = player2
                            playing = False
                        elif three == "o" and six == "o" and nine == "o":
                            won = player2
                            playing = False
                        elif one == "o" and two == "o" and three == "o":
                            won = player2
                            playing = False
                        elif four == "o" and five == "o" and six == "o":
                            won = player2
                            playing = False
                        elif seven == "o" and eight == "o" and nine == "o":
                            won = player2
                            playing = False
                        elif one == "o" and five == "o" and nine == "o":
                            won = player2
                            playing = False
                        elif seven == "o" and five == "o" and three == "o":
                            won = player2
                            playing = False
                        else:
                            print("No winner")
                    
                    #prints the table
                    def table():
                        global one, two, three, four , five, six , seven, eight ,nine
                        clearconsole()
                        print(" " + one + " | " + two + " | " + three) # ----> DO NOT TOUCH <----
                        print("-----------")
                        print(" " + four + " | " + five + " | " + six)# ----> DO NOT TOUCH <----
                        print("-----------")
                        print(" " + seven + " | " + eight + " | " + nine)# ----> DO NOT TOUCH <----
                    
                    #Inputs for X
                    def inputx(choice):
                        global one, two, three, four , five, six , seven, eight ,nine
                        if choice == "1":
                            one = "x"
                        elif choice == "2":
                            two = "x"
                        elif choice == "3":
                            three = "x"
                        elif choice == "4":
                            four = "x"
                        elif choice == "5":
                            five = "x"
                        elif choice == "6":
                            six = "x"
                        elif choice == "7":
                            seven = "x"
                        elif choice == "8":
                            eight = "x"
                        elif choice == "9":
                            nine = "x"
                    
                    #Inputs for o
                    def inputo(choice):
                        global one, two, three, four , five, six , seven, eight ,nine
                        if choice == "1":
                            one = "o"
                        elif choice == "2":
                            two = "o"
                        elif choice == "3":
                            three = "o"
                        elif choice == "4":
                            four = "o"
                        elif choice == "5":
                            five = "o"
                        elif choice == "6":
                            six = "o"
                        elif choice == "7":
                            seven = "o"
                        elif choice == "8":
                            eight = "o"
                        elif choice == "9":
                            nine = "o"
                            
                    #Start of code
                    clearconsole()
                    correct = False
                    while correct == False:
                        print("Welcome to tic tac toe!!!")
                        print("You can play either against the person next to you or computer!!")
                        print("Please pick option below!")
                        print("1. Against another human")
                        print("2. Computer (might be easy might be not)")
                        print("3. How to play!")
                        ans = input("Option: ")
                        if ans == "1":
                            correct = True
                        elif ans == "2":
                            correct = True
                        elif ans == "3":
                            clearconsole()
                            print("You'll be asked to input a number to add a x or o to the board.")
                            print("")
                            print(" 1 | 2 | 3     Remember these numbers so")
                            print("-----------    you know where to place")
                            print(" 4 | 5 | 6     your x and o")
                            print("-----------")
                            print(" 7 | 8 | 9 ")
                            print("")
                            print("Your goals is to get a line with your symbol")
                            print("For example:")
                            print("")
                            print(" x |   |  ")
                            print("-----------")
                            print("   | x |   ")
                            print("-----------")
                            print("   |   | x ")
                            print("")
                            print("But this is not a win:")
                            print("")
                            print(" x | x |  ")
                            print("-----------")
                            print(" x |   |   ")
                            print("-----------")
                            print("   |   |   ")
                            print("")
                            input("Press Enter to continue")
                            clearconsole()
                        else:
                            clearconsole()
                            time.sleep(2)
                            print("ERROR - Wrong Input")
                    #Time to start doing what the player/s chose to play.
                    if ans == "1": # Against another huamn
                        clearconsole()
                        player1 = input("Please input the name of player 1!\n : ")
                        clearconsole()
                        player2 = input("Please now input name of player 2!\n : ")
                        clearconsole()
                        quit1 = False
                        while quit1 == False:
                            while playing == True:
                                table()
                                x = input("Please may " + player1 + " input their answer (1-9) \n : ")
                                good = False
                                while good == False:
                                    if x in allowed:
                                        allowed.remove(x)
                                        inputx(x)
                                        good = True
                                    else:
                                        x = input("Please may " + player1 + " input a good value their answer (1-9) \n : ")
                                x_check()
                                if playing == False:
                                    break
                                else:
                                    clearconsole()
                                if not allowed:
                                    won = "tie"
                                    break
                                table()
                                o = input("Please may " + player2 + " input their answer (1-9) \n : ")
                                good = False
                                while good == False:
                                    if o in allowed:
                                        allowed.remove(o)
                                        inputo(o)
                                        good = True
                                    else:
                                        o = input("Please may " + player2 + " input a good value their answer (1-9) \n : ")
                                o_check()
                                if playing == False:
                                    break
                                else:
                                    clearconsole()
                                if not allowed:
                                    won = "tie"
                                    break
                            clearconsole()
                            table()
                            if won == "tie":
                                print("It was a tie!")
                                print("Pick now!")
                                print("1. Play again?")
                                print("2. Quit")   
                            else:
                                print("Player " + won + " has won!!")
                                print("Pick now!")
                                print("1. Play again?")
                                print("2. Quit")
                            ans = input(" : ")
                            if ans == "1":
                                playing = True
                                one = " "
                                two = " "
                                three = " "
                                four = " "
                                five = " "
                                six = " "
                                seven = " "
                                eight = " "
                                nine = " "
                                allowed = ["1","2","3","4","5","6","7","8","9"]
                                tempplayer1 = player1
                                player1 = player2
                                player2 = tempplayer1
                            elif ans == "2":
                                quit1 = True
                                clearconsole()
                                print("Goodbye!!!!")
                            else:
                                ans = input(" : ")
                    
                    
                    elif ans == "2": # Against a computer, it's basically just guessing (no joke it's just doing random.choice())
                        clearconsole()
                        player1 = input("Please input the name of player 1!\n : ")
                        clearconsole()
                        quit1 = False
                        while quit1 == False:
                            while playing == True:
                                table()
                                x = input("Please may " + player1 + " input their answer (1-9) \n : ")
                                good = False
                                while good == False:
                                    if x in allowed:
                                        allowed.remove(x)
                                        inputx(x)
                                        good = True
                                    else:
                                        x = input("Please may " + player1 + " input a good value their answer (1-9) \n : ")
                                x_check()
                                if playing == False:
                                    break
                                else:
                                    clearconsole()
                                if not allowed:
                                    won = "tie"
                                    break
                                o = random.choice(allowed)
                                allowed.remove(o)
                                inputo(o)
                                o_check()
                                if playing == False:
                                    break
                                else:
                                    clearconsole()
                                if not allowed:
                                    won = "tie"
                                    break
                            clearconsole()
                            table()
                            player2 = "Computer"
                            if won == "tie":
                                print("It was a tie!")
                                print("Pick now!")
                                print("1. Play again?")
                                print("2. Quit")   
                            else:
                                print("Player " + won + " has won!!")
                                print("Pick now!")
                                print("1. Play again?")
                                print("2. Quit")
                            ans = input(" : ")
                            if ans == "1":
                                playing = True
                                one = " "
                                two = " "
                                three = " "
                                four = " "
                                five = " "
                                six = " "
                                seven = " "
                                eight = " "
                                nine = " "
                                allowed = ["1","2","3","4","5","6","7","8","9"]
                            elif ans == "2":
                                quit1 = True
                                clearconsole()
                                print("Goodbye!!!!")
                            else:
                                ans = input(" : ")            
                    
                    #The end