Return number of stones captured, keep track of captures for each player
This commit is contained in:
parent
c925a95ad7
commit
8881b1cd38
40
pygo.py
40
pygo.py
|
@ -117,6 +117,9 @@ class Goban:
|
||||||
self.board[i].append(GobanSquare((i+1, j+1)))
|
self.board[i].append(GobanSquare((i+1, j+1)))
|
||||||
|
|
||||||
self.turn = 0
|
self.turn = 0
|
||||||
|
self.captures = []
|
||||||
|
self.captures.append(0)
|
||||||
|
self.captures.append(0)
|
||||||
|
|
||||||
|
|
||||||
def place_stone(self, pos):
|
def place_stone(self, pos):
|
||||||
|
@ -147,11 +150,9 @@ class Goban:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if not self._has_liberties(p, who):
|
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):
|
def _valid_move(self, pos):
|
||||||
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:
|
||||||
|
@ -182,6 +183,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
|
# treat this as a boolean
|
||||||
|
# fixme: Apparently there is some case that causes infinite recursion,
|
||||||
|
# yay!
|
||||||
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:
|
||||||
|
@ -229,26 +232,34 @@ class Goban:
|
||||||
|
|
||||||
self.board[x][y].state = -1
|
self.board[x][y].state = -1
|
||||||
|
|
||||||
self._delete_group_r((x, y+1), who)
|
num_deleted = 1
|
||||||
self._delete_group_r((x, y-1), who)
|
|
||||||
self._delete_group_r((x+1, y), who)
|
num_deleted += self._delete_group_r((x, y+1), who)
|
||||||
self._delete_group_r((x-1, y), 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):
|
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:
|
if x < 0 or x > 18 or y < 0 or y > 18:
|
||||||
return
|
return 0
|
||||||
|
|
||||||
if self.board[x][y].state != who:
|
if self.board[x][y].state != who:
|
||||||
return
|
return 0
|
||||||
|
|
||||||
self.board[x][y].state = -1
|
self.board[x][y].state = -1
|
||||||
|
|
||||||
self._delete_group_r((x, y+1), who)
|
num_deleted = 1
|
||||||
self._delete_group_r((x, y-1), who)
|
|
||||||
self._delete_group_r((x+1, y), who)
|
num_deleted += self._delete_group_r((x, y+1), who)
|
||||||
self._delete_group_r((x-1, y), 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):
|
def draw_board(self, size, img_res):
|
||||||
|
@ -271,7 +282,7 @@ class Goban:
|
||||||
def main():
|
def main():
|
||||||
# Basic screen init
|
# Basic screen init
|
||||||
pygame.init()
|
pygame.init()
|
||||||
screen = pygame.display.set_mode((800, 800))
|
screen = pygame.display.set_mode((1000, 800))
|
||||||
pygame.display.set_caption('pyGo')
|
pygame.display.set_caption('pyGo')
|
||||||
|
|
||||||
# Create the background object, make it blank
|
# Create the background object, make it blank
|
||||||
|
@ -303,6 +314,7 @@ def main():
|
||||||
row = y / board_inc
|
row = y / board_inc
|
||||||
col = x / board_inc
|
col = x / board_inc
|
||||||
|
|
||||||
|
if x <= board_size:
|
||||||
if event.button == 1:
|
if event.button == 1:
|
||||||
goban.place_stone((row, col))
|
goban.place_stone((row, col))
|
||||||
if event.button == 3:
|
if event.button == 3:
|
||||||
|
|
Loading…
Reference in New Issue
Block a user