Decouple action from required bits.
authorJeremy Thurgood <firxen@gmail.com>
Sun, 11 May 2014 15:52:57 +0000 (17:52 +0200)
committerJeremy Thurgood <firxen@gmail.com>
Sun, 11 May 2014 15:52:57 +0000 (17:52 +0200)
naja/gameboard.py
naja/player.py
naja/tests/test_actions.py
naja/tests/test_player.py

index 5cbecb9bdac04663cdbface35eba1dc6b1bfaaca..9c5af7b3554382158c79df337742e605c6b819a7 100644 (file)
@@ -80,13 +80,13 @@ class LocationAction(object):
     """
 
     TEXT = None
-    REQUIRED_BITS = frozenset()
 
-    def __init__(self, **data):
+    def __init__(self, required_bits, **data):
+        self.required_bits = required_bits
         self.data = data
 
     def check_available(self, player):
-        return player.bits.check_bits(*self.REQUIRED_BITS)
+        return player.bits.check_bits(self.required_bits)
 
     def perform_action(self, player, board):
         raise NotImplementedError("TODO")
index acae919c0c46c0b2d14338ca11d0c87289ab7df4..fcfc3301de4ab22bdfed7982b210d4acc73a29e3 100644 (file)
@@ -33,18 +33,18 @@ class PlayerBits(object):
 
     # Operate on sets of bits
 
-    def check_bits(self, *bits):
+    def check_bits(self, bits):
         return all(self.check_bit(bit) for bit in bits)
 
-    def set_bits(self, *bits):
+    def set_bits(self, bits):
         for bit in bits:
             self.set_bit(bit)
 
-    def clear_bits(self, *bits):
+    def clear_bits(self, bits):
         for bit in bits:
             self.clear_bit(bit)
 
-    def toggle_bits(self, *bits):
+    def toggle_bits(self, bits):
         for bit in bits:
             self.toggle_bit(bit)
 
index bf04e21eacb9c4091c91a25fb3870d622f690c0f..8896b8d2607e72a6bc0ac034fba8b7f2a2a75f66 100644 (file)
@@ -10,10 +10,10 @@ class TestActions(TestCase):
 
     def test_DoNothing_check_available(self):
         player = self.make_player()
-        action = actions.DoNothing()
+        action = actions.DoNothing(set())
         self.assertEqual(action.check_available(player), True)
 
     def test_LoseHealthOrMSB_check_available(self):
         player = self.make_player()
-        action = actions.LoseHeathOrMSB()
+        action = actions.LoseHeathOrMSB(set())
         self.assertEqual(action.check_available(player), True)
index 8373eee2d53a5cc35d9049854646bf6a0ea947d8..d2b896e402b3983c6b594d5e3c12c24f0304b482 100644 (file)
@@ -52,35 +52,35 @@ class TestPlayerBits(TestCase):
 
     def test_check_bits(self):
         bits = PlayerBits(0x03)
-        self.assertEqual(bits.check_bits(BITS.NORTH), True)
-        self.assertEqual(bits.check_bits(BITS.NORTH, BITS.SOUTH), True)
-        self.assertEqual(bits.check_bits(BITS.CYAN), False)
-        self.assertEqual(bits.check_bits(BITS.CYAN, BITS.MSB), False)
+        self.assertEqual(bits.check_bits([BITS.NORTH]), True)
+        self.assertEqual(bits.check_bits([BITS.NORTH, BITS.SOUTH]), True)
+        self.assertEqual(bits.check_bits([BITS.CYAN]), False)
+        self.assertEqual(bits.check_bits([BITS.CYAN, BITS.MSB]), False)
         self.assertEqual(
-            bits.check_bits(BITS.NORTH, BITS.SOUTH, BITS.CYAN), False)
+            bits.check_bits([BITS.NORTH, BITS.SOUTH, BITS.CYAN]), False)
 
     def test_set_bits(self):
         bits = PlayerBits(0x03)
         self.assertEqual(bits._bits, 0x03)
-        bits.set_bits(BITS.NORTH, BITS.CYAN)
+        bits.set_bits([BITS.NORTH, BITS.CYAN])
         self.assertEqual(bits._bits, 0x13)
-        bits.set_bits(BITS.NORTH, BITS.CYAN, BITS.MSB)
+        bits.set_bits([BITS.NORTH, BITS.CYAN, BITS.MSB])
         self.assertEqual(bits._bits, 0x93)
 
     def test_clear_bits(self):
         bits = PlayerBits(0x03)
         self.assertEqual(bits._bits, 0x03)
-        bits.clear_bits(BITS.NORTH, BITS.CYAN)
+        bits.clear_bits([BITS.NORTH, BITS.CYAN])
         self.assertEqual(bits._bits, 0x02)
-        bits.clear_bits(BITS.NORTH, BITS.CYAN, BITS.MSB)
+        bits.clear_bits([BITS.NORTH, BITS.CYAN, BITS.MSB])
         self.assertEqual(bits._bits, 0x02)
 
     def test_toggle_bits(self):
         bits = PlayerBits(0x03)
         self.assertEqual(bits._bits, 0x03)
-        bits.toggle_bits(BITS.NORTH, BITS.CYAN)
+        bits.toggle_bits([BITS.NORTH, BITS.CYAN])
         self.assertEqual(bits._bits, 0x12)
-        bits.toggle_bits(BITS.NORTH, BITS.CYAN)
+        bits.toggle_bits([BITS.NORTH, BITS.CYAN])
         self.assertEqual(bits._bits, 0x03)
-        bits.toggle_bits(BITS.NORTH, BITS.CYAN, BITS.MSB)
+        bits.toggle_bits([BITS.NORTH, BITS.CYAN, BITS.MSB])
         self.assertEqual(bits._bits, 0x92)