]> git.treefish.org Git - shutbox.git/blob - src/game.py
added random agent
[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             for rods in [ self._diced,
14                           [ abs(self._diced[0] - self._diced[1]) ],
15                           [ self._diced[0] + self._diced[1] ] ]:
16                 if self._can_be_shut(rods):
17                     self._options.append(rods)
18
19     def get_dice(self):
20         return self._diced
21
22     def get_options(self):
23         return self._options
24
25     def shut(self, rods):
26         if self._is_valid_option(rods):
27             for rod in rods:
28                 self._score += rod
29                 self._shutable.remove(rod)
30             self._diced = None
31             self._options = []
32
33     def is_over(self):
34         return len(self._shutable) == 0 or \
35             ( self._diced and len(self._options) == 0 )
36
37     def get_score(self):
38         return self._score
39
40     def _can_be_shut(self, rods):
41         shutable = self._shutable.copy()
42         for rod in rods:
43             if rod in shutable:
44                 shutable.remove(rod)
45             else:
46                 return False
47         return True
48
49     def _is_valid_option(self, rods):
50         for option in self._options:
51             if set(option) == set(rods):
52                 return True
53         return False