Goban._valid_move() should allow placing a stone into a liberty-less space if it will result in a capture.

This commit is contained in:
Anna Rose 2012-04-08 17:10:39 -04:00
parent efa1a4f282
commit c925a95ad7

28
pygo.py
View File

@ -154,11 +154,29 @@ class Goban:
# if it will result in a capture... # 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') if x < 0 or x > 18 or y < 0 or y > 18:
liberties += self._has_liberties((x-1, y), self.turn, 'u') return False
liberties += self._has_liberties((x, y-1), self.turn, 'l')
liberties += self._has_liberties((x+1, y), self.turn, 'd') # Can't play atop another stone
return liberties if self.board[x][y].state != -1:
return False
# Temporarily place the stone
self.board[x][y].state = self.turn
liberties = self._has_liberties(pos, self.turn)
opponent = (self.turn + 1) % 2
kills_group = False
for d in [(x, y+1), (x, y-1), (x+1, y), (x-1, y)]:
if not self._has_liberties(d, opponent):
kills_group = True
break
self.board[x][y].state = -1
return liberties > 0 or kills_group
# Recursively find whether there are liberties for the group # Recursively find whether there are liberties for the group