-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtic_tac_toe.py
198 lines (172 loc) · 6.35 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
from tkinter import *
from tkinter import messagebox
screen=Tk()
screen.title('MY TIC TAC TOE')
def disable_all():
b1.config(state = 'disable')
b2.config(state = 'disable')
b3.config(state = 'disable')
b4.config(state = 'disable')
b5.config(state = 'disable')
b6.config(state = 'disable')
b7.config(state = 'disable')
b8.config(state = 'disable')
b9.config(state = 'disable')
def check_win():
global winner
winner = False
if b1['text'] + b2['text'] + b3['text'] == 'XXX':
b1.config(bg = 'red')
b2.config(bg = 'red')
b3.config(bg = 'red')
winner = True
messagebox.showinfo('TIC TAC TOE', 'X WINS')
disable_all()
elif b4['text'] + b5['text'] + b6['text'] == 'XXX':
b4.config(bg = 'red')
b5.config(bg = 'red')
b6.config(bg = 'red')
winner = True
messagebox.showinfo('TIC TAC TOE', 'X WINS')
disable_all()
elif b7['text'] + b8['text'] + b9['text'] == 'XXX':
b7.config(bg = 'red')
b8.config(bg = 'red')
b9.config(bg = 'red')
winner = True
messagebox.showinfo('TIC TAC TOE', 'X WINS')
disable_all()
elif b1['text'] + b4['text'] + b7['text'] == 'XXX':
b1.config(bg = 'red')
b4.config(bg = 'red')
b7.config(bg = 'red')
winner = True
messagebox.showinfo('TIC TAC TOE', 'X WINS')
disable_all()
elif b2['text'] + b5['text'] + b8['text'] == 'XXX':
b2.config(bg = 'red')
b5.config(bg = 'red')
b8.config(bg = 'red')
winner = True
messagebox.showinfo('TIC TAC TOE', 'X WINS')
disable_all()
elif b3['text'] + b6['text'] + b9['text'] == 'XXX':
b3.config(bg = 'red')
b6.config(bg = 'red')
b9.config(bg = 'red')
winner = True
messagebox.showinfo('TIC TAC TOE', 'X WINS')
disable_all()
elif b1['text'] + b5['text'] + b9['text'] == 'XXX':
b1.config(bg = 'red')
b5.config(bg = 'red')
b9.config(bg = 'red')
winner = True
messagebox.showinfo('TIC TAC TOE', 'X WINS')
disable_all()
elif b3['text'] + b5['text'] + b7['text'] == 'XXX':
b3.config(bg = 'red')
b5.config(bg = 'red')
b7.config(bg = 'red')
winner = True
messagebox.showinfo('TIC TAC TOE', 'X WINS')
disable_all()
if b1['text'] + b2['text'] + b3['text'] == 'OOO':
b1.config(bg = 'red')
b2.config(bg = 'red')
b3.config(bg = 'red')
winner = True
messagebox.showinfo('TIC TAC TOE', 'O WINS')
disable_all()
elif b4['text'] + b5['text'] + b6['text'] == 'OOO':
b4.config(bg = 'red')
b5.config(bg = 'red')
b6.config(bg = 'red')
winner = True
messagebox.showinfo('TIC TAC TOE', 'O WINS')
disable_all()
elif b7['text'] + b8['text'] + b9['text'] == 'OOO':
b7.config(bg = 'red')
b8.config(bg = 'red')
b9.config(bg = 'red')
winner = True
messagebox.showinfo('TIC TAC TOE', 'O WINS')
disable_all()
elif b1['text'] + b4['text'] + b7['text'] == 'OOO':
b1.config(bg = 'red')
b4.config(bg = 'red')
b7.config(bg = 'red')
winner = True
messagebox.showinfo('TIC TAC TOE', ' WINS')
disable_all()
elif b2['text'] + b5['text'] + b8['text'] == 'OOO':
b2.config(bg = 'red')
b5.config(bg = 'red')
b8.config(bg = 'red')
winner = True
messagebox.showinfo('TIC TAC TOE', 'O WINS')
disable_all()
elif b3['text'] + b6['text'] + b9['text'] == 'OOO':
b3.config(bg = 'red')
b6.config(bg = 'red')
b9.config(bg = 'red')
winner = True
messagebox.showinfo('TIC TAC TOE', 'O WINS')
disable_all()
elif b1['text'] + b5['text'] + b9['text'] == 'OOO':
b1.config(bg = 'red')
b5.config(bg = 'red')
b9.config(bg = 'red')
winner = True
messagebox.showinfo('TIC TAC TOE', 'O WINS')
disable_all()
elif b3['text'] + b5['text'] + b7['text'] == 'OOO':
b3.config(bg = 'red')
b5.config(bg = 'red')
b7.config(bg = 'red')
winner = True
messagebox.showinfo('TIC TAC TOE', 'O WINS')
disable_all()
if count == 9 and winner == False:
messagebox.showinfo('TIC TAC TOE', 'IT\'S A TIE')
disable_all()
def b_click(b):
global count,clicked
if b['text'] == '' and clicked == True:
b['text'] = 'X'
clicked = False
count = count+1
check_win()
elif b['text'] == '' and clicked == False:
b['text'] = 'O'
clicked = True
count = count+1
check_win()
else:
messagebox.showerror('TIC TAC TOE', 'ALREADY FILLED')
def reset():
global count, clicked
count = 0
clicked = True
global b1,b2,b3,b4,b5,b6,b7,b8,b9
b1=Button(screen, text='',font=('Times new roman' ,20),height=3,width=6,bg='silver',command=lambda: b_click(b1))
b2=Button(screen, text='',font=('Times new roman' ,20),height=3,width=6,bg='silver',command=lambda: b_click(b2))
b3=Button(screen, text='',font=('Times new roman' ,20),height=3,width=6,bg='silver',command=lambda: b_click(b3))
b4=Button(screen, text='',font=('Times new roman' ,20),height=3,width=6,bg='silver',command=lambda: b_click(b4))
b5=Button(screen, text='',font=('Times new roman' ,20),height=3,width=6,bg='silver',command=lambda: b_click(b5))
b6=Button(screen, text='',font=('Times new roman' ,20),height=3,width=6,bg='silver',command=lambda: b_click(b6))
b7=Button(screen, text='',font=('Times new roman' ,20),height=3,width=6,bg='silver',command=lambda: b_click(b7))
b8=Button(screen, text='',font=('Times new roman' ,20),height=3,width=6,bg='silver',command=lambda: b_click(b8))
b9=Button(screen, text='',font=('Times new roman' ,20),height=3,width=6,bg='silver',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)
rese_b = Button(screen, text='RESET',font=('Times new roman' ,12),height=3,width=6,bg='silver',command=reset)
rese_b.grid(row=3, column = 1)
reset()