From: Jeremy Thurgood Date: Sun, 11 May 2014 19:35:35 +0000 (+0200) Subject: Choose locations for the board. X-Git-Tag: 0.1~392^2~2 X-Git-Url: https://git.ctpug.org.za/?a=commitdiff_plain;h=5ef59d0391e208e0ef9d4fa998b3a8e653cc57bf;p=naja.git Choose locations for the board. --- diff --git a/naja/gameboard.py b/naja/gameboard.py index 5728d5d..de0bc1e 100644 --- a/naja/gameboard.py +++ b/naja/gameboard.py @@ -16,7 +16,7 @@ class GameBoard(object): 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 @@ -34,7 +34,8 @@ class GameBoard(object): '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 @@ -51,14 +52,11 @@ class GameBoard(object): '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(), } - def export_locations(self): - return [location.export() for location in self.locations] - @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()) - for position, location in self.board_locations) + for position, location in self.board_locations.iteritems()) @classmethod def import_board_locations(cls, board_locations_definition): @@ -78,8 +76,13 @@ class GameBoard(object): @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 @@ -97,7 +100,6 @@ class LocationCard(object): @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) diff --git a/naja/tests/test_actions.py b/naja/tests/test_actions.py index 3d3a58e..d2c723a 100644 --- a/naja/tests/test_actions.py +++ b/naja/tests/test_actions.py @@ -10,6 +10,14 @@ class TestActions(TestCase): 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) @@ -22,14 +30,14 @@ class TestActions(TestCase): 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): - board = GameBoard.new_game([]) + board = self.make_board() 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): - 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() @@ -52,7 +59,7 @@ class TestActions(TestCase): 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) @@ -65,8 +72,7 @@ class TestActions(TestCase): 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) diff --git a/naja/tests/test_gameboard.py b/naja/tests/test_gameboard.py index a825544..9d39fe8 100644 --- a/naja/tests/test_gameboard.py +++ b/naja/tests/test_gameboard.py @@ -7,19 +7,28 @@ from naja import actions 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, - 'locations': [], - 'board_locations': {}, + 'locations': [{'actions': []}], '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): - board = GameBoard.new_game([]) + board = GameBoard.new_game([{'actions': []}]) self.assertEqual(board.health, 4) state_1 = board.export()