-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathteam42.py
246 lines (202 loc) · 6.06 KB
/
team42.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
import sys
import random
import signal
import time
import copy
INF = float("inf")
class Player42:
# The team's entire code to be written under this class
def __init__(self):
pass
def evaluate(self,board):
return 1
def evaluate1(self,board):
heu = 0
blockmap = ( 0 , 1 , 10 , 100 , 1000 )
boardmap = ( 0 , 1000 , 10000 , 100000, 1000000 )
blockh = [[ 0 for i in range(4)] for j in range(4)]
for i in range(4):
for j in range(4):
#along rows
for u in range(4*i, 4*i+4):
ocount = 0
xcount = 0
for v in range(4*j, 4*j+4):
if board.board_status[u][v] == 'o':
ocount+= 1
elif board.board_status[u][v] == 'x':
xcount+= 1
if xcount > 0 and ocount == 0:
blockh[i][j] += blockmap[xcount]
elif xcount == 0 and ocount > 0 :
blockh[i][j] -= blockmap[ocount]
#along colomns
for u in range(4*j, 4*j + 4):
ocount = 0
xcount = 0
for v in range(4*i , 4*i + 4):
if board.board_status[v][u] == 'o':
ocount+= 1
elif board.board_status[v][u] == 'x':
xcount+= 1
if xcount > 0 and ocount == 0:
blockh[i][j] += blockmap[xcount]
elif xcount == 0 and ocount > 0 :
blockh[i][j] -= blockmap[ocount]
#along diagnal
xcount = 0
ocount = 0
for u in range(4):
if board.board_status[4*i + u][4*j + u] == 'o':
ocount+= 1
elif board.board_status[4*i + u][4*j + u] == 'x':
xcount+= 1
if xcount > 0 and ocount == 0:
blockh[i][j] += blockmap[xcount]
elif xcount == 0 and ocount > 0 :
blockh[i][j] -= blockmap[ocount]
#along another diagnal
xcount = 0
ocount = 0
for u in range(4):
if board.board_status[ 4*i + u][4*j + 3 - u] == 'o':
ocount+= 1
elif board.board_status[4*i + u][4*j + 3 -u] == 'x':
xcount+= 1
if xcount > 0 and ocount == 0:
blockh[i][j] += blockmap[xcount]
elif xcount == 0 and ocount > 0 :
blockh[i][j] -= blockmap[ocount]
#find xmax , omax
#along rows
for u in range(4):
ocount = 0
xcount = 0
for v in range(4):
if board.block_status[u][v] == 'o':
ocount+= 1
elif board.block_status[u][v] == 'x':
xcount+= 1
if not(xcount > 0 and ocount > 0):
if xcount == 0 and ocount > 0 :
heu -= boardmap[ocount]
elif xcount > 0 and ocount == 0:
heu += boardmap[xcount]
for v in range(4):
if board.block_status[u][v] == '-':
heu += blockh[u][v]
#along colomns
for u in range(4):
ocount = 0
xcount = 0
for v in range(4):
if board.block_status[v][u] == 'o':
ocount+= 1
elif board.block_status[v][u] == 'x':
xcount+= 1
if not(xcount > 0 and ocount > 0):
if xcount == 0 and ocount > 0 :
heu -= boardmap[ocount]
elif xcount > 0 and ocount == 0:
heu += boardmap[xcount]
for v in range(4):
if board.block_status[v][u] == '-':
heu += blockh[v][u]
#along diagnal
xcount = 0
ocount = 0
for u in range(4):
if board.block_status[u][u] == 'o':
ocount+= 1
elif board.block_status[u][u] == 'x':
xcount+= 1
if not(xcount > 0 and ocount > 0):
if xcount == 0 and ocount > 0 :
heu -= boardmap[ocount]
elif xcount > 0 and ocount == 0:
heu += boardmap[xcount]
for v in range(4):
if board.block_status[v][v] == '-':
heu += blockh[v][v]
#along another diagnal
xcount = 0
ocount = 0
for u in range(4):
if board.block_status[u][3 - u] == 'o':
ocount+= 1
elif board.block_status[u][3 -u] == 'x':
xcount+= 1
if not(xcount > 0 and ocount > 0):
if xcount == 0 and ocount > 0 :
heu -= boardmap[ocount]
elif xcount > 0 and ocount == 0:
heu += boardmap[xcount]
for v in range(4):
if board.block_status[v][3 -v] == '-':
heu += blockh[v][3 -v]
return heu
def minimax(self, board, old_move, present_d, depth, flag, alpha, beta):
status = board.find_terminal_state()
if (present_d == depth) or (status[1] == 'WON') or (status[1] == 'DRAW'):
return self.evaluate1(board)
if flag=='x':
best = -10000000 # -10^7
cells = board.find_valid_move_cells(old_move)
if len(cells) == 0:
return self.evaluate1(board)
if len(cells) > 16 and depth - present_d >= 3:
#print "len cells ",len(cells)
#print
present_d += 1
for cell in cells:
#print "ok ",cell[0],cell[1]
#print
board.board_status[cell[0]][cell[1]] = flag
best = max(best,self.minimax(board,(cell[0],cell[1]),present_d+1,depth,chr(ord('x')+ord('o')-ord(flag)),alpha,beta))
board.board_status[cell[0]][cell[1]] = '-'
alpha = max(alpha, best)
if (beta <= alpha):
break
return best
elif flag=='o':
best = 10000000 #10^7
cells = board.find_valid_move_cells(old_move)
if len(cells) == 0:
return self.evaluate1(board)
if len(cells) > 16 and depth - present_d >= 3:
present_d += 1
for cell in cells:
board.board_status[cell[0]][cell[1]] = flag
best = min(best,self.minimax(board,(cell[0],cell[1]),present_d+1,depth,chr(ord('x')+ord('o')-ord(flag)),alpha,beta))
board.board_status[cell[0]][cell[1]] = '-'
beta = min(beta, best)
if (beta <= alpha):
break
return best
def move(self, board, old_move, flag):
cells = board.find_valid_move_cells(old_move) #Found all the possible steps and need to choose one from them
if (flag=='x'):
bestval = -10000000 # -10^7
elif (flag=='o'):
bestval = 10000000 # 10^7
bestrow = -1
bestcol = -1
depth = 4
if len(cells) > 16:
depth = 3
for cell in cells:
board.board_status[cell[0]][cell[1]] = flag
old_move = (cell[0],cell[1])
moveval = self.minimax(board, old_move, 1, depth, chr(ord('x')+ord('o')-ord(flag)), -INF, INF)
board.board_status[cell[0]][cell[1]] = '-'
if flag=='x': # maximizer
if moveval>bestval:
bestval = moveval
bestrow = cell[0]
bestcol = cell[1]
elif flag=='o': # minimizer
if moveval<bestval:
bestval = moveval
bestrow = cell[0]
bestcol = cell[1]
return (bestrow,bestcol)