Allow optional specification of a locations bits.
authorSimon Cross <hodgestar@gmail.com>
Thu, 15 May 2014 23:22:19 +0000 (01:22 +0200)
committerSimon Cross <hodgestar@gmail.com>
Thu, 15 May 2014 23:22:19 +0000 (01:22 +0200)
data/location_decks/test.yaml
naja/actions.py
naja/gameboard.py
naja/tests/test_actions.py
naja/tests/test_gameboard.py

index 42a3d0004cd730cdff1dd700a5f945688775e211..a2a6e496a5c3a00a18598bd8d752a2169ae6e42e 100644 (file)
@@ -44,3 +44,7 @@ cards:
     - action_class: 'ShiftLocations'
       required_bits: [GREEN, BLUE]
       data: {'direction': NORTH}
+  - bits: [RED, GREEN]  # colour-blind robot!
+    actions:
+    - action_class: 'ToggleBits'
+      required_bits: [GREEN]
\ No newline at end of file
index ff8a431d0369fe0fa6f1ed1a52cbda87126fd538..0535c8be1b37964d752773636d2538c877adc78b 100644 (file)
@@ -10,11 +10,7 @@ class LocationAction(object):
     USES_MSB = False
 
     def __init__(self, required_bits, **data):
-        bits = set()
-        for bit in required_bits:
-            # Convert names to numbers if applicable.
-            bits.add(BITS.get(bit, bit))
-        self.required_bits = frozenset(bits)
+        self.required_bits = required_bits
         self.data = data
 
     def get_text(self):
index edda43c702c146211ec0fca2f016e44c89f5c626..3717b564dcf0809a5ebda745f283bdfd7dd73ca8 100644 (file)
@@ -170,17 +170,26 @@ class LocationCard(object):
     @classmethod
     def build_action(cls, definition):
         action_class = getattr(actions, definition['action_class'])
-        required_bits = definition['required_bits']
+        required_bits = cls.parse_bits(definition['required_bits'])
         data = definition.get('data', {})
         return action_class(required_bits, **data)
 
     @classmethod
     def new_location(cls, definition):
+        if 'bits' in definition:
+            bits = cls.parse_bits(definition['bits'])
+        else:
+            bits = cls.generate_bitwise_operand()
         return cls.import_location({
-            'bitwise_operand': cls.generate_bitwise_operand(),
+            'bitwise_operand': bits,
             'actions': definition['actions'],
         })
 
+    @classmethod
+    def parse_bits(self, bit_list):
+        # Convert names to numbers if applicable.
+        return frozenset(BITS.get(bit, bit) for bit in bit_list)
+
     def export(self):
         return {
             'bitwise_operand': self.bitwise_operand,
index 5cf4c76011958267a324087a7a5eae9706fe6b19..e8be3c8ca8dd60c98b728e97b52f640a938f4dee 100644 (file)
@@ -77,10 +77,6 @@ class TestActions(TestCase):
         action_west = DirectionAction([], direction='WEST')
         self.assertEqual(action_west.get_text(), "foo WEST row")
 
-    def test_bits_translation(self):
-        action = actions.LocationAction(set([BITS.NORTH, 'MSB']))
-        self.assertEqual(action.required_bits, set([BITS.NORTH, BITS.MSB]))
-
     def test_DoNothing(self):
         board = self.make_board()
         state_before = board.export()
index 5dae954fafaae21a532b7a7d842b408f6d210101..d40873f12e707e78ef04a34e5322564c2812ed28 100644 (file)
@@ -155,3 +155,14 @@ class TestLocationCard(TestCase):
         [action] = location.actions
         self.assertEqual(type(action), actions.DoNothing)
         self.assertEqual(action.required_bits, set())
+
+    def test_parse_bits(self):
+        self.assertEqual(
+            LocationCard.parse_bits([]), frozenset([]))
+        self.assertEqual(
+            LocationCard.parse_bits(['RED']), frozenset([BITS.RED]))
+        self.assertEqual(
+            LocationCard.parse_bits([BITS.BLUE]), frozenset([BITS.BLUE]))
+        self.assertEqual(
+            LocationCard.parse_bits([BITS.NORTH, 'MSB']),
+            frozenset([BITS.NORTH, BITS.MSB]))