Choose locations for the board.
authorJeremy Thurgood <firxen@gmail.com>
Sun, 11 May 2014 19:35:35 +0000 (21:35 +0200)
committerJeremy Thurgood <firxen@gmail.com>
Sun, 11 May 2014 19:35:49 +0000 (21:35 +0200)
naja/gameboard.py
naja/tests/test_actions.py
naja/tests/test_gameboard.py

index 5728d5d0f28535abfeadcbb5ab3f98c3a24b74b7..de0bc1e3f31c5b4b196d41a9345747a065900595 100644 (file)
@@ -16,7 +16,7 @@ class GameBoard(object):
         self.wins_required = state['wins_required']
         self.health = state['health']
         self.wins = state['wins']
         self.wins_required = state['wins_required']
         self.health = state['health']
         self.wins = state['wins']
-        self.locations = self.import_locations(state['locations'])
+        self.locations = [item.copy() for item in state['locations']]
         self.player = player
         self.board_locations = board_locations
 
         self.player = player
         self.board_locations = board_locations
 
@@ -34,7 +34,8 @@ class GameBoard(object):
             'locations': locations_definition,
         }
         player = Player(initial_bits, initial_pos)
             'locations': locations_definition,
         }
         player = Player(initial_bits, initial_pos)
-        board_locations = cls.generate_board(locations_definition)
+        board_locations = cls.import_board_locations(
+            cls.generate_board(locations_definition))
         return cls(state, player, board_locations)
 
     @classmethod
         return cls(state, player, board_locations)
 
     @classmethod
@@ -51,14 +52,11 @@ class GameBoard(object):
             'health': self.health,
             'wins_required': self.wins_required,
             'wins': self.wins,
             'health': self.health,
             'wins_required': self.wins_required,
             'wins': self.wins,
-            'locations': self.export_locations(),
+            'locations': [item.copy() for item in self.locations],
             'player': self.player.export(),
             'board_locations': self.export_board_locations(),
         }
 
             'player': self.player.export(),
             'board_locations': self.export_board_locations(),
         }
 
