Fixed captures - everything works correctly now
This commit is contained in:
parent
80f4d11720
commit
21b5930431
49
lib/goban.py
49
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
|
||||
|
|
Loading…
Reference in New Issue
Block a user