-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtic-tac-toe.py
183 lines (150 loc) · 5.33 KB
/
tic-tac-toe.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
# Tic Tac Toe game using tkinter
# Importing modules
from tkinter import *
import tkinter.messagebox
# Window defined
root = Tk()
root.iconbitmap('tic-tac-toe.ico')
root.title('Tic-Tac-Toe')
root.resizable(False, False)
click = True
# Count variable to check the no. of turns
count = 0
btn1 = StringVar()
btn2 = StringVar()
btn3 = StringVar()
btn4 = StringVar()
btn5 = StringVar()
btn6 = StringVar()
btn7 = StringVar()
btn8 = StringVar()
btn9 = StringVar()
xPhoto = PhotoImage(file='cross.png')
oPhoto = PhotoImage(file='happy.png')
# Grid buttons
def start():
button1 = Button(root, height=9, width=19, bd=.5, relief='sunken', bg='#ccfff7', textvariable=btn1,
command=lambda: press(1, 0, 0))
button1.grid(row=0, column=0)
button2 = Button(root, height=9, width=19, bd=.5, relief='sunken', bg='#ccfff7', textvariable=btn2,
command=lambda: press(2, 0, 1))
button2.grid(row=0, column=1)
button3 = Button(root, height=9, width=19, bd=.5, relief='sunken', bg='#ccfff7', textvariable=btn3,
command=lambda: press(3, 0, 2))
button3.grid(row=0, column=2)
button4 = Button(root, height=9, width=19, bd=.5, relief='sunken', bg='#99ffee', textvariable=btn4,
command=lambda: press(4, 1, 0))
button4.grid(row=1, column=0)
button5 = Button(root, height=9, width=19, bd=.5, relief='sunken', bg='#99ffee', textvariable=btn5,
command=lambda: press(5, 1, 1))
button5.grid(row=1, column=1)
button6 = Button(root, height=9, width=19, bd=.5, relief='sunken', bg='#99ffee', textvariable=btn6,
command=lambda: press(6, 1, 2))
button6.grid(row=1, column=2)
button7 = Button(root, height=9, width=19, bd=.5, relief='sunken', bg='#66ffe6', textvariable=btn7,
command=lambda: press(7, 2, 0))
button7.grid(row=2, column=0)
button8 = Button(root, height=9, width=19, bd=.5, relief='sunken', bg='#66ffe6', textvariable=btn8,
command=lambda: press(8, 2, 1))
button8.grid(row=2, column=1)
button9 = Button(root, height=9, width=19, bd=.5, relief='sunken', bg='#66ffe6', textvariable=btn9,
command=lambda: press(9, 2, 2))
button9.grid(row=2, column=2)
# Changing the value of button
def press(num, r, c):
global click, count
if click == True:
labelPhoto = Label(root, image=xPhoto)
labelPhoto.grid(row=r, column=c)
if num == 1:
btn1.set('X')
elif num == 2:
btn2.set('X')
elif num == 3:
btn3.set('X')
elif num == 4:
btn4.set('X')
elif num == 5:
btn5.set('X')
elif num == 6:
btn6.set('X')
elif num == 7:
btn7.set('X')
elif num == 8:
btn8.set('X')
else:
btn9.set('X')
count += 1
click = False
checkWin()
else:
labelPhoto = Label(root, image=oPhoto)
labelPhoto.grid(row=r, column=c)
if num == 1:
btn1.set('O')
elif num == 2:
btn2.set('O')
elif num == 3:
btn3.set('O')
elif num == 4:
btn4.set('O')
elif num == 5:
btn5.set('O')
elif num == 6:
btn6.set('O')
elif num == 7:
btn7.set('O')
elif num == 8:
btn8.set('O')
else:
btn9.set('O')
count += 1
click = True
checkWin()
# Checks the winner
def checkWin():
global count, click
if (btn1.get() == 'X' and btn2.get() == 'X' and btn3.get() == 'X' or
btn4.get() == 'X' and btn5.get() == 'X' and btn6.get() == 'X' or
btn7.get() == 'X' and btn8.get() == 'X' and btn9.get() == 'X' or
btn1.get() == 'X' and btn4.get() == 'X' and btn7.get() == 'X' or
btn2.get() == 'X' and btn5.get() == 'X' and btn8.get() == 'X' or
btn3.get() == 'X' and btn6.get() == 'X' and btn9.get() == 'X' or
btn1.get() == 'X' and btn5.get() == 'X' and btn9.get() == 'X' or
btn3.get() == 'X' and btn5.get() == 'X' and btn7.get() == 'X'):
tkinter.messagebox.showinfo("Tic-Tac-Toe", 'X Wins !')
click = True
count = 0
clear()
start()
elif (btn1.get() == 'O' and btn2.get() == 'O' and btn3.get() == 'O' or
btn4.get() == 'O' and btn5.get() == 'O' and btn6.get() == 'O' or
btn7.get() == 'O' and btn8.get() == 'O' and btn9.get() == 'O' or
btn1.get() == 'O' and btn4.get() == 'O' and btn7.get() == 'O' or
btn2.get() == 'O' and btn5.get() == 'O' and btn8.get() == 'O' or
btn3.get() == 'O' and btn6.get() == 'O' and btn9.get() == 'O' or
btn1.get() == 'O' and btn5.get() == 'O' and btn9.get() == 'O' or
btn3.get() == 'O' and btn5.get() == 'O' and btn7.get() == 'O'):
tkinter.messagebox.showinfo("Tic-Tac-Toe", 'O Wins !')
count = 0
clear()
start()
elif (count == 9):
tkinter.messagebox.showinfo("Tic-Tac-Toe", 'Tie Game!')
click = True
count = 0
clear()
start()
# Clear the tiles
def clear():
btn1.set('')
btn2.set('')
btn3.set('')
btn4.set('')
btn5.set('')
btn6.set('')
btn7.set('')
btn8.set('')
btn9.set('')
start()
root.mainloop()