Add a card_name parameter
authorNeil <neil@dip.sun.ac.za>
Sat, 17 May 2014 12:28:11 +0000 (14:28 +0200)
committerNeil <neil@dip.sun.ac.za>
Sat, 17 May 2014 12:40:37 +0000 (14:40 +0200)
naja/gameboard.py
naja/tests/test_actions.py
naja/tests/test_gameboard.py

index 7d8916ed9d328e0401bc6fff5dd2ed8ed7ad2270..944f21fadac1ba732de90e4b797b1c5fadab8860 100644 (file)
@@ -221,7 +221,8 @@ class LocationCard(object):
     A particular set of options available on a location.
     """
 
-    def __init__(self, bitwise_operand, location_actions):
+    def __init__(self, card_name, bitwise_operand, location_actions):
+        self.card_name = card_name
         self.bitwise_operand = bitwise_operand
         self.actions = location_actions
         self.check_actions()
@@ -230,7 +231,8 @@ class LocationCard(object):
     def import_location(cls, state):
         location_actions = [
             cls.build_action(definition) for definition in state['actions']]
-        return cls(state['bitwise_operand'], location_actions)
+        return cls(state['card_name'], state['bitwise_operand'],
+                   location_actions)
 
     @classmethod
     def build_action(cls, definition):
@@ -245,9 +247,11 @@ class LocationCard(object):
             bits = cls.parse_bits(definition['bits'])
         else:
             bits = cls.generate_bitwise_operand()
+        card_name = definition['card_name']
         return cls.import_location({
             'bitwise_operand': bits,
             'actions': definition['actions'],
+            'card_name': card_name,
         })
 
     @classmethod
@@ -259,6 +263,7 @@ class LocationCard(object):
         return {
             'bitwise_operand': sorted(self.bitwise_operand),
             'actions': [action.export() for action in self.actions],
+            'card_name': self.card_name,
         }
 
     def check_actions(self):
index d770f12397f04ffec487192ba299a8445f51a3a1..0717f0cfd6899cf3b15eaba3dab3a28bf47e6bcf 100644 (file)
@@ -19,7 +19,7 @@ class TestActions(TestCase):
 
     def make_board(self, player_bits=None, locations=None):
         if locations is None:
-            locations = [{'actions': []}]
+            locations = [{'card_name': 'card', 'actions': []}]
         board = GameBoard.new_game({'cards': locations})
         if player_bits is not None:
             board.player.bits.bits = 0
@@ -107,7 +107,7 @@ class TestActions(TestCase):
     def test_SetBits(self):
         board = self.make_board()
         state_before = board.export()
-        card = LocationCard(set([BITS.MSB, BITS.NORTH]), [])
+        card = LocationCard('card', set([BITS.MSB, BITS.NORTH]), [])
         actions.SetBits(set()).perform_action(board, card)
         state_after = board.export()
         self.assertEqual(
@@ -117,7 +117,7 @@ class TestActions(TestCase):
     def test_ToggleBits(self):
         board = self.make_board(player_bits=[BITS.NORTH])
         state_before = board.export()
-        card = LocationCard(set([BITS.MSB, BITS.NORTH]), [])
+        card = LocationCard('card', set([BITS.MSB, BITS.NORTH]), [])
         actions.ToggleBits(set()).perform_action(board, card)
         state_after = board.export()
         self.assertEqual(board.player.bits.check_bit(BITS.MSB), True)
@@ -127,7 +127,7 @@ class TestActions(TestCase):
     def test_LoseHealthOrMSBAndSetBits_MSB_clear(self):
         board = self.make_board(player_bits=[])
         state_before = board.export()
-        card = LocationCard(set([BITS.BLUE, BITS.NORTH]), [])
+        card = LocationCard('card', set([BITS.BLUE, BITS.NORTH]), [])
         actions.LoseHealthOrMSBAndSetBits(set()).perform_action(board, card)
         state_after = board.export()
         self.assertEqual(state_after['health'], state_before['health'] - 1)
@@ -139,7 +139,7 @@ class TestActions(TestCase):
     def test_LoseHealthOrMSBAndSetBits_MSB_set(self):
         board = self.make_board(player_bits=[BITS.MSB])
         state_before = board.export()
-        card = LocationCard(set([BITS.BLUE, BITS.NORTH]), [])
+        card = LocationCard('card', set([BITS.BLUE, BITS.NORTH]), [])
         actions.LoseHealthOrMSBAndSetBits(set()).perform_action(board, card)
         state_after = board.export()
         self.assert_player_bits(board, BITS.BLUE, BITS.NORTH)
@@ -148,7 +148,7 @@ class TestActions(TestCase):
     def test_LoseHealthOrMSBAndSetBits_MSB_set_and_on_card(self):
         board = self.make_board(player_bits=[BITS.MSB])
         state_before = board.export()
-        card = LocationCard(set([BITS.MSB, BITS.NORTH]), [])
+        card = LocationCard('card', set([BITS.MSB, BITS.NORTH]), [])
         actions.LoseHealthOrMSBAndSetBits(set()).perform_action(board, card)
         state_after = board.export()
         self.assert_player_bits(board, BITS.MSB, BITS.NORTH)
@@ -170,7 +170,7 @@ class TestActions(TestCase):
         board = self.make_board(player_bits=[BITS.NORTH])
         board.lose_health()
         state_before = board.export()
-        card = LocationCard(set([BITS.BLUE, BITS.NORTH]), [])
+        card = LocationCard('card', set([BITS.BLUE, BITS.NORTH]), [])
         actions.GainHealthAndClearBitsOrMSB(set()).perform_action(board, card)
         state_after = board.export()
         self.assertEqual(state_after['health'], state_before['health'] + 1)
@@ -183,7 +183,7 @@ class TestActions(TestCase):
         board = self.make_board(player_bits=[BITS.MSB, BITS.NORTH])
         board.lose_health()
         state_before = board.export()
-        card = LocationCard(set([BITS.BLUE, BITS.NORTH]), [])
+        card = LocationCard('card', set([BITS.BLUE, BITS.NORTH]), [])
         actions.GainHealthAndClearBitsOrMSB(set()).perform_action(board, card)
         state_after = board.export()
         self.assertEqual(state_after['health'], state_before['health'] + 1)
index 2db02a78b436d8bdf72dabb9c5f888e0fd547285..5b383991273e9d20f919a54cfec4104913ffcbae 100644 (file)
@@ -12,7 +12,7 @@ class TestGameBoard(TestCase):
 
     def make_deck(self, cards=None):
         if cards is None:
-            cards = [{'actions': []}]
+            cards = [{'card_name': 'card', 'actions': []}]
         return {'cards': cards}
 
     def assert_state(self, state1, state2, exclude=(), player_exclude=()):
@@ -30,7 +30,8 @@ class TestGameBoard(TestCase):
         self.assertEqual(state1, state2)
 
     def test_export_new_board(self):
-        board = GameBoard.new_game({'cards': [{'actions': [
+        board = GameBoard.new_game({'cards': [
+            {'card_name' : 'card1', 'actions': [
             {
                 'action_class': 'LoseHealthOrMSB',
                 'required_bits': [],
@@ -46,7 +47,7 @@ class TestGameBoard(TestCase):
             'health': 4,
             'wins_required': 4,
             'wins': 0,
-            'locations': [{'actions': [
+            'locations': [{'card_name' : 'card1', 'actions': [
                 {
                     'action_class': 'LoseHealthOrMSB',
                     'required_bits': [],
@@ -63,7 +64,8 @@ class TestGameBoard(TestCase):
         for position, location_state in board_locations:
             positions.append(position)
             self.assertEqual(
-                sorted(location_state.keys()), ['actions', 'bitwise_operand'])
+                sorted(location_state.keys()), ['actions', 'bitwise_operand',
+                                                'card_name'])
             self.assertEqual(location_state['actions'], [
                 {
                     'action_class': 'LoseHealthOrMSB',
@@ -244,14 +246,15 @@ class TestLocationCard(TestCase):
         self.assertEqual(bits, set(BITS.values()))
 
     def test_new_location_no_actions(self):
-        location = LocationCard.new_location({'actions': []})
+        location = LocationCard.new_location({'card_name': 'card',
+                                              'actions': []})
         [action] = location.actions
         self.assertEqual(type(action), actions.DoNothing)
         self.assertEqual(action.required_bits, set())
 
     def test_new_location_one_action(self):
-        location = LocationCard.new_location({'actions': [
-            {'required_bits': [], 'action_class': 'DoNothing'},
+        location = LocationCard.new_location({'card_name': 'card1',
+            'actions': [{'required_bits': [], 'action_class': 'DoNothing'},
         ]})
         [action] = location.actions
         self.assertEqual(type(action), actions.DoNothing)