-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathteam8.py
587 lines (527 loc) · 22.9 KB
/
team8.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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
"""
Class for AI bot
"""
import random
import traceback
import sys
import numpy as np
from time import time
from copy import deepcopy
class Team8:
"""
AI implemented
"""
def __init__(self):
"""
Initialize variables
"""
self.default=(0,4,4)#default move
self.limit=20#time limit
self.start=0#start time
self.player=0#x=1 o=0
self.opponent=0
self.bestmv=(0,0,0)
self.inf = 1000
self.bot_player = 1
self.startdepth=1
self.movecounter = 0
self.maxdepth=9*9*9
self.bonus_move_cur = [0 , 0]
self.last_function_call = 0
self.numsteps = 0
self.dict = {}
self.player_char = ['o', 'x']
self.blockweight=[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
# self.blockweight=[1,.9,1,.9,1.1,.9,1,.9,1,1,.9,1,.9,1.1,.9,1,.9,1]
def smallboardutility(self,board,move):
"""
Find weights of each cell
"""
board_num = move[0]
row_num = 3*move[1]
col_num = 3*move[2]
cur_big_board = board.big_boards_status[board_num]
winning_comb = []
winning_comb.append(np.zeros(4)) #0 for bot
winning_comb.append(np.zeros(4)) #1 for opposition
bot_win_cells = 0
opp_win_cells = 0
win_chance = np.ones(2)
prob_chance = np.zeros(2)
for i in range(3):
bot_win_cells = 0
opp_win_cells = 0
win_chance = np.ones(2)
prob_chance = np.zeros(2)
for j in range(3):
curtictoc = cur_big_board[row_num+i][col_num+j]
if curtictoc == self.player_char[self.bot_player]:
win_chance[1] = 0
bot_win_cells+=1
if curtictoc == self.player_char[self.bot_player^1]:
win_chance[0] = 0
opp_win_cells+=1
if win_chance[0] == 1:
winning_comb[0][bot_win_cells]+=1
if win_chance[1] == 1:
winning_comb[1][opp_win_cells]+=1
bot_win_cells = 0
opp_win_cells = 0
win_chance = np.ones(2)
prob_chance = np.zeros(2)
for i in range(3):
bot_win_cells = 0
opp_win_cells = 0
win_chance = np.ones(2)
prob_chance = np.zeros(2)
for j in range(3):
curtictoc = cur_big_board[row_num+j][col_num+i]
if curtictoc == self.player_char[self.bot_player]:
win_chance[1] = 0
bot_win_cells+=1
if curtictoc == self.player_char[self.bot_player^1]:
win_chance[0] = 0
opp_win_cells+=1
if win_chance[0] == 1:
winning_comb[0][bot_win_cells]+=1
if win_chance[1] == 1:
winning_comb[1][opp_win_cells]+=1
bot_win_cells = 0
opp_win_cells = 0
win_chance = np.ones(2)
prob_chance = np.zeros(2)
for i in range(3):
#for j in range(3):
curtictoc = cur_big_board[row_num+i][col_num+i]
if curtictoc == self.player_char[self.bot_player]:
win_chance[1] = 0
bot_win_cells+=1
if curtictoc == self.player_char[self.bot_player^1]:
win_chance[0] = 0
opp_win_cells+=1
if win_chance[0] == 1:
winning_comb[0][bot_win_cells]+=1
if win_chance[1] == 1:
winning_comb[1][opp_win_cells]+=1
bot_win_cells = 0
opp_win_cells = 0
win_chance = np.ones(2)
prob_chance = np.zeros(2)
for i in range(3):
#for j in range(3):
curtictoc = cur_big_board[row_num+i][col_num+2-i]
if curtictoc == self.player_char[self.bot_player]:
win_chance[1] = 0
bot_win_cells+=1
if curtictoc == self.player_char[self.bot_player^1]:
win_chance[0] = 0
opp_win_cells+=1
if win_chance[0] == 1:
winning_comb[0][bot_win_cells]+=1
if win_chance[1] == 1:
winning_comb[1][opp_win_cells]+=1
small_util = 1.2*(winning_comb[0][1] -winning_comb[1][1]) + 4*(winning_comb[0][2] - winning_comb[1][2])
if winning_comb[0][3] == 1:
small_util = 17
if winning_comb[1][3] == 1:
small_util = -17
return(small_util)
def blockutilityondraw(self,board):
"""
Calculate utility of each cell of big board
"""
draw_util = 0
bot = self.player_char[self.bot_player]
opp = self.player_char[self.bot_player^1]
cur_small_board = board.small_boards_status
for m in range(2):
for i in range(3):
for j in range(3):
if i%2 == 0 and j%2 == 0 and cur_small_board[m][i][j] == self.player_char[self.bot_player]:
draw_util += 4
elif i%2 == 0 and j%2 == 0 and cur_small_board[m][i][j] == self.player_char[self.bot_player^1]:
draw_util -= 4
elif i%2 == 1 and j%2 == 1 and cur_small_board[m][i][j] == self.player_char[self.bot_player]:
draw_util +=3
elif i%2 == 1 and j%2 == 1 and cur_small_board[m][i][j] == self.player_char[self.bot_player^1]:
draw_util -=3
elif ((i%2 == 0 and j%2 ==1) or (i%2 ==1 and j%2 == 0)) and cur_small_board[m][i][j] == self.player_char[self.bot_player^1]:
draw_util -=6
elif ((i%2 == 0 and j%2 ==1) or (i%2 ==1 and j%2 == 0)) and cur_small_board[m][i][j] == self.player_char[self.bot_player]:
draw_util +=6
return draw_util
def check_prob_win_big(self,board,board_num,row_num,col_num,player):
cur_big_board = board.big_boards_status[board_num]
for i in range(3):
player_comb = 0
for j in range(3):
if (cur_big_board[3*row_num+i][3*col_num+j] == self.player_char[player]
or cur_big_board[3*row_num+i][3*col_num+j] == '-'):
player_comb = 1
else:
player_comb = 0
break
if player_comb == 1:
return 1
for i in range(3):
player_comb = 0
for j in range(3):
if (cur_big_board[3*row_num+j][3*col_num+i] == self.player_char[player]
or cur_big_board[3*row_num+j][3*col_num+i] == '-'):
player_comb = 1
else:
player_comb = 0
break
if player_comb == 1:
return 1
for i in range(3):
#for j in range(3):
player_comb = 0
if (cur_big_board[3*row_num+i][3*col_num+i] == self.player_char[player]
or cur_big_board[3*row_num+i][3*col_num+i] == '-'):
player_comb = 1
else:
player_comb = 0
break
if player_comb == 1:
return 1
for i in range(3):
if (cur_big_board[3*row_num+i][3*col_num+2-i] == self.player_char[player]
or cur_big_board[3*row_num+i][3*col_num+2-i] == '-'):
player_comb = 1
else:
player_comb = 0
break
if player_comb ==1:
return 1
return 0
def utility(self,board,symbol):
"""
Heuristic function
"""
utility=0
util_bot = 0
util_opp = 0
# self.blockweight=[1,.9,1,.9,1.1,.9,1,.9,1,1,.9,1,.9,1.1,.9,1,.9,1]
self.blockweight=[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
winning_comb = []
winning_comb.append(np.zeros(4)) #0 for bot
winning_comb.append(np.zeros(4)) #1 for opposition
for m in range(2):
for i in range(3):
bot_win_cells = 0
opp_win_cells = 0
win_chance = np.ones(2)
prob_chance = np.zeros(2)
lookat = []
for j in range(3):
if board.small_boards_status[m][i][j] == self.player_char[self.bot_player]:
win_chance[1] = 0
bot_win_cells+=1
if board.small_boards_status[m][i][j] == self.player_char[self.bot_player^1]:
win_chance[0] = 0
opp_win_cells+=1
if board.small_boards_status[m][i][j] == 'd':
win_chance[0] = 0
win_chance[1] = 0
if board.small_boards_status[m][i][j] == '-':
prob_chance[0] = self.check_prob_win_big(board,m,i,j,self.bot_player)
prob_chance[1] = self.check_prob_win_big(board,m,i,j,self.bot_player^1)
if prob_chance[0] == 1 and prob_chance[1] == 1:
lookat.append([m,i,j])
if prob_chance[0] == 0:
win_chance[0] = 0
if prob_chance[1] == 0:
win_chance[1] = 0
if win_chance[0] == 1:
winning_comb[0][bot_win_cells] +=1
if win_chance[1] == 1:
winning_comb[1][opp_win_cells] +=1
if win_chance[0]==1 or win_chance[1] ==1:
for z in range(len(lookat)):
utility+=self.smallboardutility(board,lookat[z])
#winning_comb.append(np.zeros(4)) #0 for bot
#winning_comb.append(np.zeros(4)) #1 for opposition
bot_win_cells = 0
opp_win_cells = 0
win_chance = np.ones(2)
prob_chance = np.zeros(2)
#for m in range(2):
for i in range(3):
bot_win_cells = 0
opp_win_cells = 0
win_chance = np.ones(2)
prob_chance = np.zeros(2)
lookat = []
for j in range(3):
if board.small_boards_status[m][j][i] == self.player_char[self.bot_player]:
win_chance[1] = 0
bot_win_cells+=1
if board.small_boards_status[m][j][i] == self.player_char[self.bot_player^1]:
win_chance[0] = 0
opp_win_cells+=1
if board.small_boards_status[m][j][i] == 'd':
win_chance[0] = 0
win_chance[1] = 0
if board.small_boards_status[m][j][i] == '-':
prob_chance[0] = self.check_prob_win_big(board,m,j,i,self.bot_player)
prob_chance[1] = self.check_prob_win_big(board,m,j,i,self.bot_player^1)
if prob_chance[0] == 1 and prob_chance[1] == 1:
lookat.append([m,j,i])
if prob_chance[0] == 0:
win_chance[0] = 0
if prob_chance[1] == 0:
win_chance[1] = 0
if win_chance[0] == 1:
winning_comb[0][bot_win_cells] +=1
if win_chance[1] == 1:
winning_comb[1][opp_win_cells] +=1
if win_chance[0]==1 or win_chance[1] ==1:
for z in range(len(lookat)):
utility+=self.smallboardutility(board,lookat[z])
#winning_comb.append(np.zeros(4)) #0 for bot
#winning_comb.append(np.zeros(4)) #1 for opposition
bot_win_cells = 0
opp_win_cells = 0
win_chance = np.ones(2)
prob_chance = np.zeros(2)
#for m in range(2):
bot_win_cells = 0
opp_win_cells = 0
win_chance = np.ones(2)
prob_chance = np.zeros(2)
for i in range(3):
#for j in range(3):
if board.small_boards_status[m][i][i] == self.player_char[self.bot_player]:
win_chance[1] = 0
bot_win_cells+=1
if board.small_boards_status[m][i][i] == self.player_char[self.bot_player^1]:
win_chance[0] = 0
opp_win_cells+=1
if board.small_boards_status[m][i][i] == 'd':
win_chance[0] = 0
win_chance[1] = 0
if board.small_boards_status[m][i][i] == '-':
prob_chance[0] = self.check_prob_win_big(board,m,i,i,self.bot_player)
prob_chance[1] = self.check_prob_win_big(board,m,i,i,self.bot_player^1)
if prob_chance[0] == 1 and prob_chance[1] == 1:
lookat.append([m,i,i])
if prob_chance[0] == 0:
win_chance[0] = 0
if prob_chance[1] == 0:
win_chance[1] = 0
if win_chance[0] == 1:
winning_comb[0][bot_win_cells] +=1
if win_chance[1] == 1:
winning_comb[1][opp_win_cells] +=1
if win_chance[0]==1 or win_chance[1] ==1:
for z in range(len(lookat)):
utility+=self.smallboardutility(board,lookat[z])
#winning_comb.append(np.zeros(4)) #0 for bot
#winning_comb.append(np.zeros(4)) #1 for opposition
bot_win_cells = 0
opp_win_cells = 0
win_chance = np.ones(2)
prob_chance = np.zeros(2)
#for m in range(2):
bot_win_cells = 0
opp_win_cells = 0
win_chance = np.ones(2)
prob_chance = np.zeros(2)
lookat = []
for i in range(3):
#for j in range(3):
if board.small_boards_status[m][i][2-i] == self.player_char[self.bot_player]:
win_chance[1] = 0
bot_win_cells+=1
if board.small_boards_status[m][i][2-i] == self.player_char[self.bot_player^1]:
win_chance[0] = 0
opp_win_cells+=1
if board.small_boards_status[m][i][2-i] == 'd':
win_chance[0] = 0
win_chance[1] = 0
if board.small_boards_status[m][i][2-i] == '-':
prob_chance[0] = self.check_prob_win_big(board,m,i,2-i,self.bot_player)
prob_chance[1] = self.check_prob_win_big(board,m,i,2-i,self.bot_player^1)
if prob_chance[0] == 1 and prob_chance[1] == 1:
lookat.append([m,i,2-i])
if prob_chance[0] == 0:
win_chance[0] = 0
if prob_chance[1] == 0:
win_chance[1] = 0
if win_chance[0] == 1:
winning_comb[0][bot_win_cells] +=1
if win_chance[1] == 1:
winning_comb[1][opp_win_cells] +=1
if win_chance[0]==1 or win_chance[1] ==1:
for z in range(len(lookat)):
utility+=self.smallboardutility(board,lookat[z])
utility += 17* (winning_comb[0][1] -winning_comb[1][1]) + 134* (winning_comb[0][2] - winning_comb[1][2])
if winning_comb[0][3] == 1:
utility = self.inf
if winning_comb[1][3] == 1:
utility = -self.inf
return(utility)
def prunealphabeta(self,board,depth,player,player_move,alphaplayer,betaplayer,prev):
"""
minimax+alphabeta
"""
if board.find_terminal_state() == (self.player_char[self.bot_player],'WON'):
return self.inf
if board.find_terminal_state() == (self.player_char[self.bot_player^1],'WON'):
return -self.inf
if board.find_terminal_state() == ('NONE','DRAW'):
return self.blockutilityondraw(board)
if depth == 0:
return self.utility(board,self.player_char[prev])
if time() - self.start > self.limit:
return self.utility(board,self.player_char[prev])
moves_available = board.find_valid_move_cells(player_move)
tempbonusmove = self.bonus_move_cur[player]
if player == self.bot_player :
cur_utility = -self.inf
else:
cur_utility = self.inf
#random.shuffle(moves_available)
arr2 = []
for moves in moves_available:
sort_temp_board = deepcopy(board)
sort_temp_board.update(player_move,moves,self.player_char[self.bot_player])
arr2.append((self.utility(sort_temp_board,self.player_char[self.bot_player]),moves))
self.nextmoves = sorted(arr2,key= lambda x:x[0])
self.nextmoves.reverse()
myarr = [x[1] for x in self.nextmoves]
for moves in myarr:
self.bonus_move_cur[player] = tempbonusmove
tempboard = deepcopy(board)
gamepos,status = tempboard.update(player_move,moves,self.player_char[player])
if not status: #and self.bonus_move_cur[player] == 0:
self.bonus_move_cur[player] = 0
elif status:
self.bonus_move_cur[player] ^= 1
if player == self.bot_player: #bot playing
if status and self.bonus_move_cur[player] == 1: #bot has bonus
max_minmax = self.prunealphabeta(tempboard,depth-1,
player,moves,alphaplayer,betaplayer,player)
cur_utility = max(cur_utility,max_minmax)
self.bonus_move_cur[player] = 0
else:
max_minmax = self.prunealphabeta(tempboard,depth-1,
player^1,moves,alphaplayer,betaplayer,player)
cur_utility = max(cur_utility,max_minmax)
alphaplayer = max(alphaplayer,cur_utility) #recursive minmax
else:
if status and self.bonus_move_cur[player] == 1: #opp has bonus
min_minmax = self.prunealphabeta(tempboard,depth-1,
player,moves,alphaplayer,betaplayer,player)
cur_utility = min(cur_utility,min_minmax)
self.bonus_move_cur[player] = 0
else:
min_minmax = self.prunealphabeta(tempboard,depth-1,
player^1,moves,alphaplayer,betaplayer,player)
cur_utility = min(cur_utility,min_minmax)
betaplayer = min(betaplayer,cur_utility) #recursive minmax
if(betaplayer <= alphaplayer):
break
if time() - self.start > self.limit:
break
self.bonus_move_cur[player] = tempbonusmove
return cur_utility
def alphabetamove(self,board,old_move,player,depth):
"""
Preprocessing for alphaplayer betaplayer algorithm
"""
#find all possible moves
arr = board.find_valid_move_cells(old_move)
#tells if player has bonus move
tempbonusmove = self.bonus_move_cur[player]
#initialise maximum value
curmax = -self.inf-1
arr2 = []
for moves in arr:
sort_temp_board = deepcopy(board)
sort_temp_board.update(old_move,moves,self.player_char[player])
arr2.append((self.utility(sort_temp_board,self.player_char[player]),moves))
self.nextmoves = sorted(arr2,key= lambda x:x[0])
self.nextmoves.reverse()
myarr = [x[1] for x in self.nextmoves]
for moves in myarr:
self.bonus_move_cur[player] = tempbonusmove
#checks if any cell won
tempboard = deepcopy(board)
gamepos,status = tempboard.update(old_move,moves,self.player_char[player])
if not status:
self.bonus_move_cur[player] = 0
elif status:
self.bonus_move_cur[player] ^= 1
#check utility after winning
if status and (self.bonus_move_cur[player] == 1):
player_utility = self.prunealphabeta(tempboard,depth,player,moves,-self.inf,self.inf,player)#same player moves
else:
player_utility = self.prunealphabeta(tempboard,depth,player^1,moves,-self.inf,self.inf,player)#opponent moves
#restore the board states
#board.big_boards_status[moves[0]][moves[1]][moves[2]] = "-"
#board.small_boards_status[moves[0]][moves[1]/3][moves[2]/3] = "-"
if player_utility > curmax:
cur_best_move = moves
curmax = player_utility
self.bonus_move_cur[player] = tempbonusmove
return cur_best_move
def idfs(self,board,oldmv,tree_level,player):
"""
idfs returns best move
"""
#if(time()-self.start)>self.limit:
# break
output = self.alphabetamove(board,oldmv,player,self.startdepth)
return output
def move(self,gameboard,oldmove,symbol):
"""
Main code
"""
#start timer
self.start=time()
self.movecounter+=1
if(self.movecounter > 17 ):
self.startdepth+=1
self.movecounter = 0
#initialising players
if symbol=='x':
self.bot_player=1
self.player=1
self.opponent=0
else:
self.bot_player=0
self.player=0
self.opponent=1
try:
#if first move
if oldmove == (-1,-1,-1):
return self.default
#calculate from hash tables
depth=1
tempboard=deepcopy(gameboard)
self.bonus_move_cur=[0,0]
if(self.last_function_call):
self.bonus_move_cur[self.bot_player]^=1
else:
self.bonus_move_cur[self.bot_player]=0
tempmove=self.idfs(tempboard,oldmove,depth,self.player)
last_check_board = deepcopy(tempboard)
gamepos,status=last_check_board.update(oldmove,tempmove,self.player_char[self.player])
if not status:
self.last_function_call = 0
elif status:
self.last_function_call ^=1
#restore the board states
#tempboard.big_boards_status[tempmove[0]][tempmove[1]][tempmove[2]] = "-"
#tempboard.small_boards_status[tempmove[0]][tempmove[1]/3][tempmove[2]/3] = "-"
#print time()-self.start
cells = gameboard.find_valid_move_cells(oldmove)
if tempmove not in cells:
return cells[random.randrange(len(cells))]
return tempmove
except Exception as e:
print 'Exception occurred ', e
print 'Traceback printing ', sys.exc_info()
print traceback.format_exc()