Squawylaous

tic tac toe python (bad)

May 15th, 2021
922
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. def prep(x):
  2.     if x==0: y=" "
  3.     elif x==1: y="X"
  4.     elif x==2: y="O"
  5.     return y
  6. def out(t3): print(" "+prep(t3[0]),"│",prep(t3[1]),"│",prep(t3[2]),"\n───┼───┼───\n",prep(t3[3]),"│",prep(t3[4]),"│",prep(t3[5]),"\n───┼───┼───\n",prep(t3[6]),"│",prep(t3[7]),"│",prep(t3[8]))
  7. def check(t3):
  8.     x=0
  9.     for i in range(9):
  10.         if t3[i]!=0: x+=1
  11.     if x<5: y=0
  12.     elif ((t3[4]==t3[0] and t3[4]==t3[8]) or (t3[4]==t3[2] and t3[4]==t3[6]) or (t3[4]==t3[1] and t3[4]==t3[7]) or (t3[4]==t3[3] and t3[4]==t3[5])) and t3[4]!=0: y=t3[4]
  13.     elif (t3[1]==t3[0] and t3[1]==t3[2]) or (t3[3]==t3[6] and t3[3]==t3[0]) and t3[0]!=0: y=t3[0]
  14.     elif (t3[7]==t3[6] and t3[7]==t3[8]) or (t3[5]==t3[2] and t3[5]==t3[8]) and t3[8]!=0: y=t3[8]
  15.     elif x==9: y=3
  16.     else: y=0
  17.     return y
  18. def turn(x,t3,p):
  19.     t3[x-1]=p
  20.     out(t3)
  21.     return t3
  22. # X │ X │ X
  23. #───┼───┼───
  24. # X │ X │ X
  25. #───┼───┼───
  26. # X │ X │ X
  27. play="y"
  28. while play=="Y" or play=="y":
  29.     t3=[0,0,0,0,0,0,0,0,0]
  30.     w=0
  31.     p=2
  32.     c=input("Do you want to play against another person? Y/N ")
  33.     out(t3)
  34.     while w==0:
  35.         if p==2: p=1
  36.         else: p=2
  37.         if c=="Y" or c=="y" or p==1: x=int(input("Player "+str(p)+"'s turn. Input 1-9 to play. "))
  38.         else: x=5
  39.         l=0
  40.         while t3[x-1]!=0:
  41.             if c=="Y" or c=="y" or p==1: x=int(input("That space is taken. turn. Input 1-9 to play. "))
  42.             else:
  43.                 if l==0: x=1
  44.                 elif l==1: x=9
  45.                 elif l==2: x=3
  46.                 elif l==3: x=5
  47.                 elif l==4: x=2
  48.                 elif l==5: x=8
  49.                 elif l==6: x=4
  50.                 elif l==7: x=6
  51.             l+=1
  52.         t3=turn(x,t3,p)
  53.         w=check(t3)
  54.     if w==3: print("Tie!")
  55.     else: print("Player",w,"wins! Congratulations!")
  56.     play=input("Do you want to play again? (Y/N) ")
  57. print("Goodbye!")
RAW Paste Data