]> git.treefish.org Git - shutbox.git/commitdiff
added random agent
authorAlexander Schmidt <alex@treefish.org>
Mon, 12 Oct 2020 11:01:38 +0000 (13:01 +0200)
committerAlexander Schmidt <alex@treefish.org>
Mon, 12 Oct 2020 11:01:38 +0000 (13:01 +0200)
.gitignore [new file with mode: 0644]
src/game.py
src/random-agent.py [new file with mode: 0755]

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..9b5e2e8
--- /dev/null
@@ -0,0 +1,2 @@
+*~
+__pycache__
index 50d83c024b130ac2f76613bcbdd5fd94616728df..5e2fe70df5f0f58f733f7e72e1b46affbeda6392 100644 (file)
@@ -1,5 +1,3 @@
-#!/usr/bin/env python3
-
 import random
 
 class Game:
@@ -7,30 +5,38 @@ class Game:
         self._shutable = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
         self._diced = None
         self._options = []
+        self._score = 0
 
     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] ] ]:
+            self._diced = [random.randint(1, 6), random.randint(1, 6)]
+            for rods in [ self._diced,
+                          [ abs(self._diced[0] - self._diced[1]) ],
+                          [ self._diced[0] + self._diced[1] ] ]:
                 if self._can_be_shut(rods):
                     self._options.append(rods)
 
     def get_dice(self):
         return self._diced
 
+    def get_options(self):
+        return self._options
+
     def shut(self, rods):
         if self._is_valid_option(rods):
             for rod in rods:
+                self._score += rod
                 self._shutable.remove(rod)
             self._diced = None
             self._options = []
 
-    def is_game_over(self):
+    def is_over(self):
         return len(self._shutable) == 0 or \
             ( self._diced and len(self._options) == 0 )
 
+    def get_score(self):
+        return self._score
+
     def _can_be_shut(self, rods):
         shutable = self._shutable.copy()
         for rod in rods:
@@ -38,6 +44,7 @@ class Game:
                 shutable.remove(rod)
             else:
                 return False
+        return True
 
     def _is_valid_option(self, rods):
         for option in self._options:
diff --git a/src/random-agent.py b/src/random-agent.py
new file mode 100755 (executable)
index 0000000..d9354a1
--- /dev/null
@@ -0,0 +1,23 @@
+#!/usr/bin/env python3
+
+from game import Game
+import random
+
+def play_game():
+    g = Game()
+    while not g.is_over():
+        g.dice()
+        options = g.get_options()
+        if len(options) > 0:
+            choice = random.randint(0, len(options) - 1)
+            g.shut(options[choice])
+    return g.get_score()
+
+avg_score = 0.0
+
+for i in range(0, 10000):
+    avg_score += play_game()
+
+avg_score /= 10000
+
+print(avg_score)