-
Notifications
You must be signed in to change notification settings - Fork 0
/
tictac.py
351 lines (312 loc) · 8.87 KB
/
tictac.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#Tic Tac Toe game created by Matti roughly based on a youtube video
#created: February 2012
#
#url to the video: http://www.youtube.com/watch?v=mMO-spo20jU
import random
import copy
board = [ 0, 1, 2,
3, 4, 5,
6, 7, 8 ]
#test board used for hard difficulty
tboard = [ 0, 1, 2,
3, 4, 5,
6, 7, 8 ]
result = [ 'u' ] #result of the game
def show():
print board[0], "|", board[1], "|", board[2]
print "----------"
print board[3], "|", board[4], "|", board[5]
print "----------"
print board[6], "|", board[7], "|", board[8]
def showt(): #show the test board, used for debugging
print tboard[0], "|", tboard[1], "|", tboard[2]
print "----------"
print tboard[3], "|", tboard[4], "|", tboard[5]
print "----------"
print tboard[6], "|", tboard[7], "|", tboard[8]
def boardIsFull():
cnt = 0
for i in range(9):
if board[i] != i:
cnt += 1
if cnt == 9:
return True
else:
return False
def checkLine(char,spot1,spot2,spot3):
return char == board[spot1] and char == board[spot2] and char == board[spot3]
def checkAll(char):
if checkLine(char,0,1,2):
return True
elif checkLine(char,3,4,5):
return True
elif checkLine(char,6,7,8):
return True
elif checkLine(char,0,3,6):
return True
elif checkLine(char,1,4,7):
return True
elif checkLine(char,2,5,8):
return True
elif checkLine(char,0,4,8):
return True
elif checkLine(char,2,4,6):
return True
else:
return False
def checkForResult():
r = True;
if checkAll('x'):
result[0] = 'x'
return r
elif checkAll('o'):
result[0] = 'o'
return r
elif boardIsFull():
result[0] = 'd'
return r
else:
return False
#Computer AI
def easy(): #randomly generated
while True:
if boardIsFull():
break
random.seed()
op = random.randint(0,8)
if (board[op] != 'x' and board[op] != 'o'):
board[op] = 'o'
break
def medium(): #wins if two o in a row/ stops user win if two x in a row
if boardIsFull():
return
if checkForTwo('o', board):
return
elif checkForTwo('x', board):
return
else:
easy()
def hard(): #yeah a copout, but hey, kindof works
random.seed()
fate = random.randint(0,323323323)
if fate > (323323323)/3.0:
impossible()
elif fate > 666 and fate < 666666:
easy()
else:
medium()
def impossible(): #name says it all, it allegedly always draws, but does not try to trick you to win
if boardIsFull():
return
if checkForTwo('o', board):
return
elif checkForTwo('x', board):
return
elif board[4] == 4: #always put in middle if possible
board[4] = 'o'
return
elif onexwin():
return
elif board[4] == 'x' and isCorner(): #put in a corner
cornerlist = corner()
random.seed()
op = random.randint(0,len(cornerlist)-1) #random number
board[cornerlist[op]] = 'o'
return
#these two are not really functioning as desired, but attain the desirable effect
elif checkForTwoAhead('x'):
return
elif checkForTwoAhead('o'):
return
else:
medium()
#onexwin checks the one win I couldnt program to avoid, do it manually
def onexwin():
# this:
# 0 x 2
# 3 o 5
# 6 7 x
if (board[0] == 'x'):
if board[7] == 'x' or board[5] == 'x':
if board[8] == 8:
board[8] = 'o'
return True
else:
return False
elif (board[2] == 'x'):
if board[3] == 'x' or board[7] == 'x':
if board[6] == 6:
board[6] = 'o'
return True
else:
return False
elif (board[6] == 'x'):
if board[1] == 'x' or board[5] == 'x':
if board[2] == 2:
board[2] = 'o'
return True
else:
return False
elif (board[8] == 'x'):
if board[1] == 'x' or board[3] == 'x':
if board[0] == 0:
board[0] = 'o'
return True
else:
return False
#checks to see if user can put an x so that next round he'll have two spots to put an x for a win
def checkForTwoAhead(char):
checked = []
cnt = 0
#check how many o's and x's are already
for i in range(len(board)):
if board[i] != i:
cnt += 1
while True:
#if we have checked all empty spots we have to break
if len(checked)+cnt > 8:
break
#copy board[elements] into tboard[elements]
for i in range(len(board)):
tboard[i] = board[i]
for i in range(len(tboard)):
if tboard[i] == i and not(inList(checked,i)):
tboard[i] = char
checked.append(i)
if checkForTwo(char, tboard):
pos = differenceOfBoards(tboard,board)
if pos < 0 or pos > 8:
print "Unexpected error in determining position for computer"
else:
board[pos] = 'o'
return True
else:
break
return False
#len(b1) == len(b2)
def differenceOfBoards(b1,b2):
pos = -1
for i in range(len(b1)):
if b1[i] != b2[i]:
return i
return pos
#er x i listanum listi?
def inList(listi, x):
for i in range(len(listi)):
if listi[i] == x:
return True
return False
#returns true iff there is an available corner
def isCorner():
return board[0] == 0 or board[2] == 2 or board[6] == 6 or board[8] == 8
#returns a list of available corners
def corner():
list = [ ]
if board[0] == 0:
list.append(0)
if board[2] == 2:
list.append(2)
if board[6] == 6:
list.append(6)
if board[8] == 8:
list.append(8)
return list
def checkForTwo(char,b): #b is current board for testing
#check all the lines
if checkTwo(char,0,1,2,b):
return True
elif checkTwo(char,3,4,5,b):
return True
elif checkTwo(char,6,7,8,b):
return True
elif checkTwo(char,0,3,6,b):
return True
elif checkTwo(char,1,4,7,b):
return True
elif checkTwo(char,2,5,8,b):
return True
elif checkTwo(char,0,4,8,b):
return True
elif checkTwo(char,2,4,6,b):
return True
else:
return False
def checkTwo(char,spot1,spot2,spot3, b): # b = current board for testing
cnt = 0 #char-count
target = -1
if (b[spot1] == char):
cnt += 1
else:
target = spot1
if (b[spot2] == char):
cnt += 1
else:
target = spot2
if (b[spot3] == char):
cnt += 1
else:
target = spot3
if (cnt == 2):
if (target >= 0):
if char == 'x':
if b[target] == 'o': #spot taken
return False
b[target] = 'o' # to stop a win from user
else:
if b[target] == 'x': #spot taken
return False
b[target] = 'o' # for a win
else:
return False
return True
else:
return False
def compare(dif):
diflist = [ "easy", "medium", "hard", "impossible", "e", "m", "h", "i" ]
for i in range(len(diflist)):
if cmp(dif, diflist[i]) == 0:
return True
return False
while True:
difficulty = raw_input("Choose difficuty:\neasy - medium - hard - impossible\n")
difficulty = str(difficulty)
if compare(difficulty):
break
else:
print "Wrong input, type in one of the diffuculties"
continue
while True:
if checkForResult():
show()
break
else:
show()
input = raw_input("Select a spot: ")
input = int(input)
#user's turn
if (board[input] != 'x' and board[input] != 'o'):
board[input] = 'x'
if checkForResult():
show()
break
#computer's turn
if (cmp(difficulty,"easy") == 0 or cmp(difficulty,"e") == 0):
easy()
elif (cmp(difficulty,"medium") == 0 or cmp(difficulty,"m") == 0):
medium()
elif (cmp(difficulty,"hard") == 0 or cmp(difficulty,"h") == 0):
hard()
elif (cmp(difficulty,"impossible") == 0 or cmp(difficulty,"i") == 0):
impossible()
else:
print "That spot is taken!"
if result[0] == 'x':
print '~~ X wins! ~~'
if (cmp(difficulty,"impossible") == 0 or cmp(difficulty,"i") == 0):
print "You won the impossible difficulty!?\nDang it, I need to improve my AI making skills."
elif result[0] == 'o':
print '~~ O wins! ~~'
elif result[0] == 'd':
print '~~ Draw! ~~'
else:
print 'Unexpected outcome of game'