-    def export_locations(self):
-        return [location.export() for location in self.locations]
-
     @classmethod
     def import_locations(cls, locations_definition):
         return [
     @classmethod
     def import_locations(cls, locations_definition):
         return [
@@ -68,7 +66,7 @@ class GameBoard(object):
     def export_board_locations(self):
         return dict(
             (position, location.export())
     def export_board_locations(self):
         return dict(
             (position, location.export())
-            for position, location in self.board_locations)
+            for position, location in self.board_locations.iteritems())
 
     @classmethod
     def import_board_locations(cls, board_locations_definition):
 
     @classmethod
     def import_board_locations(cls, board_locations_definition):
@@ -78,8 +76,13 @@ class GameBoard(object):
 
     @classmethod
     def generate_board(cls, locations_definition):
 
     @classmethod
     def generate_board(cls, locations_definition):
-        # TODO: Choose some locations.
-        return {}
+        board_locations = {}
+        for x in range(5):
+            for y in range(5):
+                board_location = LocationCard.new_location(
+                    choice(locations_definition).copy())
+                board_locations[(x, y)] = board_location.export()
+        return board_locations
 
     def lose_health(self):
         self.health -= 1
 
     def lose_health(self):
         self.health -= 1
@@ -97,7 +100,6 @@ class LocationCard(object):
 
     @classmethod
     def import_location(cls, state):
 
     @classmethod
     def import_location(cls, state):
-        # TODO: Import real locations.
         location_actions = [
             cls.build_action(definition) for definition in state['actions']]
         return cls(state['bitwise_operand'], location_actions)
         location_actions = [
             cls.build_action(definition) for definition in state['actions']]
         return cls(state['bitwise_operand'], location_actions)
index 3d3a58ea63346c936ebfb2b1c8a619640dbb5d87..d2c723a6b4082c0135da73565a44d0862afab361 100644 (file)
@@ -10,6 +10,14 @@ class TestActions(TestCase):
     def make_player(self, *bits):
         return Player(sum(1 << bit for bit in bits), None)
 
     def make_player(self, *bits):
         return Player(sum(1 << bit for bit in bits), None)
 
+    def make_board(self, player_bits=None, locations=None):
+        if locations is None:
+            locations = [{'actions': []}]
+        board = GameBoard.new_game(locations)
+        if player_bits is not None:
+            board.player.bits.set_bits(player_bits)
+        return board
+
     def test_check_available(self):
         def check_available(action_bits, player_bits, expected_result):
             action = actions.LocationAction(action_bits)
     def test_check_available(self):
         def check_available(action_bits, player_bits, expected_result):
             action = actions.LocationAction(action_bits)
@@ -22,14 +30,14 @@ class TestActions(TestCase):
         check_available(set([BITS.MSB]), [BITS.MSB], True)
 
     def test_DoNothing(self):
         check_available(set([BITS.MSB]), [BITS.MSB], True)
 
     def test_DoNothing(self):
-        board = GameBoard.new_game([])
+        board = self.make_board()
         state_before = board.export()
         actions.DoNothing(set()).perform_action(board, None)
         state_after = board.export()
         self.assertEqual(state_before, state_after)
 
     def test_LoseHealthOrMSB_MSB_clear(self):
         state_before = board.export()
         actions.DoNothing(set()).perform_action(board, None)
         state_after = board.export()
         self.assertEqual(state_before, state_after)
 
     def test_LoseHealthOrMSB_MSB_clear(self):
-        board = GameBoard.new_game([])
+        board = self.make_board()
         state_before = board.export()
         actions.LoseHeathOrMSB(set()).perform_action(board, None)
         state_after = board.export()
         state_before = board.export()
         actions.LoseHeathOrMSB(set()).perform_action(board, None)
         state_after = board.export()
@@ -40,8 +48,7 @@ class TestActions(TestCase):
         self.assertEqual(state_before, state_after)
 
     def test_LoseHealthOrMSB_MSB_set(self):
         self.assertEqual(state_before, state_after)
 
     def test_LoseHealthOrMSB_MSB_set(self):
-        board = GameBoard.new_game([])
-        board.player.bits.set_bit(BITS.MSB)
+        board = self.make_board(player_bits=[BITS.MSB])
         state_before = board.export()
         actions.LoseHeathOrMSB(set()).perform_action(board, None)
         state_after = board.export()
         state_before = board.export()
         actions.LoseHeathOrMSB(set()).perform_action(board, None)
         state_after = board.export()
@@ -52,7 +59,7 @@ class TestActions(TestCase):
         self.assertEqual(state_before, state_after)
 
     def test_SetBits(self):
         self.assertEqual(state_before, state_after)
 
     def test_SetBits(self):
-        board = GameBoard.new_game([])
+        board = self.make_board()
         state_before = board.export()
         location = LocationCard(set([BITS.MSB, BITS.NORTH]), [])
         actions.SetBits(set()).perform_action(board, location)
         state_before = board.export()
         location = LocationCard(set([BITS.MSB, BITS.NORTH]), [])
         actions.SetBits(set()).perform_action(board, location)
@@ -65,8 +72,7 @@ class TestActions(TestCase):
         self.assertEqual(state_before, state_after)
 
     def test_ToggleBits(self):
         self.assertEqual(state_before, state_after)
 
     def test_ToggleBits(self):
-        board = GameBoard.new_game([])
-        board.player.bits.set_bit(BITS.NORTH)
+        board = self.make_board(player_bits=[BITS.NORTH])
         state_before = board.export()
         location = LocationCard(set([BITS.MSB, BITS.NORTH]), [])
         actions.ToggleBits(set()).perform_action(board, location)
         state_before = board.export()
         location = LocationCard(set([BITS.MSB, BITS.NORTH]), [])
         actions.ToggleBits(set()).perform_action(board, location)
index a825544b6bf7727e3a8eb60dfa78dba241684080..9d39fe85a4e34a2fcb7a5ad3290ce4104ad5ed7f 100644 (file)
@@ -7,19 +7,28 @@ from naja import actions
 
 class TestGameBoard(TestCase):
     def test_export_new_board(self):
 
 class TestGameBoard(TestCase):
     def test_export_new_board(self):
-        board = GameBoard.new_game([])
-        self.assertEqual(board.export(), {
+        board = GameBoard.new_game([{'actions': []}])
+        exported_state = board.export()
+        board_locations = exported_state.pop('board_locations')
+        self.assertEqual(exported_state, {
             'max_health': 4,
             'health': 4,
             'wins_required': 4,
             'wins': 0,
             'max_health': 4,
             'health': 4,
             'wins_required': 4,
             'wins': 0,
-            'locations': [],
-            'board_locations': {},
+            'locations': [{'actions': []}],
             'player': board.player.export(),
         })
             'player': board.player.export(),
         })
+        self.assertEqual(
+            set(board_locations.keys()),
+            set((x, y) for x in range(5) for y in range(5)))
+        for location_state in board_locations.values():
+            self.assertEqual(
+                sorted(location_state.keys()), ['actions', 'bitwise_operand'])
+            self.assertEqual(location_state['actions'], [])
+            self.assertTrue(2 <= len(location_state['bitwise_operand']) <= 3)
 
     def test_lose_health(self):
 
     def test_lose_health(self):
-        board = GameBoard.new_game([])
+        board = GameBoard.new_game([{'actions': []}])
         self.assertEqual(board.health, 4)
         state_1 = board.export()
 
         self.assertEqual(board.health, 4)
         state_1 = board.export()