]> git.treefish.org Git - shutbox.git/blob - src/game.py
50d83c024b130ac2f76613bcbdd5fd94616728df
[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 get_dice(self):
21         return self._diced
22
23     def shut(self, rods):
24         if self._is_valid_option(rods):
25             for rod in rods:
26                 self._shutable.remove(rod)
27             self._diced = None
28             self._options = []
29
30     def is_game_over(self):
31         return len(self._shutable) == 0 or \
32             ( self._diced and len(self._options) == 0 )
33
34     def _can_be_shut(self, rods):
35         shutable = self._shutable.copy()
36         for rod in rods:
37             if rod in shutable:
38                 shutable.remove(rod)
39             else:
40                 return False
41
42     def _is_valid_option(self, rods):
43         for option in self._options:
44             if set(option) == set(rods):
45                 return True
46         return False