-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
43 lines (39 loc) · 1.51 KB
/
util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
class Group(object):
"""Represents a group of connected stones on the board.
Attributes:
stones (set): list of all coordinates where the group has a stone
border (set): list of all fields that are adjacent to the group
For a new group empty fields must be added manually
since the group does not know about the field size
color (bool): color of the group
Property:
size (int): equal to len(self.stones), the number of stones in
the group.
"""
def __init__(self, stones=None, color=None):
"""
Initialise group
"""
if stones is not None:
self.stones = set(stones)
else:
self.stones = set()
self.border = set()
self.color = color
def __add__(self, other):
"""To add two groups of the same color
The new group contains all the stones of the previous groups and
the border will be updated correctly.
Raises:
TypeError: The colours of the groups do not match
"""
if self.color != other.color:
raise ValueError('Only groups of same colour can be added!')
grp = Group(stones=self.stones.union(other.stones))
grp.color = self.color
grp.border = self.border.union(other.border).difference(grp.stones)
return grp
@property
def size(self):
"""Size of the group"""
return len(self.stones)