Return number of stones captured, keep track of captures for each player

This commit is contained in:
Anna Rose 2012-04-08 18:07:10 -04:00
parent c925a95ad7
commit 8881b1cd38

48
pygo.py
View File

@ -117,6 +117,9 @@ class Goban:
self.board[i].append(GobanSquare((i+1, j+1)))
self.turn = 0
self.captures = []
self.captures.append(0)
self.captures.append(0)
def place_stone(self, pos):
@ -147,11 +150,9 @@ class Goban:
continue
if not self._has_liberties(p, who):
self._delete_group(p)
captures[self.turn] += 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
if x < 0 or x > 18 or y < 0 or y > 18:
@ -182,6 +183,8 @@ class Goban:
# Recursively find whether there are liberties for the group
# at pos. Positive numbers are not necessarily accurate -
# treat this as a boolean
# fixme: Apparently there is some case that causes infinite recursion,
# yay!
def _has_liberties(self, pos, who, direction = None):
x,y = pos
if x < 0 or x > 18 or y < 0 or y > 18:
@ -229,26 +232,34 @@ class Goban:
self.board[x][y].state = -1
self._delete_group_r((x, y+1), who)
self._delete_group_r((x, y-1), who)
self._delete_group_r((x+1, y), who)
self._delete_group_r((x-1, y), who)
num_deleted = 1
num_deleted += self._delete_group_r((x, y+1), who)
num_deleted += self._delete_group_r((x, y-1), who)
num_deleted += self._delete_group_r((x+1, y), who)
num_deleted += self._delete_group_r((x-1, y), who)
return num_deleted
def _delete_group_r(self, pos, who):
x,y = pos
if x < 0 or x > 18 or y < 0 or y > 18:
return
return 0
if self.board[x][y].state != who:
return
return 0
self.board[x][y].state = -1
self._delete_group_r((x, y+1), who)
self._delete_group_r((x, y-1), who)
self._delete_group_r((x+1, y), who)
self._delete_group_r((x-1, y), who)
num_deleted = 1
num_deleted += self._delete_group_r((x, y+1), who)
num_deleted += self._delete_group_r((x, y-1), who)
num_deleted += self._delete_group_r((x+1, y), who)
num_deleted += self._delete_group_r((x-1, y), who)
return num_deleted
def draw_board(self, size, img_res):
@ -271,7 +282,7 @@ class Goban:
def main():
# Basic screen init
pygame.init()
screen = pygame.display.set_mode((800, 800))
screen = pygame.display.set_mode((1000, 800))
pygame.display.set_caption('pyGo')
# Create the background object, make it blank
@ -303,10 +314,11 @@ def main():
row = y / board_inc
col = x / board_inc
if event.button == 1:
goban.place_stone((row, col))
if event.button == 3:
goban.toggle_marked((row, col))
if x <= board_size:
if event.button == 1:
goban.place_stone((row, col))
if event.button == 3:
goban.toggle_marked((row, col))
board = goban.draw_board(board_size, img_res)
background.blit(board, (0,0))