-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboard.py
376 lines (344 loc) · 12.1 KB
/
board.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
"""
board.py
Implements a basic Go board with functions to:
- initialize to a given board size
- check if a move is legal
- play a move
The board uses a 1-dimensional representation with padding
"""
import numpy as np
from board_util import (
GoBoardUtil,
BLACK,
WHITE,
EMPTY,
BORDER,
PASS,
is_black_white,
is_black_white_empty,
coord_to_point,
where1d,
MAXSIZE,
GO_POINT
)
"""
The GoBoard class implements a board and basic functions to play
moves, check the end of the game, and count the acore at the end.
The class also contains basic utility functions for writing a Go player.
For many more utility functions, see the GoBoardUtil class in board_util.py.
The board is stored as a one-dimensional array of GO_POINT in self.board.
See GoBoardUtil.coord_to_point for explanations of the array encoding.
"""
class GoBoard(object):
def __init__(self, size):
"""
Creates a Go board of given size
"""
assert 2 <= size <= MAXSIZE
self.reset(size)
self.calculate_rows_cols_diags()
def calculate_rows_cols_diags(self):
if self.size < 5:
return
# precalculate all rows, cols, and diags for 5-in-a-row detection
self.rows = []
self.cols = []
for i in range(1, self.size + 1):
current_row = []
start = self.row_start(i)
for pt in range(start, start + self.size):
current_row.append(pt)
self.rows.append(current_row)
start = self.row_start(1) + i - 1
current_col = []
for pt in range(start, self.row_start(self.size) + i, self.NS):
current_col.append(pt)
self.cols.append(current_col)
self.diags = []
# diag towards SE, starting from first row (1,1) moving right to (1,n)
start = self.row_start(1)
for i in range(start, start + self.size):
diag_SE = []
pt = i
while self.get_color(pt) == EMPTY:
diag_SE.append(pt)
pt += self.NS + 1
if len(diag_SE) >= 5:
self.diags.append(diag_SE)
# diag towards SE and NE, starting from (2,1) downwards to (n,1)
for i in range(start + self.NS, self.row_start(self.size) + 1, self.NS):
diag_SE = []
diag_NE = []
pt = i
while self.get_color(pt) == EMPTY:
diag_SE.append(pt)
pt += self.NS + 1
pt = i
while self.get_color(pt) == EMPTY:
diag_NE.append(pt)
pt += -1 * self.NS + 1
if len(diag_SE) >= 5:
self.diags.append(diag_SE)
if len(diag_NE) >= 5:
self.diags.append(diag_NE)
# diag towards NE, starting from (n,2) moving right to (n,n)
start = self.row_start(self.size) + 1
for i in range(start, start + self.size):
diag_NE = []
pt = i
while self.get_color(pt) == EMPTY:
diag_NE.append(pt)
pt += -1 * self.NS + 1
if len(diag_NE) >=5:
self.diags.append(diag_NE)
assert len(self.rows) == self.size
assert len(self.cols) == self.size
assert len(self.diags) == (2 * (self.size - 5) + 1) * 2
def reset(self, size):
"""
Creates a start state, an empty board with given size.
"""
self.size = size
self.NS = size + 1
self.WE = 1
self.ko_recapture = None
self.last_move = None
self.last2_move = None
self.current_player = BLACK
self.maxpoint = size * size + 3 * (size + 1)
self.board = np.full(self.maxpoint, BORDER, dtype=GO_POINT)
self._initialize_empty_points(self.board)
self.calculate_rows_cols_diags()
def copy(self):
b = GoBoard(self.size)
assert b.NS == self.NS
assert b.WE == self.WE
b.ko_recapture = self.ko_recapture
b.last_move = self.last_move
b.last2_move = self.last2_move
b.current_player = self.current_player
assert b.maxpoint == self.maxpoint
b.board = np.copy(self.board)
return b
def get_color(self, point):
return self.board[point]
def undo(self, point):
self.board[point] = EMPTY
self.current_player = GoBoardUtil.opponent(self.current_player)
def pt(self, row, col):
return coord_to_point(row, col, self.size)
def is_legal(self, point, color):
"""
Check whether it is legal for color to play on point
This method tries to play the move on a temporary copy of the board.
This prevents the board from being modified by the move
"""
board_copy = self.copy()
can_play_move = board_copy.play_move(point, color)
return can_play_move
def get_empty_points(self):
"""
Return:
The empty points on the board
"""
return where1d(self.board == EMPTY)
def get_color_points(self, color):
"""
Return:
All points of color on the board
"""
return where1d(self.board == color)
def row_start(self, row):
assert row >= 1
assert row <= self.size
return row * self.NS + 1
def _initialize_empty_points(self, board):
"""
Fills points on the board with EMPTY
Argument
---------
board: numpy array, filled with BORDER
"""
for row in range(1, self.size + 1):
start = self.row_start(row)
board[start : start + self.size] = EMPTY
def is_eye(self, point, color):
"""
Check if point is a simple eye for color
"""
if not self._is_surrounded(point, color):
return False
# Eye-like shape. Check diagonals to detect false eye
opp_color = GoBoardUtil.opponent(color)
false_count = 0
at_edge = 0
for d in self._diag_neighbors(point):
if self.board[d] == BORDER:
at_edge = 1
elif self.board[d] == opp_color:
false_count += 1
return false_count <= 1 - at_edge # 0 at edge, 1 in center
def _is_surrounded(self, point, color):
"""
check whether empty point is surrounded by stones of color
(or BORDER) neighbors
"""
for nb in self._neighbors(point):
nb_color = self.board[nb]
if nb_color != BORDER and nb_color != color:
return False
return True
def _has_liberty(self, block):
"""
Check if the given block has any liberty.
block is a numpy boolean array
"""
for stone in where1d(block):
empty_nbs = self.neighbors_of_color(stone, EMPTY)
if empty_nbs:
return True
return False
def _block_of(self, stone):
"""
Find the block of given stone
Returns a board of boolean markers which are set for
all the points in the block
"""
color = self.get_color(stone)
assert is_black_white(color)
return self.connected_component(stone)
def connected_component(self, point):
"""
Find the connected component of the given point.
"""
marker = np.full(self.maxpoint, False, dtype=bool)
pointstack = [point]
color = self.get_color(point)
assert is_black_white_empty(color)
marker[point] = True
while pointstack:
p = pointstack.pop()
neighbors = self.neighbors_of_color(p, color)
for nb in neighbors:
if not marker[nb]:
marker[nb] = True
pointstack.append(nb)
return marker
def _detect_and_process_capture(self, nb_point):
"""
Check whether opponent block on nb_point is captured.
If yes, remove the stones.
Returns the stone if only a single stone was captured,
and returns None otherwise.
This result is used in play_move to check for possible ko
"""
single_capture = None
opp_block = self._block_of(nb_point)
if not self._has_liberty(opp_block):
captures = list(where1d(opp_block))
self.board[captures] = EMPTY
if len(captures) == 1:
single_capture = nb_point
return single_capture
def play_move(self, point, color):
"""
Play a move of color on point
Returns boolean: whether move was legal
"""
assert is_black_white(color)
# Special cases
if point == PASS:
self.ko_recapture = None
self.current_player = GoBoardUtil.opponent(color)
self.last2_move = self.last_move
self.last_move = point
return True
elif self.board[point] != EMPTY:
return False
# if point == self.ko_recapture:
# return False
# General case: deal with captures, suicide, and next ko point
# opp_color = GoBoardUtil.opponent(color)
# in_enemy_eye = self._is_surrounded(point, opp_color)
self.board[point] = color
# single_captures = []
# neighbors = self._neighbors(point)
# for nb in neighbors:
# if self.board[nb] == opp_color:
# single_capture = self._detect_and_process_capture(nb)
# if single_capture != None:
# single_captures.append(single_capture)
# block = self._block_of(point)
# if not self._has_liberty(block): # undo suicide move
# self.board[point] = EMPTY
# return False
# self.ko_recapture = None
# if in_enemy_eye and len(single_captures) == 1:
# self.ko_recapture = single_captures[0]
self.current_player = GoBoardUtil.opponent(color)
self.last2_move = self.last_move
self.last_move = point
return True
def neighbors_of_color(self, point, color):
""" List of neighbors of point of given color """
nbc = []
for nb in self._neighbors(point):
if self.get_color(nb) == color:
nbc.append(nb)
return nbc
def _neighbors(self, point):
""" List of all four neighbors of the point """
return [point - 1, point + 1, point - self.NS, point + self.NS]
def _diag_neighbors(self, point):
""" List of all four diagonal neighbors of point """
return [
point - self.NS - 1,
point - self.NS + 1,
point + self.NS - 1,
point + self.NS + 1,
]
def last_board_moves(self):
"""
Get the list of last_move and second last move.
Only include moves on the board (not None, not PASS).
"""
board_moves = []
if self.last_move != None and self.last_move != PASS:
board_moves.append(self.last_move)
if self.last2_move != None and self.last2_move != PASS:
board_moves.append(self.last2_move)
return
def detect_five_in_a_row(self):
"""
Returns BLACK or WHITE if any five in a row is detected for the color
EMPTY otherwise.
"""
for r in self.rows:
result = self.has_five_in_list(r)
if result != EMPTY:
return result
for c in self.cols:
result = self.has_five_in_list(c)
if result != EMPTY:
return result
for d in self.diags:
result = self.has_five_in_list(d)
if result != EMPTY:
return result
return EMPTY
def has_five_in_list(self, list):
"""
Returns BLACK or WHITE if any five in a rows exist in the list.
EMPTY otherwise.
"""
prev = BORDER
counter = 1
for stone in list:
if self.get_color(stone) == prev:
counter += 1
else:
counter = 1
prev = self.get_color(stone)
if counter == 5 and prev != EMPTY:
return prev
return EMPTY