From: Simon Cross Date: Thu, 15 May 2014 23:22:19 +0000 (+0200) Subject: Allow optional specification of a locations bits. X-Git-Tag: 0.1~244 X-Git-Url: https://git.ctpug.org.za/?p=naja.git;a=commitdiff_plain;h=d2d864e01ddcf38536812f37b8bffb3ee3227d4f Allow optional specification of a locations bits. --- diff --git a/data/location_decks/test.yaml b/data/location_decks/test.yaml index 42a3d00..a2a6e49 100644 --- a/data/location_decks/test.yaml +++ b/data/location_decks/test.yaml @@ -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 diff --git a/naja/actions.py b/naja/actions.py index ff8a431..0535c8b 100644 --- a/naja/actions.py +++ b/naja/actions.py @@ -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): diff --git a/naja/gameboard.py b/naja/gameboard.py index edda43c..3717b56 100644 --- a/naja/gameboard.py +++ b/naja/gameboard.py @@ -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, diff --git a/naja/tests/test_actions.py b/naja/tests/test_actions.py index 5cf4c76..e8be3c8 100644 --- a/naja/tests/test_actions.py +++ b/naja/tests/test_actions.py @@ -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() diff --git a/naja/tests/test_gameboard.py b/naja/tests/test_gameboard.py index 5dae954..d40873f 100644 --- a/naja/tests/test_gameboard.py +++ b/naja/tests/test_gameboard.py @@ -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]))