-
Notifications
You must be signed in to change notification settings - Fork 0
/
team14.py
297 lines (269 loc) · 11 KB
/
team14.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
import random
import copy
inf = 1e6
class Player14(object):
def __init__(self):
self.inf = 1e10
self.generateHeuristicMatrix()
def move(self, current_board_game, board_stat, move_by_opponent, flag):
if move_by_opponent == (-1, -1):
return (4, 4)
self.myMark = flag
self.other = self.getOpp(self.myMark)
possible_cells = self.getValidCells(current_board_game, board_stat, move_by_opponent)
self.node_count = 0
idx = possible_cells[0]
best_val = -self.inf
depth = 0
while best_val != inf and self.node_count < 100000:
depth += 1
best_val = -self.inf
for cell in possible_cells:
bstat = board_stat[:]
self.updateBoardStat(current_board_game, bstat, cell, flag)
temp = self.alphaBetaPruning(current_board_game, bstat, depth, -self.inf, self.inf, True, cell)
if temp > best_val:
best_val = temp
idx = cell
current_board_game[cell[0]][cell[1]] = '-'
my_move = idx
print "sahay ", flag
return my_move
def getOpp(self, flag):
if flag == 'x':
return 'o'
return 'x'
def getEmptyCells(self, gameBoard, blocksAllowed, blockStat):
cells = []
for block in blocksAllowed:
i=block/3
j=block%3
for k in range(i*3,i*3+3):
for l in range(j*3,j*3+3):
if(gameBoard[k][l] == '-'):
cells.append((k,l))
if cells == []:
block = [0,1,2,3,4,5,6,7,8]
blocksAllowed = []
for i in block:
if blockStat[i]=='-':
blocksAllowed.append(i)
for block in blocksAllowed:
i=block/3
j=block%3
for k in range(i*3,i*3+3):
for l in range(j*3,j*3+3):
if(gameBoard[k][l] == '-'):
cells.append((k,l))
return cells
def getAllowedblocks(self,oldMove, blockStat):
# To check block status and get permittedBlocks in which we can make our move
permittedBlocks=[]
if oldMove[0]%3==0:
if oldMove[1]%3==0:
if(blockStat[1]=='-'):
permittedBlocks.append(1)
if(blockStat[3]=='-'):
permittedBlocks.append(3)
elif oldMove[1]%3==1:
if(blockStat[0]=='-'):
permittedBlocks.append(0)
if(blockStat[2]=='-'):
permittedBlocks.append(2)
elif oldMove[1]%3==2:
if(blockStat[1]=='-'):
permittedBlocks.append(1)
if(blockStat[5]=='-'):
permittedBlocks.append(5)
else:
sys.exit(1)
elif oldMove[0]%3==1:
if oldMove[1]%3==0:
if(blockStat[0]=='-'):
permittedBlocks.append(0)
if(blockStat[6]=='-'):
permittedBlocks.append(6)
elif oldMove[1]%3==1:
if(blockStat[4]=='-'):
permittedBlocks.append(4)
elif oldMove[1]%3==2:
if(blockStat[2]=='-'):
permittedBlocks.append(2)
if(blockStat[8]=='-'):
permittedBlocks.append(8)
else:
sys.exit(1)
elif oldMove[0]%3==2:
if oldMove[1]%3==0:
if(blockStat[3]=='-'):
permittedBlocks.append(3)
if(blockStat[7]=='-'):
permittedBlocks.append(7)
elif oldMove[1]%3==1:
if(blockStat[6]=='-'):
permittedBlocks.append(6)
if(blockStat[8]=='-'):
permittedBlocks.append(8)
elif oldMove[1]%3==2:
if(blockStat[7]=='-'):
permittedBlocks.append(7)
if(blockStat[5]=='-'):
permittedBlocks.append(5)
else:
sys.exit(1)
else:
sys.exit(1)
return permittedBlocks
#Tan
row, column = move_by_opponent[0] % 3, move_by_opponent[1] % 3
valid_blocks = []
if row == 0 and column == 0:
valid_blocks = [0, 1, 3]
elif row == 0 and column == 2:
valid_blocks = [1, 2, 5]
elif row == 2 and column == 0:
valid_blocks = [3, 6, 7]
elif row == 2 and column == 2:
valid_blocks = [5, 7, 8]
else:
valid_blocks = [3 * row + column]
valid_cells = []
for i in valid_blocks:
if board_stat[i] != '-':
continue
row = (i/3) * 3
column = (i%3) * 3
for j in xrange(0, 3):
for k in xrange(0, 3):
r = row + j
c = column + k
if current_board_game[r][c] == '-':
valid_cells.append((r, c))
if len(valid_cells) == 0:
for i in xrange(0, 9):
for j in xrange(0, 9):
if board_stat[(i/3) * 3 + (j/3)] == '-' and current_board_game[i][j] == '-':
valid_cells.append((i, j))
easy_move = []
for i in valid_cells:
if board_stat[(i[0]%3) * 3 + i[1]%3] != '-':
valid_cells.remove(i)
easy_move.append(i)
if len(valid_cells) == 0:
valid_cells = easy_move
random.shuffle(valid_cells)
return valid_cells
def updateBoardStat(self, board_game, board_stat, move, flag):
board_game[move[0]][move[1]] = flag
block_no = (move[0]/3) * 3 + move[1]/3
row = (block_no/3) * 3
column = (block_no%3) * 3
is_done = 0
if board_stat[block_no] == '-':
if board_game[row][column] == board_game[row+1][column+1] and board_game[row+1][column+1] == board_game[row+2][column+2] and board_game[row][column] != '-':
is_done = 1
if board_game[row+2][column] == board_game[row+1][column+1] and board_game[row+1][column+1] == board_game[row][column+2] and board_game[row+1][column+1] != '-':
is_done = 1
if not is_done:
for i in xrange(column, column + 3):
if board_game[row][i] == board_game[row+1][i] and board_game[row+1][i] == board_game[row+2][i] and board_game[row][i] != '-':
is_done = 1
break
if not is_done:
for i in xrange(row, row + 3):
if board_game[i][column] == board_game[i][column+1] and board_game[i][column+1] == board_game[i][column+2] and board_game[i][column] != '-':
is_done = 1
break
if is_done:
board_stat[block_no] = flag
empty_cells = []
for i in xrange(row, row + 3):
for j in xrange(column, column + 3):
if board_game[i][j] == '-':
empty_cells.append((i, j))
if len(empty_cells) == 0 and not is_done:
board_stat[block_no] = 'd'
return
def getValidCells(self, current_board_game, board_stat, move_by_opponent):
cells = []
blocksAllowed = self.getAllowedblocks(move_by_opponent,board_stat)
cells = self.getEmptyCells(current_board_game, blocksAllowed, board_stat)
return cells
def alphaBetaPruning(self, board_game, board_stat, depth, alpha, beta, flag, node):
self.node_count += 1
if self.isTerminal(board_stat):
return self.getUtilityVal(board_stat)
if depth == 0:
return self.getHeuristicVal(board_game, board_stat)
children = self.getValidCells(board_game, board_stat, node)
if flag:
val = -self.inf
for child in children:
new_board_stat = board_stat[:]
self.updateBoardStat(board_game, new_board_stat, child, self.myMark if flag else self.other)
val = max(val, self.alphaBetaPruning(board_game, new_board_stat, depth - 1, alpha, beta, False, child))
alpha = max(alpha, val)
board_game[child[0]][child[1]] = '-'
if beta <= alpha:
break
return val
else:
val = self.inf
for child in children:
new_board_stat = board_stat[:]
self.updateBoardStat(board_game, new_board_stat, child, myMark if flag else self.other)
val = min(val, self.alphaBetaPruning(board_game, new_board_stat, depth - 1, alpha, beta, True, child))
beta = min(beta, value)
board_game[child[0]][child[1]] = '-'
if beta <= alpha:
break
return val
def isTerminal(self, board_stat):
for i in xrange(0, 9):
if board_stat[i] == '-':
return False
return True
def getUtilityVal(self, board_stat):
for i in self.representative_three_matrix:
myCount = 0
otherCount = 0
for j in i:
if board_stat[j] == self.myMark:
myCount += 1
elif board_stat[j] == self.other:
otherCount += 1
if myCount == 3:
return self.inf
elif otherCount == 3:
return -self.inf
return 0
def getHeuristicVal(self, board_game, board_stat):
heuristic = 0
for i in self.representative_three_matrix:
myCount = 0
otherCount = 0
for j in i:
if board_stat[j] == self.myMark:
myCount += 1
elif board_stat[j] == self.other:
otherCount += 1
heuristic += 100 * self.heuristic_value_matrix[myCount][otherCount]
for i in xrange(0, 3):
for j in xrange(0, 3):
if board_stat[3 * i + j] != '-':
continue
for representative_three_matrix in self.representative_three_matrix:
myCount = 0
otherCount = 0
for idx in representative_three_matrix:
r = 3 * i + idx/3
c = 3 * j + idx%3
if board_game[r][c] == self.myMark:
myCount += 1
elif board_game[r][c] == self.other:
otherCount += 1
heuristic += self.heuristic_value_matrix[myCount][otherCount]
return heuristic
def generateHeuristicMatrix(self):
self.representative_three_matrix = [[0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 3, 6], [1, 4, 7], [2, 5, 8], [0, 4, 8], [2, 4, 6]]
self.heuristic_value_matrix = [[0, -10, -100, -1000], [10, 0, 0], [100, 0], [1000]]