Advertisement
Arcot

tic tac toe pygame

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