Advertisement
Arcot

tic tac toe

Oct 13th, 2022
1,036
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.61 KB | None | 0 0
  1. from tkinter import Tk,ttk,Button
  2. from tkinter import messagebox
  3. from random import randint
  4.  
  5. ActivePlayer = 1
  6. p1 = []
  7. p2 = []
  8. mov = 0
  9.  
  10. def SetLayout(id,player_symbol):
  11.     if id==1:
  12.         b1.config(text= player_symbol)
  13.         b1.state(['disabled'])
  14.     elif id==2:
  15.         b2.config(text= player_symbol)
  16.         b2.state(['disabled'])
  17.     elif id==3:
  18.         b3.config(text= player_symbol)
  19.         b3.state(['disabled'])
  20.     elif id==4:
  21.         b4.config(text= player_symbol)
  22.         b4.state(['disabled'])
  23.     elif id==5:
  24.         b5.config(text= player_symbol)
  25.         b5.state(['disabled'])
  26.     elif id==6:
  27.         b6.config(text= player_symbol)
  28.         b6.state(['disabled'])
  29.     elif id==7:
  30.         b7.config(text= player_symbol)
  31.         b7.state(['disabled'])
  32.     elif id==8:
  33.         b8.config(text= player_symbol)
  34.         b8.state(['disabled'])
  35.     elif id==9:
  36.         b9.config(text= player_symbol)
  37.         b9.state(['disabled'])
  38.  
  39. def CheckWinner():
  40.     global mov
  41.     winner = -1
  42.  
  43.     if(1 in p1) and (2 in p1) and (3 in p1):
  44.         winner = 1
  45.     if(1 in p2) and (2 in p2) and (3 in p2):
  46.         winner = 2
  47.  
  48.     if(4 in p1) and (5 in p1) and (6 in p1):
  49.         winner = 1
  50.     if(4 in p2) and (5 in p2) and (6 in p2):
  51.         winner = 2
  52.  
  53.     if(7 in p1) and (8 in p1) and (9 in p1):
  54.         winner = 1
  55.     if(7 in p2) and (8 in p2) and (9 in p2):
  56.         winner = 2
  57.  
  58.     if(1 in p1) and (4 in p1) and (7 in p1):
  59.         winner = 1
  60.     if(1 in p2) and (4 in p2) and (7 in p2):
  61.         winner = 2
  62.  
  63.     if(2 in p1) and (5 in p1) and (8 in p1):
  64.         winner = 1
  65.     if(2 in p2) and (5 in p2) and (8 in p2):
  66.         winner = 2
  67.  
  68.     if(3 in p1) and (6 in p1) and ( 9 in p1):
  69.         winner = 1
  70.     if(3 in p2) and (6 in p2) and (9 in p2):
  71.         winner = 2
  72.  
  73.     if(1 in p1) and (5 in p1) and ( 9 in p1):
  74.         winner = 1
  75.     if(1 in p2) and (5 in p2) and (9 in p2):
  76.         winner = 2
  77.  
  78.     if(3 in p1) and (5 in p1) and ( 7 in p1):
  79.         winner = 1
  80.     if(3 in p2) and (5 in p2) and (7 in p2):
  81.         winner = 2
  82.  
  83.     if winner ==1:
  84.         messagebox.showinfo(title="Congratulations.",
  85.             message="Player 1 is the winner")
  86.     elif winner ==2:
  87.         messagebox.showinfo(title="Congratulations.",
  88.             message="Player 2 is the winner")
  89.     elif mov ==9:
  90.         messagebox.showinfo(title="Draw",
  91.             message="It's a Draw!!")
  92.  
  93. def ButtonClick(id):
  94.     global ActivePlayer
  95.     global p1,p2
  96.     global mov
  97.  
  98.     if(ActivePlayer ==1):
  99.         SetLayout(id,"X")
  100.         p1.append(id)
  101.         mov +=1
  102.         root.title("Tic Tac Toe : Player 2")
  103.         ActivePlayer =2
  104.  
  105.     elif(ActivePlayer==2):
  106.         SetLayout(id,"O")
  107.         p2.append(id)
  108.         mov +=1
  109.         root.title("Tic Tac Toe : Player 1")
  110.         ActivePlayer =1
  111.     CheckWinner()
  112.  
  113. def AutoPlay():
  114.     global p1; global p2
  115.     Empty = []
  116.     for cell in range(9):
  117.         if(not((cell +1 in p1) or (cell +1 in p2))):
  118.             Empty.append(cell+1)
  119.     try:
  120.         RandIndex = randint(0,len(Empty) -1)
  121.         ButtonClick(Empty[RandIndex])
  122.     except:
  123.         pass
  124.  
  125. def EnableAll():
  126.     b1.config(text= " ")
  127.     b1.state(['!disabled'])
  128.     b2.config(text= " ")
  129.     b2.state(['!disabled'])
  130.     b3.config(text= " ")
  131.     b3.state(['!disabled'])
  132.     b4.config(text= " ")
  133.     b4.state(['!disabled'])
  134.     b5.config(text= " ")
  135.     b5.state(['!disabled'])
  136.     b6.config(text= " ")
  137.     b6.state(['!disabled'])
  138.     b7.config(text= " ")
  139.     b7.state(['!disabled'])
  140.     b8.config(text= " ")
  141.     b8.state(['!disabled'])
  142.     b9.config(text= " ")
  143.     b9.state(['!disabled'])
  144.  
  145.  
  146. def Restart():
  147.     global p1,p2,mov,ActivePlayer
  148.     p1.clear(); p2.clear()
  149.     mov,ActivePlayer = 0,1
  150.     root.title("Tic Tac Toe : Player 1")
  151.     EnableAll()
  152.  
  153.  
  154.  
  155.  
  156. root = Tk()
  157. root.title("Tic Tac toe : Player 1")
  158. st = ttk.Style()
  159. st.configure("my.TButton", font=('Chiller',24,'bold'))
  160.  
  161. b1 = ttk.Button(root, text=" ", style="my.TButton")
  162. b1.grid(row=1, column=0, sticky="nwse", ipadx=50,ipady=50)
  163. b1.config(command = lambda : ButtonClick(1))
  164.  
  165.  
  166. b2 = ttk.Button(root, text=" ",style ="my.TButton")
  167. b2.grid(row=1, column=1, sticky="snew", ipadx=50, ipady=50)
  168. b2.config(command = lambda : ButtonClick(2))
  169.  
  170. b3= ttk.Button(root, text=" ",style="my.TButton")
  171. b3.grid(row=1, column=2, sticky="snew", ipadx=50,
  172.         ipady=50)
  173. b3.config(command = lambda : ButtonClick(3))
  174.  
  175. b4 = ttk.Button(root, text=" ",style="my.TButton")
  176. b4.grid(row=2, column=0, sticky="snew", ipadx=50,
  177.         ipady=50)
  178. b4.config(command = lambda : ButtonClick(4))
  179.  
  180. b5 = ttk.Button(root, text=" ",style="my.TButton")
  181. b5.grid(row=2, column=1, sticky="snew", ipadx=50,
  182.         ipady=50)
  183. b5.config(command = lambda : ButtonClick(5))
  184.  
  185. b6 = ttk.Button(root, text=" ",style="my.TButton")
  186. b6.grid(row=2, column=2, sticky="snew", ipadx=50,
  187.         ipady=50)
  188. b6.config(command = lambda : ButtonClick(6))
  189.  
  190. b7 = ttk.Button(root, text=" ",style="my.TButton")
  191. b7.grid(row=3, column=0, sticky="snew", ipadx=50,
  192.         ipady=50)
  193. b7.config(command = lambda : ButtonClick(7))
  194.  
  195. b8 = ttk.Button(root, text=" ",style="my.TButton")
  196. b8.grid(row=3, column=1, sticky="snew", ipadx=50,
  197.         ipady=50)
  198. b8.config(command = lambda : ButtonClick(8))
  199.  
  200. b9 = ttk.Button(root, text=" ",style="my.TButton")
  201. b9.grid(row=3, column=2, sticky="snew", ipadx=50,
  202.         ipady=50)
  203. b9.config(command = lambda : ButtonClick(9))
  204.  
  205. Button(text="New Game..", font=('Courier new', 22, 'bold'), bg='blue', fg='white',
  206.             border=5, width=4,command = lambda :Restart()).grid(row=0, column=1, sticky="we")
  207. root.resizable(0,0)
  208. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement