-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTicTacToe.py
294 lines (256 loc) · 7.7 KB
/
TicTacToe.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
from tkinter import *
from tkinter import messagebox
root = Tk()
root.title("Tic_Tac_Toe")
clicked = True
count = 0
def disable_all_buttons():
b1.config(state=DISABLED)
b2.config(state=DISABLED)
b3.config(state=DISABLED)
b4.config(state=DISABLED)
b5.config(state=DISABLED)
b6.config(state=DISABLED)
b7.config(state=DISABLED)
b8.config(state=DISABLED)
b9.config(state=DISABLED)
def checkifwon():
global winner
winner = False
if b1["text"] == "X" and b2["text"] == "X" and b3["text"] == "X":
b1.config(bg="red")
b2.config(bg="red")
b3.config(bg="red")
winner = True
messagebox.showinfo("Tic_Tac_Toe", "1st player wins!")
disable_all_buttons()
elif b4["text"] == "X" and b5["text"] == "X" and b6["text"] == "X":
b4.config(bg="red")
b5.config(bg="red")
b6.config(bg="red")
winner = True
messagebox.showinfo("Tic_Tac_Toe", "1st player wins!")
disable_all_buttons()
elif b7["text"] == "X" and b8["text"] == "X" and b9["text"] == "X":
b7.config(bg="red")
b8.config(bg="red")
b9.config(bg="red")
winner = True
messagebox.showinfo("Tic_Tac_Toe", "1st player wins!")
disable_all_buttons()
elif b1["text"] == "X" and b4["text"] == "X" and b7["text"] == "X":
b1.config(bg="red")
b4.config(bg="red")
b7.config(bg="red")
winner = True
messagebox.showinfo("Tic_Tac_Toe", "1st player wins!")
disable_all_buttons()
elif b2["text"] == "X" and b5["text"] == "X" and b8["text"] == "X":
b2.config(bg="red")
b5.config(bg="red")
b8.config(bg="red")
winner = True
messagebox.showinfo("Tic_Tac_Toe", "1st player wins!")
disable_all_buttons()
elif b3["text"] == "X" and b6["text"] == "X" and b9["text"] == "X":
b3.config(bg="red")
b6.config(bg="red")
b9.config(bg="red")
winner = True
messagebox.showinfo("Tic_Tac_Toe", "1st player wins!")
disable_all_buttons()
elif b1["text"] == "X" and b5["text"] == "X" and b9["text"] == "X":
b1.config(bg="red")
b5.config(bg="red")
b9.config(bg="red")
winner = True
messagebox.showinfo("Tic_Tac_Toe", "1st player wins!")
disable_all_buttons()
elif b3["text"] == "X" and b5["text"] == "X" and b7["text"] == "X":
b3.config(bg="red")
b5.config(bg="red")
b7.config(bg="red")
winner = True
messagebox.showinfo("Tic_Tac_Toe", "1st player wins!")
disable_all_buttons()
#### CHECK FOR O's Win
elif b1["text"] == "O" and b2["text"] == "O" and b3["text"] == "O":
b1.config(bg="green")
b2.config(bg="green")
b3.config(bg="green")
winner = True
messagebox.showinfo("Tic_Tac_Toe", "2nd player wins!")
disable_all_buttons()
elif b4["text"] == "O" and b5["text"] == "O" and b6["text"] == "O":
b4.config(bg="green")
b5.config(bg="green")
b6.config(bg="green")
winner = True
messagebox.showinfo("Tic_Tac_Toe", "2nd player wins!")
disable_all_buttons()
elif b7["text"] == "O" and b8["text"] == "O" and b9["text"] == "O":
b7.config(bg="green")
b8.config(bg="green")
b9.config(bg="green")
winner = True
messagebox.showinfo("Tic_Tac_Toe", "2nd player wins!")
disable_all_buttons()
elif b1["text"] == "O" and b4["text"] == "O" and b7["text"] == "O":
b1.config(bg="green")
b4.config(bg="green")
b7.config(bg="green")
winner = True
messagebox.showinfo("Tic_Tac_Toe", "2nd player wins!")
disable_all_buttons()
elif b2["text"] == "O" and b5["text"] == "O" and b8["text"] == "O":
b2.config(bg="green")
b5.config(bg="green")
b8.config(bg="green")
winner = True
messagebox.showinfo("Tic_Tac_Toe", "2nd player wins!")
disable_all_buttons()
elif b3["text"] == "O" and b6["text"] == "O" and b9["text"] == "O":
b3.config(bg="green")
b6.config(bg="green")
b9.config(bg="green")
winner = True
messagebox.showinfo("Tic_Tac_Toe", "2nd player wins!")
disable_all_buttons()
elif b1["text"] == "O" and b5["text"] == "O" and b9["text"] == "O":
b1.config(bg="green")
b5.config(bg="green")
b9.config(bg="green")
winner = True
messagebox.showinfo("Tic_Tac_Toe", "2nd player wins!")
disable_all_buttons()
elif b3["text"] == "O" and b5["text"] == "O" and b7["text"] == "O":
b3.config(bg="green")
b5.config(bg="green")
b7.config(bg="green")
winner = True
messagebox.showinfo("Tic_Tac_Toe", "2nd player wins!")
disable_all_buttons()
# Check if tie
if count == 9 and winner == False:
messagebox.showinfo("Tic_Tac_Toe", "It's a Tie!\n There's no winer!")
disable_all_buttons()
# Button clicked function
def b_click(b):
global clicked, count
if b["text"] == " " and clicked == True:
b["text"] = "X"
clicked = False
count += 1
checkifwon()
elif b["text"] == " " and clicked == False:
b["text"] = "O"
clicked = True
count += 1
checkifwon()
else:
messagebox.showerror(
"Tic_Tac_Toe",
"That box has already selected\nPlease pick another box...",
)
def reset():
global b1, b2, b3, b4, b5, b6, b7, b8, b9
global clicked, count
clicked = True
count = 0
b1 = Button(
root,
text=" ",
font=("Helvetica", 20),
height=3,
width=6,
bg="SystemButtonFace",
command=lambda: b_click(b1),
)
b2 = Button(
root,
text=" ",
font=("Helvetica", 20),
height=3,
width=6,
bg="SystemButtonFace",
command=lambda: b_click(b2),
)
b3 = Button(
root,
text=" ",
font=("Helvetica", 20),
height=3,
width=6,
bg="SystemButtonFace",
command=lambda: b_click(b3),
)
b4 = Button(
root,
text=" ",
font=("Helvetica", 20),
height=3,
width=6,
bg="SystemButtonFace",
command=lambda: b_click(b4),
)
b5 = Button(
root,
text=" ",
font=("Helvetica", 20),
height=3,
width=6,
bg="SystemButtonFace",
command=lambda: b_click(b5),
)
b6 = Button(
root,
text=" ",
font=("Helvetica", 20),
height=3,
width=6,
bg="SystemButtonFace",
command=lambda: b_click(b6),
)
b7 = Button(
root,
text=" ",
font=("Helvetica", 20),
height=3,
width=6,
bg="SystemButtonFace",
command=lambda: b_click(b7),
)
b8 = Button(
root,
text=" ",
font=("Helvetica", 20),
height=3,
width=6,
bg="SystemButtonFace",
command=lambda: b_click(b8),
)
b9 = Button(
root,
text=" ",
font=("Helvetica", 20),
height=3,
width=6,
bg="SystemButtonFace",
command=lambda: b_click(b9),
)
b1.grid(row=0, column=0)
b2.grid(row=0, column=1)
b3.grid(row=0, column=2)
b4.grid(row=1, column=0)
b5.grid(row=1, column=1)
b6.grid(row=1, column=2)
b7.grid(row=2, column=0)
b8.grid(row=2, column=1)
b9.grid(row=2, column=2)
my_menu = Menu(root)
root.config(menu=my_menu)
options_menu = Menu(my_menu, tearoff=False)
my_menu.add_cascade(label="Reset", menu=options_menu)
options_menu.add_command(label="Rest Game", command=reset)
reset()
root.mainloop()