def test_create_state_dict(self): level_1 = LevelBuilder()\ .create_level(40,16)\ .add_room((5,1), 7, 5, {(11,3)})\ .add_room((18,2), 5, 8, {(18,3), (20,9)})\ .add_room((3,10), 12, 5, {(14,12)})\ .add_hallway((18,3), (11,3))\ .add_hallway((14,12), (20,9), [(20,12)])\ .add_item("key", (7,12))\ .add_item("exit", (7,13))\ .build() p100 = Player("p100") p101 = Player("p101") a0 = Zombie("a0") adversaries={0: [a0]} actual = GameState(levels=[level_1], players=[p100, p101], adversaries=adversaries).to_dict() expected = { "occupants": {"p100": p100, "p101": p101, "a0": a0}, "levels": [level_1], "current_level": -1, "current_turn": "p100", "is_exit_unlocked": False, "is_game_over": False } self.assertEqual(actual["occupants"], expected["occupants"], "Occupants mismatch") self.assertEqual(actual["current_level"], expected["current_level"], "Current level mismatch") self.assertEqual(actual["current_turn"], expected["current_turn"], "Current turn mismatch") self.assertEqual(actual["is_exit_unlocked"], expected["is_exit_unlocked"], "is_exit_unlocked mismatch") self.assertEqual(actual["is_game_over"], expected["is_game_over"], "is_game_over mismatch")
def test_load_state_dict(self): level_1 = LevelBuilder()\ .create_level(40,16)\ .add_room((5,1), 7, 5, {(11,3)})\ .add_room((18,2), 5, 8, {(18,3), (20,9)})\ .add_room((3,10), 12, 5, {(14,12)})\ .add_hallway((18,3), (11,3))\ .add_hallway((14,12), (20,9), [(20,12)])\ .add_item("key", (7,12))\ .add_item("exit", (7,13))\ .build() p0 = Player("p0") p1 = Player("p1") a0 = Zombie("a0") adversaries={0: [a0]} test_dict = { "occupants": {"p0": p0, "p1": p1, "a0": a0}, "_all_adversaries": {0: [a0]}, "levels": [level_1], "current_level": -1, "current_turn": "p0", "is_exit_unlocked": False, "is_game_over": False, "occupant_order": ["p0", "p1", "a0"] } actual = GameState.from_state_dict(test_dict) expected = GameState(levels=[level_1], players=[p0, p1], adversaries=adversaries) self.assertEqual(actual.occupants, expected.occupants, "Occupants mismatch") self.assertEqual(actual.current_level, expected.current_level, "Current level mismatch") self.assertEqual(actual.current_turn, expected.current_turn, "Current turn mismatch") self.assertEqual(actual.is_exit_unlocked, expected.is_exit_unlocked, "is_exit_unlocked mismatch") self.assertEqual(actual.is_game_over, expected.is_game_over, "is_game_over mismatch") self.assertEqual(actual.occupant_order, expected.occupant_order, "occupant order mismatch")
def test_reset_health(self): p1 = Player("p1") self.assertEqual(p1.health, constants.PLAYER_HEALTH) p1.health = 0 self.assertEqual(p1.health, 0) p1.reset_health() self.assertEqual(p1.health, constants.PLAYER_HEALTH)
def test_receive_attack(self): p1 = Player("p1") a1 = Zombie("a1") self.assertEqual(p1.health, constants.PLAYER_HEALTH) p1.receive_attack(a1) self.assertEqual( p1.health, constants.PLAYER_HEALTH - constants.ZOMBIE_HIT_STRENGTH)
def test_interact_with_adversary(self): # interacting with adversary = receiving an attack from adversary p1 = Player("p1") a1 = Zombie("a1") self.assertEqual(p1.health, constants.PLAYER_HEALTH) p1.interact_with_adversary(a1) self.assertEqual( p1.health, constants.PLAYER_HEALTH - constants.ZOMBIE_HIT_STRENGTH)
def test_to_json(self): p1 = Player("p1") t1 = Tile(3, 4) p1.move(t1) expected = { "type": constants.PLAYER_ACTOR_TYPE, "name": "p1", "position": (4, 3) } actual = p1.to_json() self.assertEqual(expected, actual)
def test_interact_with_occupant(self): p1 = Player("p1") a1 = Zombie("a1") # player receives zombie attack self.assertEqual(a1.health, constants.ZOMBIE_HEALTH) a1.interact_with_occupant(p1) self.assertEqual( p1.health, constants.PLAYER_HEALTH - constants.ZOMBIE_HIT_STRENGTH) self.assertEqual(a1.health, constants.ZOMBIE_HEALTH) # zombie receives player attack p1.interact_with_occupant(a1) self.assertEqual( p1.health, constants.PLAYER_HEALTH - constants.ZOMBIE_HIT_STRENGTH) self.assertEqual( a1.health, constants.ZOMBIE_HEALTH - constants.PLAYER_HIT_STRENGTH)
def create_basic_game_state_ghost() -> GameState: players = [Player("p1")] adversaries = [Ghost("a1")] game_state = GameState([level_1()], players, {0: adversaries}) game_state.progress_to_next_level(False) return game_state
def test_interact_with_player(self): # interacting with player = receiving an attack from player p1 = Player("p1") a1 = Zombie("a1") self.assertEqual(a1.health, constants.ZOMBIE_HEALTH) a1.receive_attack(p1) self.assertEqual( a1.health, constants.ZOMBIE_HEALTH - constants.PLAYER_HIT_STRENGTH)
def create_two_level_game_state(num_players=3, two_levels=True) -> GameState: players = [Player("p" + str(i)) for i in range(num_players)] adversaries = [Zombie("a" + str(i)) for i in range(5)] levels = [level_1(), level_2()] if two_levels else [level_1()] game_state = GameState(levels, players, {0: adversaries, 1: []}) game_state.progress_to_next_level(False) return game_state
def __create_initial_game_state(self): """ Creates inital game state. """ actor_type_to_object_type = { constants.GHOST_ACTOR_TYPE: Ghost, constants.ZOMBIE_ACTOR_TYPE: Zombie } adversary_dict = { level: [actor_type_to_object_type[adversary.provide_adversary_type()](adversary.name, level_number=level) \ for adversary in dict_of_adversaries.values()] for level, dict_of_adversaries in self.__adversaries.items() } self.state = GameState(self.__levels, [Player(name) for name in self.__players], adversary_dict) for _ in range(self.__initial_level): self.state.progress_to_next_level()
def test_move(self): """ Further movement based interactions are tested in test_state and test_adversary_strategies. """ p1 = Player("p1") a1 = Zombie("a1") for occupant in [p1, a1]: t1 = Tile(3, 4) t2 = Tile(4, 5) self.assertIsNone(occupant.current_tile) occupant.move(t1) self.assertEqual(occupant.current_tile, t1) self.assertEqual(t1.current_occupant, occupant) occupant.move(t2) self.assertEqual(occupant.current_tile, t2) self.assertEqual(t2.current_occupant, occupant) self.assertIsNone(t1.current_occupant)
def test_occupant_creation(self): p1 = Player("p1") self.assertEqual(p1.id, "p1") self.assertIsNone(p1.current_tile) self.assertTrue(p1.is_alive)
def test_expel(self): p1 = Player("p1") self.assertTrue(p1.is_alive) p1.expel() self.assertFalse(p1.is_alive)