]> git.treefish.org Git - shutbox.git/commitdiff
added game logic
authorAlexander Schmidt <alex@treefish.org>
Tue, 6 Oct 2020 09:40:07 +0000 (11:40 +0200)
committerAlexander Schmidt <alex@treefish.org>
Tue, 6 Oct 2020 09:40:10 +0000 (11:40 +0200)
src/game.py [new file with mode: 0644]

diff --git a/src/game.py b/src/game.py
new file mode 100644 (file)
index 0000000..04a32e2
--- /dev/null
@@ -0,0 +1,43 @@
+#!/usr/bin/env python3
+
+import random
+
+class Game:
+    def __init__(self):
+        self._shutable = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
+        self._diced = None
+        self._options = []
+
+    def dice(self):
+        if not self._diced:
+            self._diced = [random.randint(0, 6), random.randint(0, 6)]
+            for rods in [ self._dice,
+                          [ abs(self._dice[0] - self._dice[1]) ],
+                          [ self._dice[0] + self._dice[1] ] ]:
+                if self._can_be_shut(rods):
+                    self._options.append(rods)
+
+    def shut(self, rods):
+        if self._is_valid_option(rods):
+            for rod in rods:
+                self._shutable.remove(rod)
+            self._diced = None
+            self._options = []
+
+    def is_game_over(self):
+        return len(self._shutable) == 0 or \
+            ( self._diced and len(self._options) == 0 )
+
+    def _can_be_shut(self, rods):
+        shutable = self._shutable.copy()
+        for rod in rods:
+            if rod in shutable:
+                shutable.remove(rod)
+            else:
+                return False
+
+    def _is_valid_option(self, rods):
+        for option in self._options:
+            if set(option) == set(rods):
+                return True
+        return False