-
Notifications
You must be signed in to change notification settings - Fork 0
/
team31.py
437 lines (408 loc) · 15.3 KB
/
team31.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
import random
import sys
import copy
import time
class Player31:
"""
Class to Deal with the agent in game
"""
def __init__(self):
""" Default Constructor """
self.ply = 5
self.num = 0
self.cntp = 0
self.cnto = 0
pass
def get_free_cells(self, board, blocks_allowed, block_status):
"""
:param board: board input
:param blocks_allowed: list containing numbers of blocks which are allowed
:return: list of cells which are available for a move
"""
cells = []
for block in blocks_allowed:
startx = block / 3
starty = block % 3
for i in range(startx * 3, startx * 3 + 3):
for j in range(starty * 3, starty * 3 + 3):
if board[i][j] == '-':
cells.append((i, j))
if len(cells) == 0:
# print "Used Free Move"
new_blocks_allowed = [0, 1, 2, 3, 4, 5, 6, 7, 8]
for block in new_blocks_allowed:
startx = block / 3
starty = block % 3
for i in range(startx * 3, startx * 3 + 3):
for j in range(starty * 3, starty * 3 + 3):
if board[i][j] == '-' and block_status[block] == '-':
cells.append((i, j))
return cells
def free_move(self, block):
""" Function that deals with the case of a free move """
blocks_allowed = []
for i in range(9):
if block[i] == '-':
blocks_allowed.append(i);
return blocks_allowed
def get_allowed_blocks(self, block, old_move):
"""
:param block: list containg status of each block
:param old_move: previous move
:return: list of block numbres which are allowed
"""
blocks_allowed = []
if old_move == (-1, -1):
blocks_allowed = self.free_move(block)
elif old_move[0] % 3 == 0 and old_move[1] % 3 == 0:
blocks_allowed = [1, 3]
elif old_move[0] % 3 == 0 and old_move[1] % 3 == 2:
blocks_allowed = [1, 5]
elif old_move[0] % 3 == 2 and old_move[1] % 3 == 0:
blocks_allowed = [3, 7]
elif old_move[0] % 3 == 2 and old_move[1] % 3 == 2:
blocks_allowed = [5, 7]
elif old_move[0] % 3 == 0 and old_move[1] % 3 == 1:
blocks_allowed = [0, 2]
elif old_move[0] % 3 == 1 and old_move[1] % 3 == 0:
blocks_allowed = [0, 6]
elif old_move[0] % 3 == 2 and old_move[1] % 3 == 1:
blocks_allowed = [6, 8]
elif old_move[0] % 3 == 1 and old_move[1] % 3 == 2:
blocks_allowed = [2, 8]
elif old_move[0] % 3 == 1 and old_move[1] % 3 == 1:
blocks_allowed = [4]
else:
sys.exit(1)
final = []
for i in blocks_allowed:
if block[i] == '-':
final.append(i)
return final
def get_allowed_moves(self, board, block, old_move):
""" Function that gets the allowed moves in a particular state of game
:param board: board input
:param block: list containg status of each block
:param old_move: previous move
:return: list of block numbres which are allowed"""
blocks_allowed = self.get_allowed_blocks(block, old_move)
return self.get_free_cells(board, blocks_allowed, block)
def update_block(self, local_board, block_current, block_no, fl):
local_block = copy.deepcopy(block_current)
id1 = block_no / 3
id2 = block_no % 3
mflg = 0
flag = 0
for i in range(id1 * 3, id1 * 3 + 3):
for j in range(id2 * 3, id2 * 3 + 3):
if local_board[i][j] == '-':
flag = 1
if local_block[block_no] == '-':
if local_board[id1 * 3][id2 * 3] == local_board[id1 * 3 + 1][id2 * 3 + 1] and local_board[id1 * 3 + 1][
id2 * 3 + 1] == local_board[id1 * 3 + 2][id2 * 3 + 2] and local_board[id1 * 3 + 1][
id2 * 3 + 1] != '-' and local_board[id1 * 3 + 1][id2 * 3 + 1] != 'D':
mflg = 1
if local_board[id1 * 3 + 2][id2 * 3] == local_board[id1 * 3 + 1][id2 * 3 + 1] and local_board[id1 * 3 + 1][
id2 * 3 + 1] == local_board[id1 * 3][id2 * 3 + 2] and local_board[id1 * 3 + 1][
id2 * 3 + 1] != '-' and local_board[id1 * 3 + 1][id2 * 3 + 1] != 'D':
mflg = 1
if mflg != 1:
for i in range(id2 * 3, id2 * 3 + 3):
if local_board[id1 * 3][i] == local_board[id1 * 3 + 1][i] and local_board[id1 * 3 + 1][i] == \
local_board[id1 * 3 + 2][i] and local_board[id1 * 3][i] != '-' and local_board[id1 * 3][
i] != 'D':
mflg = 1
break
if mflg != 1:
for i in range(id1 * 3, id1 * 3 + 3):
if local_board[i][id2 * 3] == local_board[i][id2 * 3 + 1] and local_board[i][id2 * 3 + 1] == \
local_board[i][id2 * 3 + 2] and local_board[i][id2 * 3] != '-' and local_board[i][
id2 * 3] != 'D':
mflg = 1
break
if flag == 0:
local_block[block_no] = 'D'
if mflg == 1:
local_block[block_no] = fl
return local_block
def minimax(self, board, block, old_move, maxnode, player_flag, flag2, depth, alpha, beta, best_row, best_col):
"""
Applies the minimax algorithm on the state of game
:param board: board
:param block: status of blocks
:param old_move: previous move
:param maxnode: flag that tells whether it is maxnode or not
:param player_flag: players flag (x or o)
:param flag2: opponents flag
:param depth: depth in tree
:param alpha: alpha value
:param beta: beta value
:param best_row: present best move x-cordinate
:param best_col: present best move y-cordinate
:return: three tuple consisting of utlity of block and best moves cordinates
"""
if depth == self.ply:
utility = self.get_utility(board, block, player_flag, flag2)
return (utility, best_row, best_col)
else:
moves = self.get_allowed_moves(board, block, old_move)
# print "Number of moves:",len(moves)
if len(moves) == 0:
utility = self.get_utility(board, block, player_flag, flag2)
utility = round(utility,4)
self.ply = max(depth, 4)
return (utility, old_move[0], old_move[1])
if depth == 0:
if len(moves) > 10:
self.ply = min(self.ply, 4)
for move in moves:
# self.isp = 0
if maxnode:
board[move[0]][move[1]] = player_flag
else:
board[move[0]][move[1]] = flag2
block_no = (move[0] / 3) * 3 + move[1] / 3
fl = flag2
if maxnode: fl = player_flag
temp_block = self.update_block(board, block, block_no, fl)
if maxnode:
util = self.minimax(board, temp_block, move, False, player_flag, flag2, depth + 1, alpha, beta,
best_row,
best_col)
utility = round(util[0], 4)
if utility > alpha:
alpha = utility
best_row = move[0]
best_col = move[1]
else:
util = self.minimax(board, temp_block, move, True, player_flag, flag2, depth + 1, alpha, beta,
best_row,
best_col)
utility = round(util[0], 4)
if utility < beta:
beta = utility
best_row = move[0]
best_col = move[1]
board[move[0]][move[1]] = '-'
if alpha >= beta:
break
if depth == 0:
if best_row == '-1' or best_col == '-1':
best_row = moves[0][0]
best_col = moves[0][1]
if maxnode:
return (alpha, best_row, best_col, len(moves))
else:
return (beta, best_row, best_col, len(moves))
def move(self, board, block, old_move, player_flag):
"""
:param board: is the list of lists that represents the 9x9 grid
:param block: is a list that represents if a block is won or available to play in
:param old_move: is a tuple of integers representing co-ordintates of the last move made
:param flag: is player marker. it can be 'x' or 'o'.
board[i] can be 'x' or 'o'. block[i] can be 'x' or 'o'
Chooses a move based on minimax and alphabeta-pruning algorithm and returns it
:rtype tuple: the co-ordinates in 9X9 board
"""
self.isp = 0
if old_move == (-1, -1):
return (4, 4)
startt = time.clock()
if player_flag == 'o':
flag2 = 'x'
else:
flag2 = 'o'
self.num += 1
max_ply = 6
self.cntp = block.count(player_flag)
self.cnto = block.count(flag2)
if self.cnto - self.cntp > 1 or self.num > 25 or self.cntp == 2:
self.ply = max_ply
temp_board = copy.deepcopy(board)
temp_block = copy.deepcopy(block)
next_move = self.minimax(temp_board, temp_block, old_move, True, player_flag, flag2, 0, -100000.0, 100000.0, -1,
-1)
elapsed = (time.clock() - startt)
# print "Finally :", next_move, "Took:", elapsed
return (next_move[1], next_move[2])
def get_utility(self, board, block, playerFlag, opFlag):
"""
Function to find and return utility of a block
:param board: is the list of lists that represents the 9x9 grid
:param block: is a list that represents if a block is won or available to play in
:param playerFlag: player marker
:param opFlag: Opponent Marker
"""
util_values = [0 for i in range(9)]
for i in range(9):
util_values[i] = self.calc_utility(board, i, playerFlag)
gain = 0
lim = 100.0
for i in range(9):
util_values[i] /= lim
for i in range(3):
p = 0
cp = 0
ce = 0
for j in range(3):
p += util_values[j * 3 + i]
if block[j * 3 + i] == playerFlag:
cp += 1
elif block[j * 3 + i] == opFlag:
ce += 1
gain = self.get_factor(p, gain)
gain = self.get_new(cp, ce, gain)
for j in range(3):
p = 0
cp = 0
ce = 0
for i in range(3):
p += util_values[j * 3 + i]
if block[j * 3 + i] == playerFlag:
cp += 1
elif block[j * 3 + i] == opFlag:
ce += 1
gain = self.get_factor(p, gain)
gain = self.get_new(cp, ce, gain)
p = 0
cp = 0
ce = 0
for i in range(3):
p += util_values[3 * i + i]
if block[i * 3 + i] == playerFlag:
cp += 1
elif block[i * 3 + i] == opFlag:
ce += 1
gain = self.get_factor(p, gain)
gain = self.get_new(cp, ce, gain)
p = 0
cp = 0
ce = 0
for i in range(1, 4):
p += util_values[2 * i]
if block[i * 2] == playerFlag:
cp += 1
elif block[i * 2] == opFlag:
ce += 1
gain = self.get_new(cp, ce, gain)
gain = self.get_factor(p, gain)
if self.cntp < 2:
if block[4] == playerFlag:
gain += 10
elif block[4] != '-':
gain -= 10
cnt1 = block.count(playerFlag)
cnt2 = block.count(opFlag)
if self.cntp < cnt1 and cnt2 == self.cnto:
gain += 50
elif cnt1 > self.cntp and (cnt1 - self.cntp) < (cnt2 - self.cnto):
gain -= 20
elif cnt1 < self.cntp and cnt2 > self.cnto:
gain -= 50
return gain
def calc_utility(self, board, boardno, playerFlag):
gain = 0
startx = boardno / 3
starty = boardno % 3
starty *= 3
startx *= 3
for i in range(startx, startx + 3):
cp = 0
ce = 0
cd = 0
for j in range(starty, starty + 3):
if board[i][j] == '-':
cd += 1
elif board[i][j] == playerFlag:
cp += 1
else:
ce += 1
gain = self.calc(cp, ce, gain)
for j in range(starty, starty + 3):
cp = 0
ce = 0
cd = 0
for i in range(startx, startx + 3):
if board[i][j] == '-':
cd += 1
elif board[i][j] == playerFlag:
cp += 1
else:
ce += 1
gain = self.calc(cp, ce, gain)
cp = 0
cd = 0
ce = 0
for i in range(0, 3):
if board[startx + i][starty + i] == playerFlag:
cp += 1
elif board[startx + i][starty + i] == '-':
cd += 1
else:
ce += 1
gain = self.calc(cp, ce, gain)
for i in range(0, 3):
if board[startx + i][starty + 2 - i] == playerFlag:
cp += 1
elif board[startx + i][starty + 2 - i] == '-':
cd += 1
else:
ce += 1
gain = self.calc(cp, ce, gain)
return gain
def calc(self, cx, co, gain):
if cx == 3:
gain += 100
if cx == 2:
gain += 10
if cx == 1:
gain += 1
if co == 3:
gain -= 100
if co == 2:
gain -= 10
if co == 1:
gain -= 1
return gain
def get_factor(self, p, gain):
if p < 1 and p >= -1:
gain += p
if p >= 1 and p < 2:
val = 1
val += (p - 1) * 9
gain += val
if p >= 2 and p < 3:
val = 10
val += (p - 1) * 90
gain += val
if p >= 3:
val = 100
val += (p - 3) * 900
gain += val
if p >= -2 and p < -1:
val = -1
val -= (abs(p) - 1) * 9
gain += val
if p >= -3 and p < -2:
val = -10
val -= (abs(p) - 2) * 90
gain += val
if p < -3:
val = -100
val -= (abs(p) - 3) * 900
gain += val
return gain
def get_new(self, cx, co, gain):
if cx == 3:
gain += 1000
if cx == 2:
gain += 100
if cx == 1:
gain += 10
if co == 3:
gain -= 1000
if co == 2:
gain -= 100
if co == 1:
gain -= 10
return gain