Skip to content

Instantly share code, notes, and snippets.

@uberto
Created March 28, 2018 19:27
Show Gist options
  • Save uberto/94464f7c10574bec244a21412901bea3 to your computer and use it in GitHub Desktop.
Save uberto/94464f7c10574bec244a21412901bea3 to your computer and use it in GitHub Desktop.
python go
def place_stone(self, player, point):
assert self.is_on_grid(point)
assert self._grid.get(point) is None
adjacent_same_color = []
adjacent_opposite_color = []
liberties = []
# First, we examine direct neighbors of this point.
for neighbor in point.neighbors():
if not self.is_on_grid(neighbor):
continue
neighbor_string = self._grid.get(neighbor)
if neighbor_string is None:
liberties.append(neighbor)
elif neighbor_string.color == player:
if neighbor_string not in adjacent_same_color:
adjacent_same_color.append(neighbor_string)
else:
if neighbor_string not in adjacent_opposite_color:
adjacent_opposite_color.append(neighbor_string)
new_string = GoString(player, [point], liberties)
# Merge any adjacent strings of the same color.
for same_color_string in adjacent_same_color:
new_string = new_string.merged_with(same_color_string)
for new_string_point in new_string.stones:
self._grid[new_string_point] = new_string
# Reduce liberties of any adjacent strings of the opposite color.
for other_color_string in adjacent_opposite_color:
other_color_string.remove_liberty(point)
# If any opposite color strings now have zero liberties, remove them.
for other_color_string in adjacent_opposite_color:
if other_color_string.num_liberties == 0:
self._remove_string(other_color_string)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment