8000 minigo/go.py at master · hxssgaa/minigo · GitHub
[go: up one dir, main page]

Skip to content

Latest commit

 

History

History
541 lines (455 loc) · 19.2 KB

go.py

File metadata and controls

541 lines (455 loc) · 19.2 KB
# by default, every move is legal
legal_moves = np.ones([N, N], dtype=np.int8)
# ...unless there is already a stone there
legal_moves[self.board != EMPTY] = 0
# calculate which spots have 4 stones next to them
# padding is because the edge always counts as a lost liberty.
adjacent = np.ones([N + 2, N + 2], dtype=np.int8)
adjacent[1:-1, 1:-1] = np.abs(self.board)
num_adjacent_stones = (adjacent[:-2, 1:-1] + adjacent[1:-1, :-2] +
adjacent[2:, 1:-1] + adjacent[1:-1, 2:])
# Surrounded spots are those that are empty and have 4 adjacent stones.
surrounded_spots = np.multiply(
(self.board == EMPTY),
(num_adjacent_stones == 4))
# Such spots are possibly illegal, unless they are capturing something.
# Iterate over and manually check each spot.
for coord in np.transpose(np.nonzero(surrounded_spots)):
if self.is_move_suicidal(tuple(coord)):
legal_moves[tuple(coord)] = 0
# ...and retaking ko is always illegal
if self.ko is not None:
legal_moves[self.ko] = 0
# and pass is always legal
return np.concatenate([legal_moves.ravel(), [1]])
def pass_move(self, mutate=False):
pos = self if mutate else copy.deepcopy(self)
pos.n += 1
pos.recent += (PlayerMove(pos.to_play, None),)
pos.board_deltas = np.concatenate((
np.zeros([1, N, N], dtype=np.int8),
pos.board_deltas[:6]))
pos.to_play *= -1
pos.ko = None
return pos
def flip_playerturn(self, mutate=False):
pos = self if mutate else copy.deepcopy(self)
pos.ko = None
pos.to_play *= -1
return pos
def get_liberties(self):
return self.lib_tracker.liberty_cache
def play_move(self, c, color=None, mutate=False):
# Obeys CGOS Rules of Play. In short:
# No suicides
# Chinese/area scoring
# Positional superko (this is very crudely approximate at the moment.)
if color is None:
color = self.to_play
pos = self if mutate else copy.deepcopy(self)
if c is None:
pos = pos.pass_move(mutate=mutate)
return pos
if not self.is_move_legal(c):
raise IllegalMove("{} move at {} is illegal: \n{}".format(
"Black" if self.to_play == BLACK else "White",
coords.to_gtp(c), self))
potential_ko = is_koish(self.board, c)
place_stones(pos.board, color, [c])
captured_stones = pos.lib_tracker.add_stone(color, c)
place_stones(pos.board, EMPTY, captured_stones)
opp_color = color * -1
new_board_delta = np.zeros([N, N], dtype=np.int8)
new_board_delta[c] = color
place_stones(new_board_delta, color, captured_stones)
if len(captured_stones) == 1 and potential_ko == opp_color:
new_ko = list(captured_stones)[0]
else:
new_ko = None
if pos.to_play == BLACK:
new_caps = (pos.caps[0] + len(captured_stones), pos.caps[1])
else:
new_caps = (pos.caps[0], pos.caps[1] + len(captured_stones))
pos.n += 1
pos.caps = new_caps
pos.ko = new_ko
pos.recent += (PlayerMove(color, c),)
# keep a rolling history of last 7 deltas - that's all we'll need to
# extract the last 8 board states.
pos.board_deltas = np.concatenate((
new_board_delta.reshape(1, N, N),
pos.board_deltas[:6]))
pos.to_play *= -1
return pos
def is_game_over(self):
return (len(self.recent) >= 2 and
self.recent[-1].move is None and
self.recent[-2].move is None)
def score(self):
'Return score from B perspective. If W is winning, score is negative.'
working_board = np.copy(self.board)
while EMPTY in working_board:
unassigned_spaces = np.where(working_board == EMPTY)
c = unassigned_spaces[0][0], unassigned_spaces[1][0]
territory, borders = find_reached(working_board, c)
border_colors = set(working_board[b] for b in borders)
X_border = BLACK in border_colors
O_border = WHITE in border_colors
if X_border and not O_border:
territory_color = BLACK
elif O_border and not X_border:
territory_color = WHITE
else:
territory_color = UNKNOWN # dame, or seki
place_stones(working_board, territory_color, territory)
return np.count_nonzero(working_board == BLACK) - np.count_nonzero(working_board == WHITE) - self.komi
def result(self):
score = self.score()
if score > 0:
return 1
elif score < 0:
return -1
else:
return 0
def result_string(self):
score = self.score()
if score > 0:
return 'B+' + '%.1f' % score
elif score < 0:
return 'W+' + '%.1f' % abs(score)
else:
return 'DRAW'
0