-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagent.py
358 lines (324 loc) · 9.16 KB
/
agent.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
351
352
353
354
355
356
357
358
import sys
import random
import signal
import time
import copy
# from simulator import BigBoard
inf = 100000000000000000
inf2 = 10000000000000
class MyPlayer():
def __init__(self):
pass
self.empty_small_board = "---------"
self.small_board_score = {}
self.small_board_score_d = {}
self.preprocess(self.empty_small_board)
self.preprocess_d(self.empty_small_board)
for i in range(1,10000000000):
pass
quit()
def change(self,s,i,c):
s = s[:i] + c + s[i+1:]
return s
def check_small_board_won_matrix(self,bs):
for i in range(3):
if (bs[i][0] == bs[i][1] == bs[i][2]) and (bs[i][0] == 'o'):
return 50
if (bs[i][0] == bs[i][1] == bs[i][2]) and (bs[i][0] == 'x'):
return -50
if (bs[0][i] == bs[1][i] == bs[2][i]) and (bs[0][i] == 'o'):
return 50
if (bs[0][i] == bs[1][i] == bs[2][i]) and (bs[0][i] == 'x'):
return -50
#checking for diagonal patterns
if (bs[0][0] == bs[1][1] == bs[2][2]) and (bs[0][0] == 'o'):
return 50
if (bs[0][0] == bs[1][1] == bs[2][2]) and (bs[0][0] == 'x'):
return -50
#diagonal 2
if (bs[0][2] == bs[1][1] == bs[2][0]) and (bs[0][2] == 'o'):
return 50
if (bs[0][2] == bs[1][1] == bs[2][0]) and (bs[0][2] == 'x'):
return -50
return 0
def check_small_board_won_str(self,bs):
for i in range(3):
if (bs[3*i] == bs[3*i+1] == bs[3*i+2]) and (bs[3*i] == 'o'):
return 50
if (bs[3*i] == bs[3*i+1] == bs[3*i+2]) and (bs[3*i] == 'x'):
return -50
if (bs[i] == bs[i+3] == bs[i+6]) and (bs[i] == 'o'):
return 50
if (bs[i] == bs[i+3] == bs[i+6]) and (bs[i] == 'x'):
return -50
#checking for diagonal patterns
if (bs[0] == bs[4] == bs[8]) and (bs[0] == 'o'):
return 50
if (bs[0] == bs[4] == bs[8]) and (bs[0] == 'x'):
return -50
#diagonal 2
if (bs[2] == bs[4] == bs[6]) and (bs[2] == 'o'):
return 50
if (bs[2] == bs[4] == bs[6]) and (bs[2] == 'x'):
return -50
return 0
def preprocess(self,bs):
x = self.check_small_board_won_str(bs)
if x == 50 :
self.small_board_score[bs]=(1,0,0)
return (1,0,0)
if x == -50 :
self.small_board_score[bs]=(0,1,0)
return (0,1,0)
#check if board is already complete
flag = 0
for i in range(9):
if bs[i] != '-' :
flag = flag + 1
if flag == 9 :
self.small_board_score[bs]= (0,0,1)
return (0,0,1)
if bs in self.small_board_score:
return self.small_board_score[bs]
ret0=0
ret1=0
ret2=0
for i in range(9):
if bs[i] == '-':
bs = self.change(bs,i,'o')
val = self.preprocess(bs)
ret0+=val[0]
ret1+=val[1]
ret2+=val[2]
bs = self.change(bs,i,'x')
val = self.preprocess(bs)
ret0+=val[0]
ret1+=val[1]
ret2+=val[2]
bs = self.change(bs,i,'-')
self.small_board_score[bs] = (ret0,ret1,ret2)
return (ret0,ret1,ret2)
def preprocess_d(self,bs):
x = self.check_small_board_won_str(bs)
if x == 50 :
self.small_board_score_d[bs]=(1,0,0)
return (1,0,0)
if x == -50 :
self.small_board_score_d[bs]=(0,1,0)
return (0,1,0)
#check if board is already complete
flag = 0
for i in range(9):
if bs[i] != '-' :
flag = flag + 1
if flag == 9 :
self.small_board_score_d[bs]= (0,0,1)
return (0,0,1)
if bs in self.small_board_score_d:
return self.small_board_score_d[bs]
ret0=0
ret1=0
ret2=0
for i in range(9):
if bs[i] == '-':
bs = self.change(bs,i,'o')
val = self.preprocess_d(bs)
ret0+=val[0]
ret1+=val[1]
ret2+=val[2]
bs = self.change(bs,i,'x')
val = self.preprocess_d(bs)
ret0+=val[0]
ret1+=val[1]
ret2+=val[2]
bs = self.change(bs,i,'d')
val = self.preprocess_d(bs)
ret0+=val[0]
ret1+=val[1]
ret2+=val[2]
bs = self.change(bs,i,'-')
self.small_board_score_d[bs] = (ret0,ret1,ret2)
return (ret0,ret1,ret2)
def find_valid_move_cells(self, board,old_move):
#returns the valid cells allowed given the last move and the current board state
allowed_cells = []
allowed_small_board = [old_move[1]%3, old_move[2]%3]
#checks if the move is a free move or not based on the rules
if old_move == (-1,-1,-1) or (board.small_boards_status[0][allowed_small_board[0]][allowed_small_board[1]] != '-' and board.small_boards_status[1][allowed_small_board[0]][allowed_small_board[1]] != '-'):
for k in range(2):
for i in range(9):
for j in range(9):
if board.big_boards_status[k][i][j] == '-' and board.small_boards_status[k][i/3][j/3] == '-':
allowed_cells.append((k,i,j))
else:
for k in range(2):
if board.small_boards_status[k][allowed_small_board[0]][allowed_small_board[1]] == "-":
for i in range(3*allowed_small_board[0], 3*allowed_small_board[0]+3):
for j in range(3*allowed_small_board[1], 3*allowed_small_board[1]+3):
if board.big_boards_status[k][i][j] == '-':
allowed_cells.append((k,i,j))
return allowed_cells
def print_board(self,board):
# for printing the state of the board
for i in range(9):
if i%3 == 0:
print
for k in range(2):
for j in range(9):
if j%3 == 0:
print "",
print board.big_boards_status[k][i][j],
if k==0:
print " ",
print
print
for i in range(3):
for k in range(2):
for j in range(3):
print board.small_boards_status[k][i][j],
if k==0:
print " ",
print
print
print
def heuristic(self,board,flg):
cross_score = 0
oval_score = 0
# print(board)
factor=1000
for k in range(2):
for i in range(3):
for j in range(3):
temp_board = ""
for x in range(3):
for y in range(3):
temp_board +=board.big_boards_status[k][3*i+x][3*j+y]
score = self.small_board_score[temp_board]
total = score[0] + score[1] + score[2]
oval_val = float(score[0])/total
cross_val = float(score[1])/total
oval_score += float(oval_val)*factor
cross_score += float(cross_val)*factor
for k in range(2):
temp_board = ""
for i in range(3):
for j in range(3):
temp_board+=board.small_boards_status[k][i][j]
score = self.small_board_score[temp_board]
total = score[0] + score[1] + score[2]
oval_val= float(score[0])/total
cross_val= float(score[1])/total
oval_score += float(oval_val)*factor*100.0
cross_score += float(cross_val)*factor*100.0
return (oval_score,cross_score)
def minmax(self,cur_board,old_move,flg,dep):
if time.time() - self.st_time > 15:
if dep == self.gdep:
return (-1,-1,-1)
else :
return (-1,-1)
flg2 = 'o'
if flg == 'o' :
flg2 = 'x'
if dep == 0 :
return self.heuristic(cur_board,flg)
mx = -inf
rx = 0
ry = 0
rz = 0
ret = (0,0)
allowed_cells = self.find_valid_move_cells(cur_board,old_move)
if allowed_cells is not None:
for i in range(len(allowed_cells)):
if time.time() - self.st_time > 15:
if dep == self.gdep:
return (-1,-1,-1)
else :
return (-1,-1)
x = allowed_cells[i][0]
y = allowed_cells[i][1]
z = allowed_cells[i][2]
cur_board.big_boards_status[x][y][z]=flg
sty = y/3
stz = z/3
temp_board = ([['-' for i in range(3)] for j in range(3)])
for i in range(3):
for j in range(3):
temp_board[i][j] = cur_board.big_boards_status[x][3*sty+i][3*stz+j]
another_turn = 0
val2 = self.check_small_board_won_matrix(temp_board)
if val2 != 0 :
cur_board.small_boards_status[x][sty][stz] = flg
another_turn = 1
arnav = self.check_small_board_won_matrix(cur_board.big_boards_status[x])
if arnav != 0 :
cur_board.big_boards_status[x][y][z]= '-'
cur_board.small_boards_status[x][sty][stz] = '-'
if dep == self.gdep:
return (x,y,z)
elif flg == 'o':
return (inf2,0)
else :
return (0,inf2)
val = (0,0)
if another_turn == 1 :
val = self.minmax(cur_board,(x,y,z),flg,dep-1)
else :
val = self.minmax(cur_board,(x,y,z),flg2,dep-1)
cur_board.big_boards_status[x][y][z]= '-'
cur_board.small_boards_status[x][sty][stz] = '-'
score = val[0] - val[1]
if flg == 'o' :
if score > mx:
mx = score
rx = x
ry = y
rz = z
ret = val
else :
score = -score
if score > mx:
mx = score
rx = x
ry = y
rz = z
ret = val
if dep == self.gdep:
return (rx,ry,rz)
else :
return ret
def move(self,board,old_move,flg):
self.st_time = time.time()
cur_board = copy.deepcopy(board)
allowed_cells = self.find_valid_move_cells(cur_board,old_move)
for i in range(len(allowed_cells)):
x = allowed_cells[i][0]
y = allowed_cells[i][1]
z = allowed_cells[i][2]
cur_board.big_boards_status[x][y][z]=flg
sty = y/3
stz = z/3
temp_board = ([['-' for i in range(3)] for j in range(3)])
for i in range(3):
for j in range(3):
temp_board[i][j]= cur_board.big_boards_status[x][3*sty+i][3*stz+j]
another_turn = 0
val2 = self.check_small_board_won_matrix(temp_board)
if val2 != 0 :
return(x,y,z)
cur_board.big_boards_status[x][y][z]='-'
x=0
y=0
z=0
for i in range(1,100):
self.gdep = i
cur_move = self.minmax(cur_board,old_move,flg,i)
if time.time()-self.st_time > 15 :
return (x,y,z)
x = cur_move[0]
y = cur_move[1]
z = cur_move[2]
return (x,y,z)
# board = BigBoard()
player = MyPlayer()
# player.heuristic(board)