Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/gogame.py
diff options
context:
space:
mode:
authorAndrés Ambrois <andresambrois@gmail.com>2008-08-28 19:13:20 (GMT)
committer Andrés Ambrois <andresambrois@gmail.com>2008-08-28 20:10:59 (GMT)
commita1dffeafb103f33b1f5d32bb2736c1462f3a1135 (patch)
tree3bdb0d9ee2c18483dfb64ad89c150462fe8b6930 /gogame.py
parentde4840470b8d27be26a4fccf0d72973219832f08 (diff)
Add get_territories() for calculating territories
get_territories() returns a dictionary of sets of points with the territories for each color.
Diffstat (limited to 'gogame.py')
-rw-r--r--gogame.py21
1 files changed, 21 insertions, 0 deletions
diff --git a/gogame.py b/gogame.py
index 51a9486..fa8e705 100644
--- a/gogame.py
+++ b/gogame.py
@@ -185,6 +185,27 @@ class GoGame:
return st # no liberties found, return list of all stones connected to the original one
+ def get_territories(self):
+ def get_group(self, first_element, color):
+ current_group = [first_element]
+ for current_element in current_group:
+ for x in self.neighbors(current_element):
+ if self.status.has_key(x):
+ if self.status[x] != color:
+ return None
+ elif not x in current_group:
+ current_group.append(x)
+ return set(current_group)
+
+ groups = {'B':set(), 'W':set()}
+ for x in self.status.keys():
+ for n in self.neighbors(x):
+ if not self.status.has_key(n):
+ new_group = get_group(self, n, self.status[x])
+ if new_group:
+ groups[self.status[x]] |= new_group
+ return groups
+
def undo(self, no=1):
""" Undo the last no moves. """
for i in range(no):