-
Notifications
You must be signed in to change notification settings - Fork 0
/
BlackEvaluationSearch.py
240 lines (213 loc) · 8.74 KB
/
BlackEvaluationSearch.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
import numpy as np
import math
import operator
import time
FRIEND = 1
ENEMY = -1
EMPTY = 0
class BlackEvaluationSearch():
"""
NOTE:
in this module, all things based on canonical board,
and use coordinate (column, row)
"""
def __init__(self, game, player):
# Player will always be White(1/friend), as we pass in canonical board
self.game = game
self.myColor = player
def timeOut(self):
if abs(time.time() - self.time) > 20:
return True
def search(self, board, turn, curPlayer):
"""
:param board: a objective board
:param turn: the turn
:param curPlayer: the actual color
:return:
"""
board = self.game.getCanonicalForm(board, curPlayer)
action = 0
action = self.AggressivePlacing(board)
if action != 0:
return action
elif turn == 1:
"""
the case my actual color is black
"""
action = self.game.actionRefereeToGame((4, 4))
# board[row][column]
if board[3][4] == ENEMY:
action = self.game.actionRefereeToGame((3, 4))
elif board[4][3] == ENEMY:
action = self.game.actionRefereeToGame((4, 3))
elif board[3][3] == ENEMY:
action = self.game.actionRefereeToGame((4, 4))
elif board[4][4] == ENEMY:
action = self.game.actionRefereeToGame((3, 3))
else:
if board[5][4] == ENEMY or board[4][5] == ENEMY:
action = self.game.actionRefereeToGame((3, 3))
else:
action = self.game.actionRefereeToGame((4, 4))
""" first placing round end"""
return action
elif turn == 3:
if board[3][3] == -1 and board[5][4] == -1:
result = (3,2)
action = self.game.actionRefereeToGame(result)
elif board[3][4] == -1 and board[5][3] == -1:
result = (4,2)
action = self.game.actionRefereeToGame(result)
elif board[4][4] == 1:
possible_places = [(4, 2), (3, 2), (3, 3), (4, 3), (3, 4)] # Column, row
for (column, row) in possible_places:
if board[row][column] == EMPTY:
if not self.dangerousPlace(board, (column, row)):
result = (column, row)
break
action = self.game.actionRefereeToGame(result)
else:
action = self.evaluateBoard(board)
return action
elif turn == 7:
if board[3][3] == -1 and board[5][3] == -1 and board[5][4] == -1 and board[2][4] == 1 and board[4][4] == 1:
result = (5,4)
action = self.game.actionRefereeToGame(result)
elif board[3][4] == -1 and board[5][4] == -1 and board[5][3] == -1 and board[4][3] == 1 and board[2][3] == 1:
result = (2,4)
action = self.game.actionRefereeToGame(result)
else:
action = self.evaluateBoard(board)
return action
action = self.evaluateBoard(board)
return action
def AggressivePlacing(self, board):
possibleMoves = {}
moves = [(-1, 0), (1, 0), (0, -1), (0, 1)]
for i, row in enumerate(board):
for j, x in enumerate(row):
if x == FRIEND:
for move in moves:
i_dir, j_dir = move
if 0 <= i + 2 * i_dir < 6 and 0 <= j + 2 * j_dir < 8:
if board[i + i_dir][j + j_dir] == ENEMY and board[i + 2 * i_dir][j + 2 * j_dir] == 0:
action = self.game.actionRefereeToGame((j + 2 * j_dir, i + 2 * i_dir))
possibleMoves[action] = i+2*i_dir
if board[0][1] == 3 and board[0][2] == 0:
possibleMoves[self.game.actionRefereeToGame((0, 2))] = 2
if board[7][1] == 3 and board[7][2] == 0:
possibleMoves[self.game.actionRefereeToGame((7, 2))] = 2
if board[0][6] == 3 and board[0][5] == 0:
possibleMoves[self.game.actionRefereeToGame((0, 5))] = 5
if board[7][6] == 3 and board[7][5] == 0:
possibleMoves[self.game.actionRefereeToGame((7, 5))] = 5
try:
action = min(possibleMoves, key = possibleMoves.get)
except:
action = 0
return action
def dangerousPlace(self, board, action):
"""
:param action: (column, row)
:return:
"""
j, i = action
if j < 2:
return False
moves = [(-1, 0), (1, 0), (0, -1), (0, 1)]
for move in moves:
i_dir, j_dir = move
if 0 <= i + i_dir < 8 and 0 <= j + j_dir < 8:
if (board[i + i_dir][j + j_dir] == -self.myColor or board[i + i_dir][j + j_dir] == 3) and board[i - i_dir][j - j_dir] != self.myColor and i - i_dir > 2:
return True
return False
def evaluateBoard(self, board):
"""
:param board: a canonical board
:return: an optimal pos
"""
results = np.zeros([8,4]) # col row
i = 16
while i < 48:
col, row = i%8, i//8
if i < 24:
results[col][row-2] += 2
if board[row][col]==1:
results = self.updateSides(results, (col,row), 1)
results = self.updateCorners(results, (col, row), 1)
elif board[row][col]==-1:
results = self.updateSides(results, (col,row), -2)
results = self.updateSidess(results, (col,row), 1)
results = self.updateCorners(results, (col, row), 0.5)
results = self.updateDefence(board, results, (col, row), 2)
results = self.updateTake(board, results, (col, row), 3)
results[col][row-2] -= self.distanceToCenter((col,row))
i +=1
valids = self.game.getValidMoves(board, 1)
# print(np.array(results).T)
for i in range(len(valids)):
if 15< i < 48 and not valids[i]:
col, row = i%8, i//8
col, row = col, row-2
# print(col,row)
results[col, row] = 0
max = -100
for col, x in enumerate(results):
for row, y in enumerate(x):
if y>max:
max = y
action = col, row+2
return self.game.actionRefereeToGame((action))
def updateSides(self, results, pos, value):
sides = [(1,0),(-1,0),(0,1),(0,-1)]
col, row = pos
col, row = col, row - 2
for dir in sides:
col_dir, row_dir = dir
if 0 <= col+col_dir < 8 and 0<= row+row_dir < 4:
results[col+col_dir][row+row_dir] += value
return results
def updateSidess(self, results, pos, value):
sides = [(1,0),(-1,0),(0,1),(0,-1)]
col, row = pos
col, row = col, row - 2
for dir in sides:
col_dir, row_dir = dir
if 0 <= col+2*col_dir < 8 and 0<= row+2*row_dir < 4:
results[col+2*col_dir][row+2*row_dir] += value
return results
def updateCorners(self, results, pos, value):
sides = [(1,1),(-1,1),(1,-1),(-1,-1)]
col, row = pos
col, row = col, row - 2
for dir in sides:
col_dir, row_dir = dir
if 0 <= col+col_dir < 8 and 0<= row+row_dir < 4:
results[col+col_dir][row+row_dir] += value
return results
def updateDefence(self, board, results, pos, value):
sides = [(1,0),(-1,0),(0,1),(0,-1)]
col, row = pos
res_col, res_row = col, row - 2
for dir in sides:
col_dir, row_dir = dir
if 0 <= res_col+2*col_dir < 8 and 0<= res_row+2*row_dir < 4 and board[row+row_dir][col+col_dir] == 1:
results[res_col+2*col_dir][res_row+2*row_dir] += value
return results
def updateTake(self, board, results, pos, value):
sides = [(1,0),(-1,0),(0,1),(0,-1)]
col, row = pos
res_col, res_row = col, row - 2
for dir in sides:
col_dir, row_dir = dir
if 0 <= res_col+col_dir < 8 or 0<= res_row+row_dir < 4:
continue
if 0 <= res_col+2*col_dir < 8 or 0<= res_row+2*row_dir < 4 or board[row+2*row_dir][col+2*col_dir] == 1:
results[res_col+col_dir][res_row+row_dir] += value
return results
def distanceToCenter(self, pos):
x,y = pos
return 0.1*math.sqrt((x-3.5)**2 + (y-3.5)**2)
def distanceToCenter(self, pos):
x,y = pos
return 0.1*math.sqrt((x-3.5)**2 + (y-3.5)**2)