diff --git a/go.py b/go.py index 2b87ee2..53de8b8 100644 --- a/go.py +++ b/go.py @@ -41,10 +41,10 @@ def remove(self): @property def neighbors(self): """Return a list of neighboring points.""" - neighboring = [(self.point[0]-1, self.point[1]), - (self.point[0]+1, self.point[1]), - (self.point[0], self.point[1]-1), - (self.point[0], self.point[1]+1)] + neighboring = [(self.point[0] - 1, self.point[1]), + (self.point[0] + 1, self.point[1]), + (self.point[0], self.point[1] - 1), + (self.point[0], self.point[1] + 1)] for point in neighboring: if not 0 < point[0] < 20 or not 0 < point[1] < 20: neighboring.remove(point) diff --git a/goban.py b/goban.py index ef6fc02..85f7357 100755 --- a/goban.py +++ b/goban.py @@ -25,7 +25,7 @@ class Stone(go.Stone): def __init__(self, board, point, color): """Create, initialize and draw a stone.""" super(Stone, self).__init__(board, point, color) - self.coords = (5+self.point[0]*40, 5+self.point[1]*40) + self.coords = (5 + self.point[0] * 40, 5 + self.point[1] * 40) self.draw() def draw(self): @@ -35,7 +35,7 @@ def draw(self): def remove(self): """Remove the stone from board.""" - blit_coords = (self.coords[0]-20, self.coords[1]-20) + blit_coords = (self.coords[0] - 20, self.coords[1] - 20) area_rect = pygame.Rect(blit_coords, (40, 40)) screen.blit(background, blit_coords, area_rect) pygame.display.update() @@ -65,15 +65,30 @@ def draw(self): self.outline.inflate_ip(20, 20) for i in range(18): for j in range(18): - rect = pygame.Rect(45+(40*i), 45+(40*j), 40, 40) + rect = pygame.Rect(45 + (40 * i), 45 + (40 * j), 40, 40) pygame.draw.rect(background, BLACK, rect, 1) for i in range(3): for j in range(3): - coords = (165+(240*i), 165+(240*j)) + coords = (165 + (240 * i), 165 + (240 * j)) pygame.draw.circle(background, BLACK, coords, 5, 0) screen.blit(background, (0,0)) pygame.display.update() + def update_liberties(self, added_stone=None): + """Updates the liberties of the entire board, group by group. + + Usually a stone is added each turn. To allow killing by 'suicide', + all the 'old' groups should be updated before the newly added one. + + """ + for group in self.groups: + if added_stone: + if group == added_stone.group: + continue + group.update_liberties() + if added_stone: + added_stone.group.update_liberties() + def main(): while True: @@ -83,15 +98,14 @@ def main(): exit() if event.type == pygame.MOUSEBUTTONDOWN: if event.button == 1 and board.outline.collidepoint(event.pos): - x = int(round(((event.pos[0]-5) / 40.0), 0)) - y = int(round(((event.pos[1]-5) / 40.0), 0)) + x = int(round(((event.pos[0] - 5) / 40.0), 0)) + y = int(round(((event.pos[1] - 5 ) / 40.0), 0)) stone = board.search(point=(x, y)) if stone: stone.remove() else: - stone = Stone(board, (x, y), board.turn()) - for group in board.groups: - group.update_liberties() + added_stone = Stone(board, (x, y), board.turn()) + board.update_liberties(locals().get('added_stone', '')) if __name__ == '__main__':