- 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
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):
@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,
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()
[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]))