Skip to content

Commit

Permalink
Fixed issues with killing by 'suicide'.
Browse files Browse the repository at this point in the history
  • Loading branch information
eagleflo committed Aug 10, 2008
1 parent 400d1d4 commit ef87c4c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
8 changes: 4 additions & 4 deletions go.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
32 changes: 23 additions & 9 deletions goban.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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()
Expand Down Expand Up @@ -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:
Expand All @@ -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__':
Expand Down

0 comments on commit ef87c4c

Please sign in to comment.