-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqtris.py
223 lines (190 loc) · 6.82 KB
/
qtris.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
from tkinter import *
from tkinter import messagebox
root = Tk()
root.title('Codemy.com - Tic-Tac-Toe')
#root.geometry("1200x710")
# X starts so true
clicked = True
count = 0
# disable all the buttons
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)
# Check to see if someone won
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", "CONGRATULATIONS! X 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", "CONGRATULATIONS! X 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", "CONGRATULATIONS! X 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", "CONGRATULATIONS! X 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", "CONGRATULATIONS! X 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", "CONGRATULATIONS! X 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", "CONGRATULATIONS! X 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", "CONGRATULATIONS! X Wins!!")
disable_all_buttons()
#### CHECK FOR O's Win
elif b1["text"] == "O" and b2["text"] == "O" and b3["text"] == "O":
b1.config(bg="red")
b2.config(bg="red")
b3.config(bg="red")
winner = True
messagebox.showinfo("Tic Tac Toe", "CONGRATULATIONS! O Wins!!")
disable_all_buttons()
elif b4["text"] == "O" and b5["text"] == "O" and b6["text"] == "O":
b4.config(bg="red")
b5.config(bg="red")
b6.config(bg="red")
winner = True
messagebox.showinfo("Tic Tac Toe", "CONGRATULATIONS! O Wins!!")
disable_all_buttons()
elif b7["text"] == "O" and b8["text"] == "O" and b9["text"] == "O":
b7.config(bg="red")
b8.config(bg="red")
b9.config(bg="red")
winner = True
messagebox.showinfo("Tic Tac Toe", "CONGRATULATIONS! O Wins!!")
disable_all_buttons()
elif b1["text"] == "O" and b4["text"] == "O" and b7["text"] == "O":
b1.config(bg="red")
b4.config(bg="red")
b7.config(bg="red")
winner = True
messagebox.showinfo("Tic Tac Toe", "CONGRATULATIONS! O Wins!!")
disable_all_buttons()
elif b2["text"] == "O" and b5["text"] == "O" and b8["text"] == "O":
b2.config(bg="red")
b5.config(bg="red")
b8.config(bg="red")
winner = True
messagebox.showinfo("Tic Tac Toe", "CONGRATULATIONS! O Wins!!")
disable_all_buttons()
elif b3["text"] == "O" and b6["text"] == "O" and b9["text"] == "O":
b3.config(bg="red")
b6.config(bg="red")
b9.config(bg="red")
winner = True
messagebox.showinfo("Tic Tac Toe", "CONGRATULATIONS! O Wins!!")
disable_all_buttons()
elif b1["text"] == "O" and b5["text"] == "O" and b9["text"] == "O":
b1.config(bg="red")
b5.config(bg="red")
b9.config(bg="red")
winner = True
messagebox.showinfo("Tic Tac Toe", "CONGRATULATIONS! O Wins!!")
disable_all_buttons()
elif b3["text"] == "O" and b5["text"] == "O" and b7["text"] == "O":
b3.config(bg="red")
b5.config(bg="red")
b7.config(bg="red")
winner = True
messagebox.showinfo("Tic Tac Toe", "CONGRATULATIONS! O Wins!!")
disable_all_buttons()
# Check if tie
if count == 9 and winner == False:
messagebox.showinfo("Tic Tac Toe", "It's A Tie!\n No One Wins!")
disable_all_buttons()
# Button clicked function
def b_click(b):
global clicked, count
if b["text"] == " " and clicked == True:
b["text"] = "a,b\nc,d"
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", "Hey! That box has already been selected\nPick Another Box..." )
# Start the game over!
def reset():
global b1, b2, b3, b4, b5, b6, b7, b8, b9
global clicked, count
clicked = True
count = 0
# Build our buttons
b1 = Button(root, text=" ", font=("Helvetica", 20), height=3, width=6, bg="#156454", command=lambda: b_click(b1))
b2 = Button(root, text=" ", font=("Helvetica", 20), height=3, width=6, bg="#468340", command=lambda: b_click(b2))
b3 = Button(root, text=" ", font=("Helvetica", 20), height=3, width=6, bg="#8FA450", command=lambda: b_click(b3))
b4 = Button(root, text=" ", font=("Helvetica", 20), height=3, width=6, bg="#B1A440", command=lambda: b_click(b4))
b5 = Button(root, text=" ", font=("Helvetica", 20), height=3, width=6, bg="#1E8F78", command=lambda: b_click(b5))
b6 = Button(root, text=" ", font=("Helvetica", 20), height=3, width=6, bg="#5CAD55", command=lambda: b_click(b6))
b7 = Button(root, text=" ", font=("Helvetica", 20), height=3, width=6, bg="#B4CF66", command=lambda: b_click(b7))
b8 = Button(root, text=" ", font=("Helvetica", 20), height=3, width=6, bg="#DBCB4F", command=lambda: b_click(b8))
b9 = Button(root, text=" ", font=("Helvetica", 20), height=3, width=6, bg="#95A3B3", command=lambda: b_click(b9))
# Grid our buttons to the screen
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)
# Create menue
my_menu = Menu(root)
root.config(menu=my_menu)
# Create Options Menu
options_menu = Menu(my_menu, tearoff=False)
my_menu.add_cascade(label="Options", menu=options_menu)
options_menu.add_command(label="Rest Game", command=reset)
reset()
root.mainloop()