Fixed captures - everything works correctly now

This commit is contained in:
Anna Rose 2012-04-14 01:47:31 -04:00
parent 80f4d11720
commit 21b5930431

View File

@ -67,7 +67,7 @@ class Goban:
# Who are we capturing # Who are we capturing
who = self._other_color(self.to_move) 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): if not self._on_board(p):
continue continue
@ -94,7 +94,7 @@ class Goban:
opponent = self._other_color(self.to_move) opponent = self._other_color(self.to_move)
kills_group = False 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): if not self._on_board(d):
continue continue
@ -112,14 +112,17 @@ class Goban:
# 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
def _has_liberties(self, pos, who, checked=None): def _has_liberties(self, pos, who):
if not self._on_board(pos): if not self._on_board(pos) or self.board[pos] != who:
return 0 return -1
if checked is None:
bs = self.board_size * self.board_size bs = self.board_size * self.board_size
checked = [False] * bs checked = [False] * bs
return self._has_liberties_r(pos, who, checked)
def _has_liberties_r(self, pos, who, checked=None):
if checked[pos]: if checked[pos]:
return 0 return 0
else: else:
@ -133,18 +136,8 @@ class Goban:
return 0 return 0
else: else:
liberties = 0 liberties = 0
neighbors = [] for d in self._neighbors(pos):
if pos >= self.board_size: liberties += self._has_liberties_r(d, who, checked)
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)
return liberties return liberties
@ -289,3 +282,17 @@ class Goban:
return Goban.WHITE return Goban.WHITE
elif color == Goban.WHITE: elif color == Goban.WHITE:
return Goban.BLACK 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