]> git.treefish.org Git - shutbox.git/blob - src/game.py
5fc133a4280169935ae5b4e376e210f3d6610c07
[shutbox.git] / src / game.py
1 import random
2
3 class Game:
4     def __init__(self):
5         self._shutable = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
6         self._diced = None
7         self._options = []
8         self._score = 0
9
10     def dice(self):
11         if not self._diced:
12             self._diced = [random.randint(1, 6), random.randint(1, 6)]
13             self._diced.sort()
14             for rods in [ self._diced,
15                           [ abs(self._diced[0] - self._diced[1]) ],
16                           [ self._diced[0] + self._diced[1] ] ]:
17                 if self._can_be_shut(rods):
18                     self._options.append(rods)
19
20     def get_diced(self):
21         return self._diced
22
23     def get_shutable(self):
24         return self._shutable
25
26     def get_options(self):
27         return self._options
28
29     def shut(self, rods):
30         if self._is_valid_option(rods):
31             for rod in rods:
32                 self._score += rod
33                 self._shutable.remove(rod)
34             self._diced = None
35             self._options = []
36
37     def is_over(self):
38         return len(self._shutable) == 0 or \
39             ( self._diced and len(self._options) == 0 )
40
41     def get_score(self):
42         return self._score
43
44     def _can_be_shut(self, rods):
45         shutable = self._shutable.copy()
46         for rod in rods:
47             if rod in shutable:
48                 shutable.remove(rod)
49             else:
50                 return False
51         return True
52
53     def _is_valid_option(self, rods):
54         for option in self._options:
55             if set(option) == set(rods):
56                 return True
57         return False