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
'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
'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 [
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 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
@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)
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)
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()
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()
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)
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)
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()