From 21b5930431bbc2161bb37c95a5573e0fb0af9e74 Mon Sep 17 00:00:00 2001 From: Anna Wiggins Date: Sat, 14 Apr 2012 01:47:31 -0400 Subject: [PATCH] Fixed captures - everything works correctly now --- lib/goban.py | 49 ++++++++++++++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 21 deletions(-) diff --git a/lib/goban.py b/lib/goban.py index c45f942..ed210f3 100644 --- a/lib/goban.py +++ b/lib/goban.py @@ -67,7 +67,7 @@ class Goban: # Who are we capturing who = self._other_color(self.to_move) - for p in [pos + 1, pos - 1, pos + self.board_size, pos - self.board_size]: + for p in self._neighbors(pos): if not self._on_board(p): continue @@ -94,7 +94,7 @@ class Goban: opponent = self._other_color(self.to_move) kills_group = False - for d in [pos + 1, pos - 1, pos + self.board_size, pos - self.board_size]: + for d in self._neighbors(pos): if not self._on_board(d): continue @@ -112,14 +112,17 @@ class Goban: # Recursively find whether there are liberties for the group # at pos. Positive numbers are not necessarily accurate - # treat this as a boolean - def _has_liberties(self, pos, who, checked=None): - if not self._on_board(pos): - return 0 - - if checked is None: - bs = self.board_size * self.board_size - checked = [False] * bs + def _has_liberties(self, pos, who): + if not self._on_board(pos) or self.board[pos] != who: + return -1 + bs = self.board_size * self.board_size + checked = [False] * bs + + return self._has_liberties_r(pos, who, checked) + + + def _has_liberties_r(self, pos, who, checked=None): if checked[pos]: return 0 else: @@ -133,18 +136,8 @@ class Goban: return 0 else: liberties = 0 - 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) + for d in self._neighbors(pos): + liberties += self._has_liberties_r(d, who, checked) return liberties @@ -289,3 +282,17 @@ class Goban: return Goban.WHITE elif color == Goban.WHITE: return Goban.BLACK + + + def _neighbors(self, pos): + 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) + + return neighbors