-
Notifications
You must be signed in to change notification settings - Fork 0
/
hand.py
61 lines (50 loc) · 2.01 KB
/
hand.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
class Hand:
def __init__(self, count=0, game=None, has=[], lacks=[]):
self.count = max(count, len(has))
self.has_set = set(has)
self.lacks_set = set(lacks)
self.eliminations_set = set()
self.game = game
def has(self, card):
self.has_set.add(card)
self.lacks_set.discard(card)
self.__check_count()
self.__eliminate()
self.game.notify_has(self, card)
def elimination(self, cards):
self.eliminations_set.add(frozenset(cards))
self.__eliminate()
def lacks(self, card):
self.lacks_set.add(card)
self.has_set.discard(card)
self.__eliminate()
self.game.notify_lacks(self, card)
def __check_count(self):
if len(self.has_set) >= self.count:
for c in self.game.cards - self.has_set:
self.lacks(c)
def __eliminate(self):
self.__filter_has_eliminations()
self.__reduce_lacks_eliminations()
self.__add_eliminated_has()
def __filter_has_eliminations(self):
for elimination in set(self.eliminations_set):
for card in self.has_set:
if card in elimination:
self.eliminations_set.discard(elimination)
def __reduce_lacks_eliminations(self):
for elimination in set(self.eliminations_set):
for card in self.lacks_set:
if card in elimination:
self.eliminations_set.discard(elimination)
self.eliminations_set.add(frozenset(set(elimination) - set([card])))
def __add_eliminated_has(self):
for elimination in set(self.eliminations_set):
if len(elimination) == 1:
self.eliminations_set.discard(elimination)
self.has(list(elimination)[0])
def __repr__(self):
return "Has: {}\nLacks: {}\nOne ofs: \n {}".format(
list(self.has_set),
list(self.lacks_set),
"\n ".join([str(list(sub)) for sub in self.eliminations_set]))