-
Notifications
You must be signed in to change notification settings - Fork 0
/
team58.py
410 lines (368 loc) · 10.4 KB
/
team58.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
import time
global depth_limit
depth_limit = 0
import random
class Player58:
def __init__(self):
# You may initialize your object here and use any variables for storing throughout the game
pass
def move(self,temp_board,temp_block,old_move,flag):
#List of permitted blocks, based on old move.
start_time = time.time()
blocks_allowed = determine_blocks_allowed(old_move, temp_block)
#Get list of empty valid cells
cells = get_empty_out_of(temp_board, blocks_allowed,temp_block)
alpha = -100000
beta = 10000
global depth_limit
depth_limit = 0
countp1 = 0
countp2 = 0
for i in range(9):
for j in range(9):
if temp_board[i][j] == 'o':
countp1 += 1
elif temp_board[i][j] == 'x':
countp2 += 1
if countp1 > countp2 :
p1 = 'x'
p2 = 'o'
elif countp1 == countp2:
p1 = 'x'
p2 = 'o'
else:
p1 = 'o'
p2 = 'x'
for i in cells:
if beta >alpha :
temp_board[i[0]][i[1]]=p1
utility_child = built_tree(temp_board,temp_block,i,0,alpha,beta,p1,p2,start_time)
if alpha < utility_child:
alpha = utility_child
possible_moves = []
possible_moves.append(i)
elif alpha == utility_child:
possible_moves.append(i)
temp_board[i[0]][i[1]]='-'
# print (time.time() - start_time),
# print depth_limit
if len(possible_moves)>1:
# print "enter"
length = random.randrange(len(possible_moves))
move = possible_moves[length]
# print possible_moves
return move
else:
return possible_moves[0]
def built_tree(temp_board,temp_block,old_move,depth,alpha_p,beta_p,p1,p2,start_time):
# making recursive tree
# assigning alpha-beta for current node
global depth_limit
alpha = alpha_p
beta = beta_p
#List of permitted blocks, based on old move.
blocks_allowed = determine_blocks_allowed(old_move, temp_block)
#Get list of empty valid cells
cells = get_empty_out_of(temp_board,blocks_allowed,temp_block)
if cells == [] or (time.time() - start_time)>11.9:
utility_value = utility(temp_board,temp_block,old_move,alpha,beta,p1,p2,depth)
# print depth,
if depth_limit < depth:
depth_limit =depth
return utility_value
else:
depth += 1
for i in cells:
# utility(temp_board,temp_block)
if beta > alpha :
if ((depth)%2) == 1:
# min
temp_board[i[0]][i[1]]=p2
utility_child = built_tree(temp_board,temp_block,i,depth,alpha,beta,p1,p2,start_time)
if beta > utility_child:
beta = utility_child
else:
# max
temp_board[i[0]][i[1]]=p1
utility_child = built_tree(temp_board,temp_block,i,depth,alpha,beta,p1,p2,start_time)
if alpha < utility_child:
alpha = utility_child
temp_board[i[0]][i[1]]='-'
# depth -= 1
if depth%2 == 1:
depth -= 1
return beta
else:
depth -= 1
return alpha
def utility(board_game,block_stat,move,alpha,beta,p1,p2,depth):
# determine utility value
# 8 lines
# decide utility value for each state
# print_lists(board_game,block_stat)
# command = raw_input("utility:")
utility = 0
utility_stat_p1 = 0
utility_stat_p2 = 0
utility_p1 = 0
utility_p2 = 0
# number of 'x' and 'o'
countp1 = 0
countp2 = 0
count_stat_p1 = 0
count_stat_p2 = 0
# defining position of block
tempx = move[0]/3
tempy = move[1]/3
# print "tempx=",tempx,"tempy=",tempy
temp_block_cell = tempx*3 + tempy
tempx *= 3
tempy *= 3
temp_board = [[0 for x in range(3)] for x in range(3)]
if (depth%2) == 1:
block_stat[temp_block_cell] = p1
# # print block_stat
else:
block_stat[temp_block_cell] = p2
# print "cell =",temp_block_cell
# temp_block = [[0 for x in range(3)] for x in range(3)]
# defining new temp_board
for i in range(3):
for j in range(3):
temp_board[i][j] = board_game[i+tempx][j+tempy]
if temp_board[i][j] == p1:
countp1 += 1
elif temp_board[i][j] == p2:
countp2 += 1
if block_stat[i*3+j] == p1:
count_stat_p1 += 1
elif block_stat[i*3+j] == p2:
count_stat_p2 += 1
# counting number of 'x' and 'o' to assign utility value
# counting in a row
for i in range(3):
count1 = 0
count2 = 0
count3 = 0
count4 = 0
for j in range(3):
if temp_board[i][j] == p1:
count2 += 1
elif temp_board[i][j] == p2:
count1 += 1
if block_stat[i*3+j] == p1:
count3 += 1
elif block_stat[i*3+j] == p2:
count4 += 1
# print "acha"
# print ":done"
utilityarray = define_utility(count1,count2,count3,count4)
utility_p1 += utilityarray[0]
utility_p2 += utilityarray[1]
utility_stat_p1 += utilityarray[2]
utility_stat_p2 += utilityarray[3]
# counting in a column
for j in range(3):
count1 = 0
count2 = 0
count3 = 0
count4 = 0
for i in range(3):
if temp_board[i][j] == p1:
count2 += 1
elif temp_board[i][j] == p2:
count1 += 1
if block_stat[i*3+j] == p1:
count3 += 1
elif block_stat[i*3+j] == p2:
count4 += 1
utilityarray = define_utility(count1,count2,count3,count4)
utility_p1 += utilityarray[0]
utility_p2 += utilityarray[1]
utility_stat_p1 += utilityarray[2]
utility_stat_p2 += utilityarray[3]
count1 = 0
count2 = 0
count3 = 0
count4 = 0
for j in range(3):
for i in range(3):
if i==j:
if temp_board[i][j] == p1:
count2 += 1
elif temp_board[i][j] == p2:
count1 += 1
if block_stat[i*3+j] == p1:
count3 += 1
elif block_stat[i*3+j] == p2:
count4 += 1
utilityarray = define_utility(count1,count2,count3,count4)
utility_p1 += utilityarray[0]
utility_p2 += utilityarray[1]
utility_stat_p1+= utilityarray[2]
utility_stat_p2 += utilityarray[3]
count1 = 0
count2 = 0
count3 = 0
count4 = 0
for j in range(3):
for i in range(3):
if i==1 and j==1 or i==0 and j ==2 or i==2 and j==0:
if temp_board[i][j] == p1:
count2 += 1
elif temp_board[i][j] == p2:
count1 += 1
if block_stat[i*3+j] == p1:
count3 += 1
elif block_stat[i*3+j] == p2:
count4 += 1
utilityarray = define_utility(count1,count2,count3,count4)
utility_p1 += utilityarray[0]
utility_p2 += utilityarray[1]
utility_stat_p1+= utilityarray[2]
utility_stat_p2 += utilityarray[3]
if (depth%2) == 1:
block_stat[temp_block_cell] = '-'
else:
block_stat[temp_block_cell] = '-'
if utility_p1 < 6 and countp1 == 2 and utility_p2 > -12:
utility_p1 += 250
if utility_p2 <-6 and countp2 ==2 and utility_p1 <12:
utility_p2 -= 250
if utility_p1 < 10 and utility_p2 > -1:
utility_p1 = 1
if utility_p2 > -10 and utility_p1 < 1:
utility_p2 = 0
if utility_stat_p1 < 3 and count_stat_p1 == 2 and utility_stat_p2 > -6:
utility_stat_p1 += 100
if utility_stat_p2 <-3 and count_stat_p2 ==2 and utility_stat_p1 <6:
utility_stat_p2 -= 100
if utility_stat_p1 < 5 and utility_stat_p2 > -0.5:
utility_stat_p1 = 0.5
if utility_stat_p2 > -5 and utility_stat_p1 < 0.5:
utility_stat_p2 = -0.5
utility += utility_p1 + utility_p2 + utility_stat_p1 + utility_stat_p2
# print_lists(board_game,block_stat)
# print "utility = ",utility,"utility_p1 =",utility_p1,"utility_p2 =",utility_p2,"utility_stat = ",utility_stat
return utility
#Gets empty cells from the list of possible blocks. Hence gets valid moves.
def get_empty_out_of(gameb, blal,block_stat):
cells = [] # it will be list of tuples
#Iterate over possible blocks and get empty cells
for idb in blal:
id1 = idb/3 #determine row
id2 = idb%3 # determine column
for i in range(id1*3,id1*3+3):
for j in range(id2*3,id2*3+3):
if gameb[i][j] == '-':
cells.append((i,j))
# If all the possible blocks are full, you can move anywhere
if cells == []:
new_blal = []
all_blal = [0,1,2,3,4,5,6,7,8]
for i in all_blal:
if block_stat[i]=='-':
new_blal.append(i)
for idb in new_blal:
id1 = idb/3
id2 = idb%3
for i in range(id1*3,id1*3+3):
for j in range(id2*3,id2*3+3):
if gameb[i][j] == '-':
cells.append((i,j))
return cells
# determine which blocks are allowed to move-in
def determine_blocks_allowed(old_move, block_stat):
blocks_allowed = []
if 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_blocks_allowed = []
for i in blocks_allowed:
if block_stat[i] == '-':
final_blocks_allowed.append(i)
return final_blocks_allowed
def define_utility(count1,count2,count3,count4):
utility_p1 = utility_p2 = utility_stat_p1= utility_stat_p2 = 0
# game board
if count2 == 3:
utility_p1 += 150
elif count2 == 2:
utility_p1 += 10
if count2 == 2 and count1 == 1:
utility_p1 -= 9.5
elif count2 == 1:
utility_p1 += 1
if count2 == 1 and count1 == 2:
utility_p1 += 50
# opponent
if count1 == 3:
utility_p2 -= 150
elif count1 == 2:
utility_p2 -= 10
if count1 == 2 and count2 == 1:
utility_p2 += 9.5
elif count1 == 1:
utility_p2 -= 0
if count2 == 2 and count1 == 1:
utility_p2 -= 50
# board stat
if count3 == 3:
utility_stat_p1 += 75
elif count3 == 2:
utility_stat_p1 += 5
if count3 == 2 and count4 == 1:
utility_stat_p1 -= 4.25
elif count3 == 1:
utility_stat_p1 += 0.5
if count3 == 1 and count4 == 2:
utility_stat_p1 += 25
if count4 == 3:
utility_stat_p2 -= 75
elif count4 == 2:
utility_stat_p2 -= 5
if count4 == 2 and count3 == 1:
utility_stat_p2 += 4.25
elif count4 == 1:
utility_stat_p2 -= 0
if count3 == 2 and count4 == 1:
utility_stat_p2 -= 25
utilityarray = [utility_p1,utility_p2,utility_stat_p1,utility_stat_p2]
return utilityarray
def print_lists(gb, bs):
command = raw_input("??")
# time.sleep(1)
print '=========== Game Board ==========='
for i in range(9):
if i > 0 and i % 3 == 0:
print
for j in range(9):
if j > 0 and j % 3 == 0:
print " " + gb[i][j],
else:
print gb[i][j],
print
print "=================================="
print "=========== Block Status ========="
for i in range(0, 9, 3):
print bs[i] + " " + bs[i+1] + " " + bs[i+2]
print "=================================="
print
return