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