diff --git a/pygo.py b/pygo.py index a0a3bac..866ba58 100755 --- a/pygo.py +++ b/pygo.py @@ -154,11 +154,29 @@ class Goban: # 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') - liberties += self._has_liberties((x-1, y), self.turn, 'u') - liberties += self._has_liberties((x, y-1), self.turn, 'l') - liberties += self._has_liberties((x+1, y), self.turn, 'd') - return liberties + if x < 0 or x > 18 or y < 0 or y > 18: + return False + + # Can't play atop another stone + 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