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