Fix liberty checking code around the edges, fix the hover code

This commit is contained in:
Anna Rose 2012-04-14 01:21:17 -04:00
parent 7652ddd596
commit 96313cd47d

View File

@ -30,6 +30,8 @@ class Goban:
def set_hover(self, pos): def set_hover(self, pos):
rpos = self._real_pos(pos) rpos = self._real_pos(pos)
if rpos == self.hover:
return
if not self._valid_move(rpos): if not self._valid_move(rpos):
self.clear_hover() self.clear_hover()
@ -40,6 +42,7 @@ class Goban:
def clear_hover(self): def clear_hover(self):
self.hover = None self.hover = None
print 'hover clear'
# fixme - somewhere in this or its component functions we need to # fixme - somewhere in this or its component functions we need to
@ -56,6 +59,7 @@ class Goban:
self.to_move = self._other_color(self.to_move) self.to_move = self._other_color(self.to_move)
def _capture(self, pos): def _capture(self, pos):
"""Look for stones captured on the 4 sides of pos, remove them and increment """Look for stones captured on the 4 sides of pos, remove them and increment
capture counter. This pos must be a *real* position value, not an x,y tuple.""" capture counter. This pos must be a *real* position value, not an x,y tuple."""
@ -74,6 +78,7 @@ class Goban:
self.white_captures += self._delete_group(p) self.white_captures += self._delete_group(p)
def _valid_move(self, pos): def _valid_move(self, pos):
if not self._on_board(pos): if not self._on_board(pos):
return False return False
@ -103,6 +108,7 @@ class Goban:
return liberties > 0 or kills_group return liberties > 0 or kills_group
# Recursively find whether there are liberties for the group # Recursively find whether there are liberties for the group
# at pos. Positive numbers are not necessarily accurate - # at pos. Positive numbers are not necessarily accurate -
# treat this as a boolean # treat this as a boolean
@ -127,12 +133,23 @@ class Goban:
return 0 return 0
else: else:
liberties = 0 liberties = 0
for d in [pos + 1, pos - 1, pos + self.board_size, pos - self.board_size]: neighbors = []
if pos >= self.board_size:
neighbors.append(pos - self.board_size)
if pos <= self.board_size * self.board_size - self.board_size - 1:
neighbors.append(pos + self.board_size)
if pos % self.board_size != 0:
neighbors.append(pos - 1)
if (pos + 1) % self.board_size != 0:
neighbors.append(pos + 1)
for d in neighbors:
liberties += self._has_liberties(d, who, checked) liberties += self._has_liberties(d, who, checked)
return liberties return liberties
# We don't need to worry about crossing ourselves with the # We don't need to worry about crossing ourselves with the
# recursion here, because we've already deleted the stone. # recursion here, because we've already deleted the stone.
# It would be more efficient to avoid going backwards, # It would be more efficient to avoid going backwards,
@ -154,6 +171,7 @@ class Goban:
self.board[x][y].state = Goban.EMPTY self.board[x][y].state = Goban.EMPTY
def _delete_group_r(self, pos, who): def _delete_group_r(self, pos, who):
if not self._on_board(pos): if not self._on_board(pos):
return return
@ -173,6 +191,7 @@ class Goban:
return num_deleted return num_deleted
def draw_board(self, size, img_res): def draw_board(self, size, img_res):
ret = pygame.Surface((size,size)) ret = pygame.Surface((size,size))
@ -191,7 +210,7 @@ class Goban:
s = pygame.transform.scale(s, (inc, inc)) s = pygame.transform.scale(s, (inc, inc))
ret.blit(s, ((pos % self.board_size) *inc, (pos / self.board_size) *inc)) ret.blit(s, ((pos % self.board_size) *inc, (pos / self.board_size) *inc))
if self.hover == point: if self.hover == pos:
c = img_res['bH'] c = img_res['bH']
if self.to_move == Goban.WHITE: if self.to_move == Goban.WHITE:
c = img_res['wH'] c = img_res['wH']
@ -201,6 +220,7 @@ class Goban:
return ret.convert_alpha() return ret.convert_alpha()
def draw_info(self): def draw_info(self):
textbox = pygame.Surface((150, 300)) textbox = pygame.Surface((150, 300))
textbox = textbox.convert() textbox = textbox.convert()
@ -225,6 +245,7 @@ class Goban:
return textbox return textbox
def _make_default_draw_codes(self): def _make_default_draw_codes(self):
ret = [] ret = []
@ -233,7 +254,7 @@ class Goban:
ret.append('ul') ret.append('ul')
elif pos == self.board_size - 1: elif pos == self.board_size - 1:
ret.append('ur') ret.append('ur')
elif pos == self.board_size * self.board_size - 19: elif pos == self.board_size * self.board_size - self.board_size:
ret.append('dl') ret.append('dl')
elif pos == self.board_size * self.board_size - 1: elif pos == self.board_size * self.board_size - 1:
ret.append('dr') ret.append('dr')
@ -243,7 +264,7 @@ class Goban:
ret.append('u') ret.append('u')
elif pos % self.board_size == 0: elif pos % self.board_size == 0:
ret.append('l') ret.append('l')
elif pos > (self.board_size * self.board_size - 20): elif pos > (self.board_size * self.board_size - self.board_size - 1):
ret.append('d') ret.append('d')
elif pos % self.board_size == 18: elif pos % self.board_size == 18:
ret.append('r') ret.append('r')