-
Notifications
You must be signed in to change notification settings - Fork 0
/
TicTacToe.py
266 lines (220 loc) · 11.3 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
import tkinter.messagebox
from tkinter import*
root = Tk()
root.geometry("1350x750+0+0")
root.title("Tic Tac Toe")
root.configure(background='Blue')
Tops = Frame(root, bg='Blue', pady=2, width=1350, height=100, relief=RIDGE)
Tops.grid(row=0, column=0)
lblTitle = Label(Tops, font=('arial', 50, 'bold'), text="Tic Tac Toe by Jamil", bd=21, bg='Blue', fg='Sky Blue', justify=CENTER)
lblTitle.grid(row=0, column=0)
MainFrame = Frame(root, bg ='Blue', padx=40, pady=2, width=1350, height=100, relief=RIDGE)
MainFrame.grid(row=1, column=0)
LeftFrame = Frame(MainFrame, bd=10, width=750, height=500, pady=0, padx=0, bg="Sky Blue", relief=RIDGE)
LeftFrame.pack(side=LEFT)
RightFrame = Frame(MainFrame, bd=10, width=560, height=500, padx=0, pady=0, bg="Sky Blue", relief=RIDGE)
RightFrame.pack(side=RIGHT)
RightFrame1 = Frame(RightFrame, bd=10, width=560, height=200, padx=0, pady=0, bg="Sky Blue", relief=RIDGE)
RightFrame1.grid(row=0, column=0)
RightFrame2 = Frame(RightFrame, bd=10, width=560, height=200, padx=0, pady=0, bg="Sky Blue", relief=RIDGE)
RightFrame2.grid(row=1, column=0)
PlayerX=IntVar()
PlayerO=IntVar()
PlayerX.set(0)
PlayerO.set(0)
#============================ GAME FUNCTIONS ========================================================================
button = StringVar()
click = True
def checker(buttons):
global click
if buttons["text"] == " " and click == True:
buttons["text"] = "X"
click = False
scorekeeper()
elif buttons["text"] == " " and click == False:
buttons["text"] = "O"
click = True
scorekeeper()
def scorekeeper():
if(button1["text"]=="X" and button2["text"]=="X" and button3["text"]=="X"):
button1.configure(background="Sky Blue")
button2.configure(background="Sky Blue")
button3.configure(background="Sky Blue")
n = float(PlayerX.get())
score = (n + 1)
PlayerX.set(score)
tkinter.messagebox.showinfo("Winner X", "You are the winner!")
if (button4["text"]=="X" and button5["text"]=="X" and button6["text"]=="X"):
button4.configure(background ="Red")
button5.configure(background ="Red")
button6.configure(background ="Red")
n = float(PlayerX.get())
score = (n + 1)
PlayerX.set(score)
tkinter.messagebox.showinfo("Winner X", "You are the winner!")
if (button7["text"] == "X" and button8["text"] == "X" and button9["text"] == "X"):
button7.configure(background="green")
button8.configure(background="green")
button9.configure(background="green")
n = float(PlayerX.get())
score = (n + 1)
PlayerX.set(score)
tkinter.messagebox.showinfo("Winner X", "You are the winner!")
if (button3["text"] == "X" and button5["text"] == "X" and button7["text"] == "X"):
button3.configure(background="gold")
button5.configure(background="gold")
button7.configure(background="gold")
n = float(PlayerX.get())
score = (n + 1)
PlayerX.set(score)
tkinter.messagebox.showinfo("Winner X", "You are the winner!")
if (button1["text"] == "X" and button5["text"] == "X" and button9["text"] == "X"):
button1.configure(background="orange")
button5.configure(background="orange")
button9.configure(background="orange")
n = float(PlayerX.get())
score = (n + 1)
PlayerX.set(score)
tkinter.messagebox.showinfo("Winner X", "You are the winner!")
if (button1["text"] == "X" and button4["text"] == "X" and button7["text"] == "X"):
button1.configure(background="purple")
button4.configure(background="purple")
button7.configure(background="purple")
n = float(PlayerX.get())
score = (n + 1)
PlayerX.set(score)
tkinter.messagebox.showinfo("Winner X", "You are the winner!")
if (button2["text"] == "X" and button5["text"] == "X" and button8["text"] == "X"):
button2.configure(background="pink")
button5.configure(background="pink")
button8.configure(background="pink")
n = float(PlayerX.get())
score = (n + 1)
PlayerX.set(score)
tkinter.messagebox.showinfo("Winner X", "You are the winner!")
if (button3["text"] == "X" and button6["text"] == "X" and button9["text"] == "X"):
button3.configure(background="cadet blue")
button6.configure(background="cadet blue")
button9.configure(background="cadet blue")
n = float(PlayerX.get())
score = (n + 1)
PlayerX.set(score)
tkinter.messagebox.showinfo("Winner X", "You are the winner!")
#===================== O's =============================
if(button1["text"] == "O" and button2["text"] == "O" and button3["text"] == "O"):
button1.configure(background="powder blue")
button2.configure(background="powder blue")
button3.configure(background="powder blue")
n = float(PlayerO.get())
score = (n + 1)
PlayerO.set(score)
tkinter.messagebox.showinfo("Winner O", "You are the winner!")
if (button4["text"] =="O" and button5["text"] == "O" and button6["text"] == "O"):
button4.configure(background="Red")
button5.configure(background="Red")
button6.configure(background="Red")
n = float(PlayerO.get())
score = (n + 1)
PlayerO.set(score)
tkinter.messagebox.showinfo("Winner O", "You are the winner!")
if (button7["text"] == "O" and button8["text"] == "O" and button9["text"] == "O"):
button7.configure(background="green")
button8.configure(background="green")
button9.configure(background="green")
n = float(PlayerO.get())
score = (n + 1)
PlayerO.set(score)
tkinter.messagebox.showinfo("Winner O", "You are the winner!")
if (button3["text"] == "O" and button5["text"] == "O" and button7["text"] == "O"):
button3.configure(background="gold")
button5.configure(background="gold")
button7.configure(background="gold")
n = float(PlayerO.get())
score = (n + 1)
PlayerO.set(score)
tkinter.messagebox.showinfo("Winner O", "You are the winner!")
if (button1["text"] == "O" and button5["text"] == "O" and button9["text"] == "O"):
button1.configure(background="orange")
button5.configure(background="orange")
button9.configure(background="orange")
n = float(PlayerO.get())
score = (n + 1)
PlayerO.set(score)
tkinter.messagebox.showinfo("Winner O", "You are the winner!")
if (button1["text"] == "O" and button4["text"] == "O" and button7["text"] == "O"):
button1.configure(background="purple")
button4.configure(background="purple")
button7.configure(background="purple")
n = float(PlayerO.get())
score = (n + 1)
PlayerO.set(score)
tkinter.messagebox.showinfo("Winner O", "You are the winner!")
if (button2["text"] == "O" and button5["text"] == "O" and button8["text"] == "O"):
button2.configure(background="pink")
button5.configure(background="pink")
button8.configure(background="pink")
n = float(PlayerO.get())
score = (n + 1)
PlayerO.set(score)
tkinter.messagebox.showinfo("Winner O", "You are the winner!")
if (button3["text"] == "O" and button6["text"] == "O" and button9["text"] == "O"):
button3.configure(background="cadet blue")
button6.configure(background="cadet blue")
button9.configure(background="cadet blue")
n = float(PlayerO.get())
score = (n + 1)
PlayerO.set(score)
tkinter.messagebox.showinfo("Winner O", "You are the winner!")
def NewGame():
button1['text'] = " "
button2['text'] = " "
button3['text'] = " "
button4['text'] = " "
button5['text'] = " "
button6['text'] = " "
button7['text'] = " "
button8['text'] = " "
button9['text'] = " "
button1.configure(background="gainsboro")
button2.configure(background="gainsboro")
button3.configure(background="gainsboro")
button4.configure(background="gainsboro")
button5.configure(background="gainsboro")
button6.configure(background="gainsboro")
button7.configure(background="gainsboro")
button8.configure(background="gainsboro")
button9.configure(background="gainsboro")
def reset():
PlayerX.set(0)
PlayerO.set(0)
#======================== RIGHT FRAME =========================================================================
lblPlayerX = Label(RightFrame1, font=('arial', 40, 'bold'), text="Player X :", padx=2, pady=2, bg="Cadet Blue")
lblPlayerX.grid(row=0, column=0, sticky=W)
txtPlayerX=Entry(RightFrame1, font=('arial', 40, 'bold'), bd=2, fg="black", textvariable=PlayerX, width=14, justify=LEFT).grid(row=0, column=1)
lblPlayerO = Label(RightFrame1, font=('arial', 40, 'bold'), text="Player O :", padx=2, pady=2, bg="Cadet Blue")
lblPlayerO.grid(row=1, column=0, sticky=W)
txtPlayerO=Entry(RightFrame1, font=('arial', 40, 'bold'), bd=2, fg="black", textvariable=PlayerO, width=14, justify=LEFT).grid(row=1, column=1)
btnReset = Button(RightFrame2, text="Reset", font=('Times 26 bold'), height=3, width=10, bg='gainsboro', command=reset)
btnReset.grid(row=0, column=0)
btnNewGame = Button(RightFrame2, text="New Game", font=('Times 26 bold'), height=3, width=10, bg='gainsboro', command=NewGame)
btnNewGame.grid(row=0, column=1)
#======================== LEFT FRAME =========================================================================
button1 = Button(LeftFrame, text=" ", font=('Times 26 bold'), height=3, width=8, bg='gainsboro', command=lambda:checker(button1))
button1.grid(row=1, column=0, sticky = S+N+E+W)
button2 = Button(LeftFrame, text=" ", font=('Times 26 bold'), height=3, width=8, bg='gainsboro', command=lambda:checker(button2))
button2.grid(row=1, column=1, sticky = S+N+E+W)
button3 = Button(LeftFrame, text=" ", font=('Times 26 bold'), height=3, width=8, bg='gainsboro', command=lambda:checker(button3))
button3.grid(row=1, column=2, sticky = S+N+E+W)
button4 = Button(LeftFrame, text=" ", font=('Times 26 bold'), height=3, width=8, bg='gainsboro', command=lambda:checker(button4))
button4.grid(row=2, column=0, sticky = S+N+E+W)
button5 = Button(LeftFrame, text=" ", font=('Times 26 bold'), height=3, width=8, bg='gainsboro', command=lambda:checker(button5))
button5.grid(row=2, column=1, sticky = S+N+E+W)
button6 = Button(LeftFrame, text=" ", font=('Times 26 bold'), height=3, width=8, bg='gainsboro', command=lambda:checker(button6))
button6.grid(row=2, column=2, sticky = S+N+E+W)
button7 = Button(LeftFrame, text=" ", font=('Times 26 bold'), height=3, width=8, bg='gainsboro', command=lambda:checker(button7))
button7.grid(row=3, column=0, sticky = S+N+E+W)
button8 = Button(LeftFrame, text=" ", font=('Times 26 bold'), height=3, width=8, bg='gainsboro', command=lambda:checker(button8))
button8.grid(row=3, column=1, sticky = S+N+E+W)
button9 = Button(LeftFrame, text=" ", font=('Times 26 bold'), height=3, width=8, bg='gainsboro', command=lambda:checker(button9))
button9.grid(row=3, column=2, sticky = S+N+E+W)
root.mainloop()