Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random, array, sys
- s = array.array("u", [" ", " ", " ", " ", " ", " ", " ", " ", " "])
- b = "-|-|-"
- def board():
- print(f"\n{s[0]}|{s[1]}|{s[2]}\n{b}\n{s[3]}|{s[4]}|{s[5]}\n{b}\n{s[6]}|{s[7]}|{s[8]}")
- def win():
- if s[0] == "x" and s[1] == "x" and s[2] == "x" or s[3] == "x" and s[4] == "x" and s[5] == "x" or s[6] == "x" and s[7] == "x" and s[8] == "x" or s[0] == "x" and s[3] == "x" and s[6] == "x" or s[1] == "x" and s[4] == "x" and s[7] == "x" or s[2] == "x" and s[5] == "x" and s[8] == "x" or s[0] == "x" and s[4] == "x" and s[8] == "x" or s[2] == "x" and s[4] == "x" and s[6] == "x":
- print("Player 1 wins!")
- input("")
- elif s[0] == "o" and s[1] == "o" and s[2] == "o" or s[3] == "o" and s[4] == "o" and s[5] == "o" or s[6] == "o" and s[7] == "o" and s[8] == "o" or s[0] == "o" and s[3] == "o" and s[6] == "o" or s[1] == "o" and s[4] == "o" and s[7] == "o" or s[2] == "o" and s[5] == "o" and s[8] == "o" or s[0] == "o" and s[4] == "o" and s[8] == "o" or s[2] == "o" and s[4] == "o" and s[6] == "o":
- print("Player 2 wins!")
- input("")
- def turns():
- if choice == "1":
- s[0] = psymbol
- elif choice == "2":
- s[1] = psymbol
- elif choice == "3":
- s[2] = psymbol
- elif choice == "4":
- s[3] = psymbol
- elif choice == "5":
- s[4] = psymbol
- elif choice == "6":
- s[5] = psymbol
- elif choice == "7":
- s[6] = psymbol
- elif choice == "8":
- s[7] = psymbol
- elif choice == "9":
- s[8] = psymbol
- else:
- print("Please enter a number between 1 and 9!")
- def chosen():
- print("That square has already been chosen!")
- print("Welcome to Tic-Tac-Toe!\nHow To Play:\nEnter a number between 1 and 9 which correlates to a square on the board below:")
- print(f"\n1|2|3\n{b}\n4|5|6\n{b}\n7|8|9")
- choice = p1t1 = input("\nPlayer 1. Choose a square:\n")
- psymbol = "x"
- turns()
- board()
- win()
- choice = p2t1 = input("\nPlayer 2. Choose a square:\n")
- psymbol = "o"
- if p2t1 == p1t1:
- chosen()
- else:
- turns()
- board()
- win()
- choice = p1t2 = input("\nPlayer 1. Choose a 2nd square:\n")
- psymbol = "x"
- if p1t2 == p1t1 or p1t2 == p2t1:
- chosen()
- else:
- turns()
- board()
- win()
- choice = p2t2 = input("\nPlayer 2. Choose a 2nd square:\n")
- psymbol = "o"
- if p2t2 == p1t1 or p2t2 == p1t2 or p2t2 == p2t1:
- chosen()
- else:
- turns()
- board()
- win()
- choice = p1t3 = input("\nPlayer 1. Choose a 3rd square:\n")
- psymbol = "x"
- if p1t3 == p1t1 or p1t3 == p1t2 or p1t3 == p2t1 or p1t3 == p2t2:
- chosen()
- else:
- turns()
- board()
- win()
- choice = p2t3 = input("\nPlayer 2. Choose a 3rd square:\n")
- psymbol = "o"
- if p2t3 == p1t1 or p2t3 == p1t2 or p2t3 == p1t3 or p2t3 == p2t1 or p2t3 == p2t2:
- chosen()
- else:
- turns()
- board()
- win()
- choice = p1t4 = input("\nPlayer 1. Choose a 4th square:\n")
- psymbol = "x"
- if p1t4 == p1t1 or p1t4 == p1t2 or p1t4 == p1t3 or p1t4 == p2t1 or p1t4 == p2t2 or p1t4 == p2t3:
- chosen()
- else:
- turns()
- board()
- win()
- choice = p2t4 = input("\nPlayer 2. Choose a 4th square:\n")
- psymbol = "o"
- if p2t4 == p1t1 or p2t4 == p1t2 or p2t4 == p1t3 or p2t4 == p1t4 or p2t4 == p2t1 or p2t4 == p2t2 or p2t4 == p2t3:
- chosen()
- else:
- turns()
- board()
- win()
- choice = p1t5 = input("\nPlayer 1. Choose a 5th square:\n")
- psymbol = "x"
- if p1t5 == p1t1 or p1t5 == p1t2 or p1t5 == p1t3 or p1t5 == p1t4 or p1t5 == p2t1 or p1t5 == p2t2 or p1t5 == p2t3 or p1t5 == p2t4:
- chosen()
- else:
- turns()
- board()
- win()
- print("It's a draw!")
- input("")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement