Fixed Goban._delete_group

This commit is contained in:
Anna Rose 2012-04-08 16:59:38 -04:00
parent 15480ae9e0
commit efa1a4f282

52
pygo.py
View File

@ -141,16 +141,17 @@ class Goban:
x, y = pos x, y = pos
who = (self.turn + 1) % 2 who = (self.turn + 1) % 2
if self._has_liberties((x, y+1), who): for p in [(x, y+1), (x, y-1), (x+1, y), (x-1, y)]:
self._delete_group((x, y+1)) newx, newy = p
if self._has_liberties((x, y-1), who): if newx < 0 or newx > 18 or newy < 0 or newy > 18 or self.board[newx][newy].state == self.turn:
self._delete_group((x, y-1)) continue
if self._has_liberties((x+1, y), who):
self._delete_group((x+1, y)) if not self._has_liberties(p, who):
if self._has_liberties((x-1, y), who): self._delete_group(p)
self._delete_group((x-1, y))
# fixme - can't play into a liberty-less hole, even
# if it will result in a capture...
def _valid_move(self, pos): def _valid_move(self, pos):
x, y = pos x, y = pos
liberties = self._has_liberties((x, y+1), self.turn, 'r') liberties = self._has_liberties((x, y+1), self.turn, 'r')
@ -161,7 +162,8 @@ 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
def _has_liberties(self, pos, who, direction = None): def _has_liberties(self, pos, who, direction = None):
x,y = pos x,y = pos
if x < 0 or x > 18 or y < 0 or y > 18: if x < 0 or x > 18 or y < 0 or y > 18:
@ -173,27 +175,22 @@ class Goban:
elif square.state != who: elif square.state != who:
return 0 return 0
elif square.state == who: elif square.state == who:
to_check = []
if direction == None: if direction == None:
liberties = self._has_liberties((x, y+1), who, 'r') to_check = [((x, y+1), 'r'), ((x-1, y), 'u'), ((x+1, y), 'd'), ((x, y-1), 'l')]
liberties += self._has_liberties((x-1, y), who, 'u')
liberties += self._has_liberties((x+1, y), who, 'd')
liberties += self._has_liberties((x, y-1), who, 'l')
if direction == 'r': if direction == 'r':
liberties = self._has_liberties((x, y+1), who, 'r') to_check = [((x, y+1), 'r'), ((x-1, y), 'u'), ((x+1, y), 'd')]
liberties += self._has_liberties((x-1, y), who, 'u')
liberties += self._has_liberties((x+1, y), who, 'd')
if direction == 'l': if direction == 'l':
liberties = self._has_liberties((x, y-1), who, 'l') to_check = [((x, y-1), 'l'), ((x-1, y), 'u'), ((x+1, y), 'd')]
liberties += self._has_liberties((x-1, y), who, 'u')
liberties += self._has_liberties((x+1, y), who, 'd')
if direction == 'u': if direction == 'u':
liberties = self._has_liberties((x, y+1), who, 'r') to_check = [((x, y+1), 'r'), ((x-1, y), 'u'), ((x, y-1), 'l')]
liberties += self._has_liberties((x-1, y), who, 'u')
liberties += self._has_liberties((x, y-1), who, 'l')
if direction == 'd': if direction == 'd':
liberties = self._has_liberties((x, y+1), who, 'r') to_check = [((x, y+1), 'r'), ((x, y-1), 'l'), ((x+1, y), 'd')]
liberties += self._has_liberties((x, y-1), who, 'l')
liberties += self._has_liberties((x+1, y), who, 'd') liberties = 0
for d, c in to_check:
liberties += self._has_liberties(d, who, c)
return liberties return liberties
@ -222,7 +219,10 @@ class Goban:
def _delete_group_r(self, pos, who): def _delete_group_r(self, pos, who):
x,y = pos x,y = pos
if x < 0 or x > 18 or y < 0 or y > 18 or self.board[x][y] != who: if x < 0 or x > 18 or y < 0 or y > 18:
return
if self.board[x][y].state != who:
return return
self.board[x][y].state = -1 self.board[x][y].state = -1