/
24 - TicTacToe Ultimate.py
331 lines (302 loc) · 9.34 KB
/
24 - TicTacToe Ultimate.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
#Tara Moses
#Assignment 24: Tic-Tac-Toe, Turtle Edition
#May 15, 2013
import turtle,Tkinter,random
screen=turtle.Screen()
screen.colormode(255)
isRunning=False
a=20
b=100
blue=(0,128,255)
pink=(240,132,153)
cursor_pos=(0,0)
grid_pos=0
x_positions=[]
o_positions=[]
one_count=0
two_count=0
cat_count=0
def restartGame():
global x_positions,o_positions
drawBoard()
x_positions=[]
o_positions=[]
def boardIsFull():
spot_count=0
if 0 in x_positions or 0 in o_positions:
spot_count+=1
if 1 in x_positions or 1 in o_positions:
spot_count+=1
if 2 in x_positions or 2 in o_positions:
spot_count+=1
if 3 in x_positions or 3 in o_positions:
spot_count+=1
if 4 in x_positions or 4 in o_positions:
spot_count+=1
if 5 in x_positions or 5 in o_positions:
spot_count+=1
if 6 in x_positions or 6 in o_positions:
spot_count+=1
if 7 in x_positions or 7 in o_positions:
spot_count+=1
if 8 in x_positions or 8 in o_positions:
spot_count+=1
if spot_count==9:
return True
else:
return False
def xWins():
global x_positions
if 0 in x_positions and 1 in x_positions and 2 in x_positions:
return True
elif 3 in x_positions and 4 in x_positions and 5 in x_positions:
return True
elif 6 in x_positions and 7 in x_positions and 8 in x_positions:
return True
elif 0 in x_positions and 3 in x_positions and 6 in x_positions:
return True
elif 1 in x_positions and 4 in x_positions and 7 in x_positions:
return True
elif 2 in x_positions and 5 in x_positions and 8 in x_positions:
return True
elif 0 in x_positions and 4 in x_positions and 8 in x_positions:
return True
elif 6 in x_positions and 4 in x_positions and 2 in x_positions:
return True
else:
return False
def oWins():
global o_positions
if 0 in o_positions and 1 in o_positions and 2 in o_positions:
return True
elif 3 in o_positions and 4 in o_positions and 5 in o_positions:
return True
elif 6 in o_positions and 7 in o_positions and 8 in o_positions:
return True
elif 0 in o_positions and 3 in o_positions and 6 in o_positions:
return True
elif 1 in o_positions and 4 in o_positions and 7 in o_positions:
return True
elif 2 in o_positions and 5 in o_positions and 8 in o_positions:
return True
elif 0 in o_positions and 4 in o_positions and 8 in o_positions:
return True
elif 6 in o_positions and 4 in o_positions and 2 in o_positions:
return True
else:
return False
def catWins():
if boardIsFull() and xWins()==False and oWins()==False:
return True
else:
return False
def drawBoard():
global b
#actually draw the board :D
turtle.ht()
turtle.width(5)
turtle.up()
turtle.goto(-3*b/2.0,b/2.0)
turtle.down()
turtle.seth(0)
turtle.forward(3*b)
turtle.up()
turtle.goto(-3*b/2.0,-b/2.0)
turtle.down()
turtle.seth(0)
turtle.forward(3*b)
turtle.up()
turtle.goto(-b/2.0,3*b/2.0)
turtle.down()
turtle.seth(270)
turtle.forward(3*b)
turtle.up()
turtle.goto(b/2.0,3*b/2.0)
turtle.down()
turtle.seth(270)
turtle.forward(3*b)
def drawX(x,y):
global isRunning,a,blue,cursor_pos,grid_pos,x_positions,one_count,two_count,cat_count,again
if isRunning:
return
else:
turtle.ht()
isRunning=True
turtle.up()
turtle.speed(0)
turtle.goto(x,y)
turtle.right(45)
turtle.width(5)
turtle.fillcolor(blue)
turtle.up()
turtle.forward(3*a/2.0)
turtle.down()
#start drawing X
turtle.fill(True)
turtle.left(90)
turtle.forward(a/2.0)
turtle.left(90)
turtle.forward(a)
turtle.right(90)
turtle.forward(a)
turtle.left(90)
turtle.forward(a)
turtle.left(90)
turtle.forward(a)
turtle.right(90)
turtle.forward(a)
turtle.left(90)
turtle.forward(a)
turtle.left(90)
turtle.forward(a)
turtle.right(90)
turtle.forward(a)
turtle.left(90)
turtle.forward(a)
turtle.left(90)
turtle.forward(a)
turtle.right(90)
turtle.forward(a)
turtle.left(90)
turtle.forward(a/2.0)
turtle.right(45)
turtle.fill(False)
#finish drawing X
isRunning=False
cursor_pos=(x,y)
x,y=cursor_pos
if x>=-b/2.0 and x<=b/2.0 and y>=-b/2.0 and y<=b/2.0:
grid_pos=4
elif x>b/2.0 and y>=-b/2.0 and y<=b/2.0:
grid_pos=5
elif x<-b/2.0 and y>=-b/2.0 and y<=b/2.0:
grid_pos=3
elif x>=-b/2.0 and x<=b/2.0 and y>b/2.0:
grid_pos=1
elif x>b/2.0 and y>b/2.0:
grid_pos=2
elif x<-b/2.0 and y>b/2.0:
grid_pos=0
elif x>=-b/2.0 and x<=b/2.0 and y<-b/2.0:
grid_pos=7
elif x>b/2.0 and y<-b/2.0:
grid_pos=8
elif x<-b/2.0 and y<-b/2.0:
grid_pos=6
x_positions.append(grid_pos)
if xWins():
print "X wins!"
again=raw_input("Would you like to play again? (y/n): ")
one_count+=1
if again!="y" and again!="Y":
print "X won",one_count,"times, O won",two_count,"times, and cat won",cat_count,"times. Click anywhere to exit."
turtle.exitonclick()
else:
turtle.clear()
restartGame()
elif oWins():
print "O wins!"
again=raw_input("Would you like to play again? (y/n): ")
two_count+=1
if again!="y" and again!="Y":
print "X won",one_count,"times, O won",two_count,"times, and cat won",cat_count,"times. Click anywhere to exit."
turtle.exitonclick()
else:
turtle.clear()
restartGame()
elif catWins():
print "Cat wins!"
again=raw_input("Would you like to play again? (y/n): ")
cat_count+=1
if again!="y" and again!="Y":
print "X won",one_count,"times, O won",two_count,"times, and cat won",cat_count,"times. Click anywhere to exit."
turtle.exitonclick()
else:
turtle.clear()
restartGame()
else:
pass
def drawO(x,y):
global isRunning,a,pink,cursor_pos,grid_pos,o_positions,one_count,two_count,cat_count,again
if isRunning:
return
else:
isRunning=True
turtle.up()
turtle.speed(0)
turtle.fillcolor(pink)
turtle.width(5)
turtle.goto(x,y)
turtle.setheading(270)
turtle.forward(a)
turtle.setheading(0)
turtle.down()
#start drawing O
turtle.fill(True)
turtle.circle(a)
turtle.fill(False)
#finish drawing O)
isRunning=False
cursor_pos=(x,y)
x,y=cursor_pos
if x>=-b/2.0 and x<=b/2.0 and y>=-b/2.0 and y<=b/2.0:
grid_pos=4
elif x>b/2.0 and y>=-b/2.0 and y<=b/2.0:
grid_pos=5
elif x<-b/2.0 and y>=-b/2.0 and y<=b/2.0:
grid_pos=3
elif x>=-b/2.0 and x<=b/2.0 and y>b/2.0:
grid_pos=1
elif x>b/2.0 and y>b/2.0:
grid_pos=2
elif x<-b/2.0 and y>b/2.0:
grid_pos=0
elif x>=-b/2.0 and x<=b/2.0 and y<-b/2.0:
grid_pos=7
elif x>b/2.0 and y<-b/2.0:
grid_pos=8
elif x<-b/2.0 and y<-b/2.0:
grid_pos=6
o_positions.append(grid_pos)
if xWins():
print "X wins!"
again=raw_input("Would you like to play again? (y/n): ")
one_count+=1
if again!="y" and again!="Y":
print "X won",one_count,"times, O won",two_count,"times, and cat won",cat_count,"times. Click anywhere to exit."
turtle.exitonclick()
else:
turtle.clear()
restartGame()
elif oWins():
print "O wins!"
again=raw_input("Would you like to play again? (y/n): ")
two_count+=1
if again!="y" and again!="Y":
print "X won",one_count,"times, O won",two_count,"times, and cat won",cat_count,"times. Click anywhere to exit."
turtle.exitonclick()
else:
turtle.clear()
restartGame()
elif catWins():
print "Cat wins!"
again=raw_input("Would you like to play again? (y/n): ")
cat_count+=1
if again!="y" and again!="Y":
print "X won",one_count,"times, O won",two_count,"times, and cat won",cat_count,"times. Click anywhere to exit."
turtle.exitonclick()
else:
turtle.clear()
restartGame()
else:
pass
#program starts here
print "******Tic-Tac-Toe, Turtle Edition******"
print "Play by yourself or with a friend.\n"
print "Left click to place an X; right click to place an O."
restartGame()
while True:
screen.onscreenclick(drawX)
screen.onscreenclick(drawO, 3)
screen.listen()
turtle.mainloop()