-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTic Tac Toe (Human vs Human).py
331 lines (309 loc) · 10.9 KB
/
Tic Tac Toe (Human vs Human).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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#Tic Tac Toe Game Using Turtle Module
#By Kaustubh Kulkarni
import turtle as t
#Window
wn = t.Screen()
wn.title("Tic Tac Toe")
wn.bgcolor("#14BDAC")
wn.setup(width=500, height=500)
#Game variable
gamevar = "×"
#Winning variable
play=True
#Cell Variables
grid=[[0,0,0],[0,0,0],[0,0,0]]
#Grid Pen: This turtle gives the title and raws the grid.
#The Title: Tic tac Toe
grid_pen = t.Turtle()
grid_pen.ht()
grid_pen.penup()
grid_pen.speed(0)
grid_pen.goto(0,190)
grid_pen.color("#FFFFFF")
grid_pen.write("Tac ", align="center", font=("Sans serif", 30, "normal"))
grid_pen.color("#545454")
grid_pen.write("Tic Toe", align="center", font=("Sans serif", 30, "normal"))
#Grid drawing
grid_pen.ht()
grid_pen.color("#0DA192")
grid_pen.pensize(10)
grid_pen.speed(0)
grid_pen.penup()
grid_pen.goto(-55,-220)
grid_pen.pendown()
grid_pen.lt(90)
grid_pen.fd(320)
grid_pen.penup()
grid_pen.goto(55,100)
grid_pen.rt(180)
grid_pen.pendown()
grid_pen.fd(320)
grid_pen.penup()
grid_pen.goto(160,-115)
grid_pen.rt(90)
grid_pen.pendown()
grid_pen.fd(320)
grid_pen.penup()
grid_pen.goto(-160,-5)
grid_pen.rt(180)
grid_pen.pendown()
grid_pen.fd(320)
#Status Turtle
status = t.Turtle()
status.ht()
status.penup()
status.color("#545454")
status.speed(0)
status.goto(-160,131)
status.write(gamevar, align="left", font=("Sans serif", 30, "normal"))
status.goto(-160,140)
status.write(" Turn", align="left", font=("Sans serif", 20, "normal"))
#Reset Turtle
reset = t.Turtle()
reset.ht()
reset.penup()
reset.color("#545454")
reset.speed(0)
reset.goto(160,140)
reset.write("Reset", align="right", font=("Sans serif", 20, "normal"))
#XO Pen
xo_pen = t.Turtle()
xo_pen.ht()
xo_pen.color("#545454")
xo_pen.speed(0)
xo_pen.penup()
def rst(x,y):
global gamevar
global grid
global play
#Winner functions
def x_winner():
global play
play=False
status.clear()
status.goto(-160,131)
status.color("#545454")
status.write("×", align="left", font=("Sans serif", 30, "normal"))
status.goto(-160,140)
status.write(" Won!", align="left", font=("Sans serif", 20, "normal"))
def o_winner():
global play
play=False
status.clear()
status.goto(-160,131)
status.color("#FFFFFF")
status.write("○", align="left", font=("Sans serif", 35, "normal"))
status.goto(-160,140)
status.write(" Won!", align="left", font=("Sans serif", 20, "normal"))
def game_draw():
status.clear()
status.color("#545454")
status.goto(-160,140)
status.write("Draw!", align="left", font=("Sans serif", 20, "normal"))
def draw_xo():
global gamevar
if gamevar=="×" and play==True:
xo_pen.color("#545454")
xo_pen.penup()
xo_pen.write(gamevar, align="center", font=("Sans serif", 100, "normal"))
gamevar="○"
status.goto(-160,131)
status.clear()
status.color("#FFFFFF")
status.write(gamevar, align="left", font=("Sans serif", 35, "normal"))
status.goto(-160,140)
status.write(" Turn", align="left", font=("Sans serif", 20, "normal"))
#X Winner
if grid[0][0]=="×" and grid[0][1]=="×" and grid[0][2]=="×":
x_winner()
xo_pen.goto(-160,50)
xo_pen.color("#545454")
xo_pen.pensize(10)
xo_pen.pendown()
xo_pen.goto(160,50)
xo_pen.penup()
if grid[1][0]=="×" and grid[1][1]=="×" and grid[1][2]=="×":
x_winner()
xo_pen.goto(-160,-60)
xo_pen.color("#545454")
xo_pen.pensize(10)
xo_pen.pendown()
xo_pen.goto(160,-60)
xo_pen.penup()
if grid[2][0]=="×" and grid[2][1]=="×" and grid[2][2]=="×":
x_winner()
xo_pen.goto(-160,-170)
xo_pen.color("#545454")
xo_pen.pensize(10)
xo_pen.pendown()
xo_pen.goto(160,-170)
xo_pen.penup()
if grid[0][0]=="×" and grid[1][0]=="×" and grid[2][0]=="×":
x_winner()
xo_pen.goto(-110,100)
xo_pen.color("#545454")
xo_pen.pensize(10)
xo_pen.pendown()
xo_pen.goto(-110,-220)
xo_pen.penup()
if grid[0][1]=="×" and grid[1][1]=="×" and grid[2][1]=="×":
x_winner()
xo_pen.goto(0,100)
xo_pen.color("#545454")
xo_pen.pensize(10)
xo_pen.pendown()
xo_pen.goto(0,-220)
xo_pen.penup()
if grid[0][2]=="×" and grid[1][2]=="×" and grid[2][2]=="×":
x_winner()
xo_pen.goto(110,100)
xo_pen.color("#545454")
xo_pen.pensize(10)
xo_pen.pendown()
xo_pen.goto(110,-220)
xo_pen.penup()
if grid[0][0]=="×" and grid[1][1]=="×" and grid[2][2]=="×":
x_winner()
xo_pen.goto(-160,100)
xo_pen.color("#545454")
xo_pen.pensize(10)
xo_pen.pendown()
xo_pen.goto(160,-220)
xo_pen.penup()
if grid[2][0]=="×" and grid[1][1]=="×" and grid[0][2]=="×":
x_winner()
xo_pen.goto(160,100)
xo_pen.color("#545454")
xo_pen.pensize(10)
xo_pen.pendown()
xo_pen.goto(-160,-220)
xo_pen.penup()
elif gamevar=="○" and play==True:
xo_pen.color("#FFFFFF")
xo_pen.penup()
xo_pen.write(gamevar, align="center", font=("Sans serif", 110, "bold"))
gamevar="×"
status.goto(-160,131)
status.clear()
status.color("#545454")
status.write(gamevar, align="left", font=("Sans serif", 30, "normal"))
status.goto(-160,140)
status.write(" Turn", align="left", font=("Sans serif", 20, "normal"))
#O Winner
if grid[0][0]=="○" and grid[0][1]=="○" and grid[0][2]=="○":
o_winner()
xo_pen.goto(-160,50)
xo_pen.color("#FFFFFF")
xo_pen.pensize(10)
xo_pen.pendown()
xo_pen.goto(160,50)
xo_pen.penup()
if grid[1][0]=="○" and grid[1][1]=="○" and grid[1][2]=="○":
o_winner()
xo_pen.goto(-160,-60)
xo_pen.color("#FFFFFF")
xo_pen.pensize(10)
xo_pen.pendown()
xo_pen.goto(160,-60)
xo_pen.penup()
if grid[2][0]=="○" and grid[2][1]=="○" and grid[2][2]=="○":
o_winner()
xo_pen.goto(-160,-170)
xo_pen.color("#FFFFFF")
xo_pen.pensize(10)
xo_pen.pendown()
xo_pen.goto(160,-170)
xo_pen.penup()
if grid[0][0]=="○" and grid[1][0]=="○" and grid[2][0]=="○":
o_winner()
xo_pen.goto(-110,100)
xo_pen.color("#FFFFFF")
xo_pen.pensize(10)
xo_pen.pendown()
xo_pen.goto(-110,-220)
xo_pen.penup()
if grid[0][1]=="○" and grid[1][1]=="○" and grid[2][1]=="○":
o_winner()
x_winner()
xo_pen.goto(0,100)
xo_pen.color("#FFFFFF")
xo_pen.pensize(10)
xo_pen.pendown()
xo_pen.goto(0,-220)
if grid[0][2]=="○" and grid[1][2]=="○" and grid[2][2]=="○":
o_winner()
xo_pen.goto(110,100)
xo_pen.color("#FFFFFF")
xo_pen.pensize(10)
xo_pen.pendown()
xo_pen.goto(110,-220)
xo_pen.penup()
if grid[0][0]=="○" and grid[1][1]=="○" and grid[2][2]=="○":
o_winner()
xo_pen.goto(-160,100)
xo_pen.color("#FFFFFF")
xo_pen.pensize(10)
xo_pen.pendown()
xo_pen.goto(160,-220)
xo_pen.penup()
if grid[2][0]=="○" and grid[1][1]=="○" and grid[0][2]=="○":
o_winner()
xo_pen.goto(160,100)
xo_pen.color("#FFFFFF")
xo_pen.pensize(10)
xo_pen.pendown()
xo_pen.goto(-160,-220)
xo_pen.penup()
if grid[0][0]!=0 and grid[0][1]!=0 and grid[0][2]!=0 and grid[1][0]!=0 and grid[1][1]!=0 and grid[1][2]!=0 and grid[2][0]!=0 and grid[2][1]!=0 and grid[2][2]!=0 and play==True:
game_draw()
if x>80 and x<164 and y>140 and y<175:
xo_pen.clear()
grid=[[0,0,0],[0,0,0],[0,0,0]]
gamevar = "×"
play=True
status.clear()
status.color("#545454")
status.goto(-160,131)
status.write(gamevar, align="left", font=("Sans serif", 30, "normal"))
status.goto(-160,140)
status.write(" Turn", align="left", font=("Sans serif", 20, "normal"))
if x>-160 and x<-60 and y>0 and y<100 and grid[0][0]==0:
xo_pen.goto(-110,-25)#50
grid[0][0]=gamevar
draw_xo()
if x>-50 and x<50 and y>0 and y<100 and grid[0][1]==0:
xo_pen.goto(0,-25)
grid[0][1]=gamevar
draw_xo()
if x>60 and x<160 and y>0 and y<100 and grid[0][2]==0:
xo_pen.goto(110,-25)
grid[0][2]=gamevar
draw_xo()
if x>-160 and x<-60 and y>-110 and y<-10 and grid[1][0]==0:
xo_pen.goto(-110,-135)
grid[1][0]=gamevar
draw_xo()
if x>-50 and x<50 and y>-110 and y<-10 and grid[1][1]==0:
xo_pen.goto(0,-135)
grid[1][1]=gamevar
draw_xo()
if x>60 and x<160 and y>-110 and y<-10 and grid[1][2]==0:
xo_pen.goto(110,-135)
grid[1][2]=gamevar
draw_xo()
if x>-160 and x<-60 and y>-220 and y<-120 and grid[2][0]==0:
xo_pen.goto(-110,-245)
grid[2][0]=gamevar
draw_xo()
if x>-50 and x<50 and y>-220 and y<-120 and grid[2][1]==0:
xo_pen.goto(0,-245)
grid[2][1]=gamevar
draw_xo()
if x>60 and x<160 and y>-220 and y<-120 and grid[2][2]==0:
xo_pen.goto(110,-245)
grid[2][2]=gamevar
draw_xo()
#Listening to mouse click
t.onscreenclick(rst)
t.listen()
t.done()