]> git.treefish.org Git - shutbox.git/blob - src/game.py
added game logic
[shutbox.git] / src / game.py
1 #!/usr/bin/env python3
2
3 import random
4
5 class Game:
6     def __init__(self):
7         self._shutable = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
8         self._diced = None
9         self._options = []
10
11     def dice(self):
12         if not self._diced:
13             self._diced = [random.randint(0, 6), random.randint(0, 6)]
14             for rods in [ self._dice,
15                           [ abs(self._dice[0] - self._dice[1]) ],
16                           [ self._dice[0] + self._dice[1] ] ]:
17                 if self._can_be_shut(rods):
18                     self._options.append(rods)
19
20     def shut(self, rods):
21         if self._is_valid_option(rods):
22             for rod in rods:
23                 self._shutable.remove(rod)
24             self._diced = None
25             self._options = []
26
27     def is_game_over(self):
28         return len(self._shutable) == 0 or \
29             ( self._diced and len(self._options) == 0 )
30
31     def _can_be_shut(self, rods):
32         shutable = self._shutable.copy()
33         for rod in rods:
34             if rod in shutable:
35                 shutable.remove(rod)
36             else:
37                 return False
38
39     def _is_valid_option(self, rods):
40         for option in self._options:
41             if set(option) == set(rods):
42                 return True
43         return False