-
Notifications
You must be signed in to change notification settings - Fork 48
/
Tic_Tac_Toe_Game.py
208 lines (187 loc) · 5.95 KB
/
Tic_Tac_Toe_Game.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
from tkinter import *
from tkinter import ttk
import tkinter.messagebox
root=Tk()
root.title("Tic Tac Toe")
#add Buttons
bu1=ttk.Button(root,text=' ')
bu1.grid(row=0,column=0,sticky='snew',ipadx=40,ipady=40)
bu1.config(command=lambda: ButtonClick(1))
bu2=ttk.Button(root,text=' ')
bu2.grid(row=0,column=1,sticky='snew',ipadx=40,ipady=40)
bu2.config(command=lambda: ButtonClick(2))
bu3=ttk.Button(root,text=' ')
bu3.grid(row=0,column=2,sticky='snew',ipadx=40,ipady=40)
bu3.config(command=lambda: ButtonClick(3))
bu4=ttk.Button(root,text=' ')
bu4.grid(row=1,column=0,sticky='snew',ipadx=40,ipady=40)
bu4.config(command=lambda: ButtonClick(4))
bu5=ttk.Button(root,text=' ')
bu5.grid(row=1,column=1,sticky='snew',ipadx=40,ipady=40)
bu5.config(command=lambda: ButtonClick(5))
bu6=ttk.Button(root,text=' ')
bu6.grid(row=1,column=2,sticky='snew',ipadx=40,ipady=40)
bu6.config(command=lambda: ButtonClick(6))
bu7=ttk.Button(root,text=' ')
bu7.grid(row=2,column=0,sticky='snew',ipadx=40,ipady=40)
bu7.config(command=lambda: ButtonClick(7))
bu8=ttk.Button(root,text=' ')
bu8.grid(row=2,column=1,sticky='snew',ipadx=40,ipady=40)
bu8.config(command=lambda: ButtonClick(8))
bu9=ttk.Button(root,text=' ')
bu9.grid(row=2,column=2,sticky='snew',ipadx=40,ipady=40)
bu9.config(command=lambda: ButtonClick(9))
playerturn=ttk.Label(root,text=" Player 1 turn! ")
playerturn.grid(row=3,column=0,sticky='snew',ipadx=40,ipady=40)
playerdetails=ttk.Label(root,text=" Player 1 is X\n\n Player 2 is O")
playerdetails.grid(row=3,column=2,sticky='snew',ipadx=40,ipady=40)
res=ttk.Button(root,text='Restart')
res.grid(row=3,column=1,sticky='snew',ipadx=40,ipady=40)
res.config(command=lambda: restartbutton())
a=1
b=0
c=0
def restartbutton():
global a,b,c
a=1
b=0
c=0
playerturn['text']=" Player 1 turn! "
bu1['text']=' '
bu2['text']=' '
bu3['text']=' '
bu4['text']=' '
bu5['text']=' '
bu6['text']=' '
bu7['text']=' '
bu8['text']=' '
bu9['text']=' '
bu1.state(['!disabled'])
bu2.state(['!disabled'])
bu3.state(['!disabled'])
bu4.state(['!disabled'])
bu5.state(['!disabled'])
bu6.state(['!disabled'])
bu7.state(['!disabled'])
bu8.state(['!disabled'])
bu9.state(['!disabled'])
#after getting result(win or loss or draw) disable button
def disableButton():
bu1.state(['disabled'])
bu2.state(['disabled'])
bu3.state(['disabled'])
bu4.state(['disabled'])
bu5.state(['disabled'])
bu6.state(['disabled'])
bu7.state(['disabled'])
bu8.state(['disabled'])
bu9.state(['disabled'])
def ButtonClick(id):
global a,b,c
print("ID:{}".format(id))
#for player 1 turn
if id==1 and bu1['text']==' ' and a==1:
bu1['text']="X"
a=0
b+=1
if id==2 and bu2['text']==' ' and a==1:
bu2['text']="X"
a=0
b+=1
if id==3 and bu3['text']==' ' and a==1:
bu3['text']="X"
a=0
b+=1
if id==4 and bu4['text']==' ' and a==1:
bu4['text']="X"
a=0
b+=1
if id==5 and bu5['text']==' ' and a==1:
bu5['text']="X"
a=0
b+=1
if id==6 and bu6['text']==' ' and a==1:
bu6['text']="X"
a=0
b+=1
if id==7 and bu7['text']==' ' and a==1:
bu7['text']="X"
a=0
b+=1
if id==8 and bu8['text']==' ' and a==1:
bu8['text']="X"
a=0
b+=1
if id==9 and bu9['text']==' ' and a==1:
bu9['text']="X"
a=0
b+=1
#for player 2 turn
if id==1 and bu1['text']==' ' and a==0:
bu1['text']="O"
a=1
b+=1
if id==2 and bu2['text']==' ' and a==0:
bu2['text']="O"
a=1
b+=1
if id==3 and bu3['text']==' ' and a==0:
bu3['text']="O"
a=1
b+=1
if id==4 and bu4['text']==' ' and a==0:
bu4['text']="O"
a=1
b+=1
if id==5 and bu5['text']==' ' and a==0:
bu5['text']="O"
a=1
b+=1
if id==6 and bu6['text']==' ' and a==0:
bu6['text']="O"
a=1
b+=1
if id==7 and bu7['text']==' ' and a==0:
bu7['text']="O"
a=1
b+=1
if id==8 and bu8['text']==' ' and a==0:
bu8['text']="O"
a=1
b+=1
if id==9 and bu9['text']==' ' and a==0:
bu9['text']="O"
a=1
b+=1
#checking for winner
if( bu1['text']=='X' and bu2['text']=='X' and bu3['text']=='X' or
bu4['text']=='X' and bu5['text']=='X' and bu6['text']=='X' or
bu7['text']=='X' and bu8['text']=='X' and bu9['text']=='X' or
bu1['text']=='X' and bu4['text']=='X' and bu7['text']=='X' or
bu2['text']=='X' and bu5['text']=='X' and bu8['text']=='X' or
bu3['text']=='X' and bu6['text']=='X' and bu9['text']=='X' or
bu1['text']=='X' and bu5['text']=='X' and bu9['text']=='X' or
bu3['text']=='X' and bu5['text']=='X' and bu7['text']=='X'):
disableButton()
c=1
tkinter.messagebox.showinfo("Tic Tac Toe","Winner is player 1")
elif( bu1['text']=='O' and bu2['text']=='O' and bu3['text']=='O' or
bu4['text']=='O' and bu5['text']=='O' and bu6['text']=='O' or
bu7['text']=='O' and bu8['text']=='O' and bu9['text']=='O' or
bu1['text']=='O' and bu4['text']=='O' and bu7['text']=='O' or
bu2['text']=='O' and bu5['text']=='O' and bu8['text']=='O' or
bu3['text']=='O' and bu6['text']=='O' and bu9['text']=='O' or
bu1['text']=='O' and bu5['text']=='O' and bu9['text']=='O' or
bu3['text']=='O' and bu5['text']=='O' and bu7['text']=='O'):
disableButton()
c=1
tkinter.messagebox.showinfo("Tic Tac Toe","Winner is player 2")
elif b==9:
disableButton()
c=1
tkinter.messagebox.showinfo("Tic Tac Toe","Match is Draw.")
if a==1 and c==0:
playerturn['text']=" Player 1 turn! "
elif a==0 and c==0:
playerturn['text']=" Player 2 turn! "
root.mainloop()