forked from paramhooda/cmput455-a2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboard.py
262 lines (218 loc) · 8 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
"""
board.py
board.py
Cmput 455 sample code
Written by Cmput 455 TA and Martin Mueller
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 typing import List, Tuple
from board_base import (
board_array_size,
coord_to_point,
is_black_white,
is_black_white_empty,
opponent,
where1d,
BLACK,
WHITE,
EMPTY,
BORDER,
MAXSIZE,
NO_POINT,
GO_COLOR,
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: int):
"""
Creates a Go board of given size
"""
assert 2 <= size <= MAXSIZE
self.reset(size)
def reset(self, size: int) -> None:
"""
Creates a start state, an empty board with given size.
"""
self.size: int = size
self.NS: int = size + 1
self.WE: int = 1
self.current_player: GO_COLOR = BLACK
self.maxpoint: int = board_array_size(size)
self.board: np.ndarray[GO_POINT] = np.full(self.maxpoint, BORDER, dtype=GO_POINT)
self._initialize_empty_points(self.board)
def copy(self) -> 'GoBoard':
b = GoBoard(self.size)
assert b.NS == self.NS
assert b.WE == self.WE
b.current_player = self.current_player
assert b.maxpoint == self.maxpoint
b.board = np.copy(self.board)
return b
def get_color(self, point: GO_POINT) -> GO_COLOR:
return self.board[point]
def pt(self, row: int, col: int) -> GO_POINT:
return coord_to_point(row, col, self.size)
def is_legal(self, point: GO_POINT, color: GO_COLOR) -> bool:
"""
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: GoBoard = self.copy()
can_play_move = board_copy.play_move(point, color)
return can_play_move
def get_empty_points(self) -> np.ndarray:
"""
Return:
The empty points on the board
"""
return where1d(self.board == EMPTY)
def row_start(self, row: int) -> int:
assert row >= 1
assert row <= self.size
return row * self.NS + 1
def _initialize_empty_points(self, board_array: np.ndarray) -> None:
"""
Fills points on the board with EMPTY
Argument
---------
board: numpy array, filled with BORDER
"""
for row in range(1, self.size + 1):
start: int = self.row_start(row)
board_array[start : start + self.size] = EMPTY
def is_eye(self, point: GO_POINT, color: GO_COLOR) -> bool:
"""
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 = 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: GO_POINT, color: GO_COLOR) -> bool:
"""
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: np.ndarray) -> bool:
"""
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: GO_POINT) -> np.ndarray:
"""
Find the block of given stone
Returns a board of boolean markers which are set for
all the points in the block
"""
color: GO_COLOR = self.get_color(stone)
assert is_black_white(color)
return self.connected_component(stone)
def connected_component(self, point: GO_POINT) -> np.ndarray:
"""
Find the connected component of the given point.
"""
marker = np.full(self.maxpoint, False, dtype=np.bool_)
pointstack = [point]
color: GO_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: GO_POINT) -> GO_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 NO_POINT otherwise.
"""
opp_block = self._block_of(nb_point)
return not self._has_liberty(opp_block)
def play_move(self, point: GO_POINT, color: GO_COLOR) -> bool:
"""
Play a move of color on point
Returns whether move was legal
"""
assert is_black_white(color)
if self.board[point] != EMPTY:
return False
opp_color = opponent(color)
in_enemy_eye = self._is_surrounded(point, opp_color)
self.board[point] = color
neighbors = self._neighbors(point)
#check for capturing
for nb in neighbors:
if self.board[nb] == opp_color:
captured = self._detect_and_process_capture(nb)
if captured:
#undo capturing move
self.board[point] = EMPTY
return False
#check for suicide
block = self._block_of(point)
if not self._has_liberty(block):
# undo suicide move
self.board[point] = EMPTY
return False
self.current_player = opponent(color)
return True
def neighbors_of_color(self, point: GO_POINT, color: GO_COLOR) -> List:
""" List of neighbors of point of given color """
nbc: List[GO_POINT] = []
for nb in self._neighbors(point):
if self.get_color(nb) == color:
nbc.append(nb)
return nbc
def _neighbors(self, point: GO_POINT) -> List:
""" List of all four neighbors of the point """
return [point - 1, point + 1, point - self.NS, point + self.NS]
def _diag_neighbors(self, point: GO_POINT) -> List:
""" 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) -> List:
"""
Get the list of last_move and second last move.
Only include moves on the board (not NO_POINT, not PASS).
"""
board_moves: List[GO_POINT] = []
return board_moves