-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoard.py
More file actions
268 lines (198 loc) · 8.1 KB
/
Board.py
File metadata and controls
268 lines (198 loc) · 8.1 KB
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
#!/usr/bin/env python
import util
STONE_WHITE = "<white>"
STONE_BLACK = "<black>"
POINT_EMPTY = "<empty>"
'''
class Point:
def __init__(self):
self.val = EMPTY
'''
def predicateWhiteOrEmpty(point):
return point == STONE_WHITE or point == POINT_EMPTY
def predicateBlackOrEmpty(point):
return point == STONE_BLACK or point == POINT_EMPTY
# This acts as the state
class Board:
def __init__(self):
self.size = 9
self.num_points = (self.size)**2
self.points = []
self.territory = []
self.prisoners = []
self.is_terminal = False # set by Game
self.resetBoard()
self.group_imprisoned_points = []
def resetBoard(self):
self.points = []
self.territory = []
for p in range(self.num_points):
self.points.append(POINT_EMPTY)
self.territory.append('o')
self.prisoners.append('o')
# possible moves for white
def getPossibleMoveList(self):
move_list = []
for idx, point in enumerate(self.points):
if point == POINT_EMPTY:
move_list.append(idx)
return move_list
def evaluateBoardState(self):
"""
evaluateBoardState:
returns a tuple of (points_white, points_black)
->points = territory
->does not count prisoners, as this class is only a state representation
->obviously only applicable to terminal states
"""
# current: evaluate territory ( naive function )
white_territory = 0
black_territory = 0
points_white = 0
points_black = 0
prisoners_of_white = 0
prisoners_of_black = 0
for idx, ep in enumerate(self.points):
if ep != POINT_EMPTY:
continue
res = self.returnSurrounded(idx)
if res == STONE_BLACK:
black_territory += 1
self.territory[idx] = 'b'
elif res == STONE_WHITE:
white_territory += 1
self.territory[idx] = 'w'
#Only for checking, currently
for idx, ep in enumerate(self.points):
if ep == POINT_EMPTY:
continue
if self.checkIfPrisoner(idx, ep):
val = 'w' if ep == STONE_WHITE else 'b'
if val == 'w':
prisoners_of_white += 1
else:
prisoners_of_black += 1
self.prisoners[idx] = val
points_white = white_territory
points_black = black_territory
print("ROOOOOOOOOOOOOOOOT", points_white,points_black, prisoners_of_white, prisoners_of_black)
return points_white, points_black, prisoners_of_white, prisoners_of_black
# note: implement this in a separate Game class; does not belong here
# as we need info about player turns and passes
def isGameOver(self):
pass
def returnSurrounded(self, index):
blank_list = [index]
self.getEmptyGroupIndices(index, blank_list)
nb = nw = 0
for blank in blank_list:
num_black, num_white = self.getStoneCountAroundEmpty(blank)
nb += num_black
nw += num_white
if nb == 0:
return STONE_WHITE
elif nw == 0:
return STONE_BLACK
return POINT_EMPTY
def getStoneCountAroundEmpty(self, blank_index):
valid_indices = self.getValidSurroundingIndices(blank_index)
good_indices = [i for i in valid_indices if i is not None]
nb = nw = 0
for ind in good_indices:
if self.points[ind] == STONE_BLACK:
nb += 1
elif self.points[ind] == STONE_WHITE:
nw += 1
return (nb, nw)
def getEmptyGroupIndices(self, index, group_list):
valid_indices = self.getValidSurroundingIndices(index)
good_indices = [i for i in valid_indices if i is not None]
for ind in good_indices:
if self.points[ind] == POINT_EMPTY and ind not in group_list:
group_list.append(ind)
self.getEmptyGroupIndices(ind, group_list) # thank god for mutable containers
return group_list
def getValidSurroundingIndices(self, index):
# Invalid if corresponding edge
# invalid if other row
# invalid if out of range
l = r = u = d = None
l = index - 1
if util.is_left_edge(self.size, index) or (l // self.size != index // self.size) or not util.is_valid_index(self.size, l):
l = None
r = index + 1
if util.is_right_edge(self.size, index) or (r // self.size != index // self.size) or not util.is_valid_index(self.size, r):
r = None
u = index - self.size
if util.is_upper_edge(self.size, index) or not util.is_valid_index(self.size, u):
u = None
d = index + self.size
if util.is_lower_edge(self.size, index) or not util.is_valid_index(self.size, d):
d = None
return (l,r,u,d)
def getSingleLiberties(self, index):
"""
Up, Right, Left, Down.
"""
up_ind = index - self.size
down_ind = index + self.size
left_ind = index - 1
right_ind = index + 1
lib = 0
if util.is_valid_index(self.size, left_ind) and not util.is_left_edge(self.size, index):
if (left_ind // self.size == index // self.size) and self.points[left_ind] == POINT_EMPTY:
lib += 1
if util.is_valid_index(self.size, right_ind) and not util.is_right_edge(self.size, index):
if (right_ind // self.size == index // self.size) and self.points[right_ind] == POINT_EMPTY:
lib += 1
if util.is_valid_index(self.size, up_ind) and not util.is_upper_edge(self.size, index):
if self.points[up_ind] == POINT_EMPTY:
lib += 1
if util.is_valid_index(self.size, down_ind) and not util.is_lower_edge(self.size, index):
if self.points[down_ind] == POINT_EMPTY:
lib += 1
return lib
def getGroupIndices(self, index, stone_color, group_list):
valid_surrounding_indices = self.getValidSurroundingIndices(index)
valid_inds = [i for i in valid_surrounding_indices if i is not None]
for sur_ind in valid_inds:
if self.points[sur_ind] == stone_color and sur_ind not in group_list:
group_list.append(sur_ind)
self.getGroupIndices(sur_ind, stone_color, group_list)
return group_list
def checkIfPrisoner(self, index, stone_color):
"""
Board.checkIfPrisoner
args:
index
stone_color
return value:
True if the stone of color stone_color at index index is a prisoner
"""
if index in self.group_imprisoned_points:
#print("AH, IMMEDAITE HTAAFKASFJASKLFJ S:ALKFJ ")
return True
# Obtain the chain that index is part of, if any
group_list = [index]
group_indices = self.getGroupIndices(index, stone_color, group_list)
total_libs = 0
for ind in group_indices:
total_libs += self.getSingleLiberties(ind)
if total_libs == 0:
for ind in group_indices:
if ind not in self.group_imprisoned_points:
self.group_imprisoned_points.append(ind)
return total_libs == 0
def isTerminalState(self):
return self.terminal
def __copy__(self):
cls = self.__class__
result = cls.__new__(cls)
result.size = self.size
result.num_points = self.num_points
result.points = self.points.copy()
result.territory = self.territory.copy()
result.prisoners = self.prisoners.copy()
result.is_terminal = self.is_terminal
result.group_imprisoned_points = self.group_imprisoned_points.copy()
return result