def test_multiple_connections_between_two_rooms(self): lb = LevelBuilder().create_level(40, 16)\ .add_room((5,5), 7, 5, {(11, 7), (11, 8)})\ .add_room((26,6), 5, 8, {(26,7), (26, 8)})\ .add_hallway((26,7), (11,7))\ .add_hallway((26,8), (11,8)) level = lb.build() actual = str(level) expected = "........................................\n" \ "........................................\n" \ "........................................\n" \ "........................................\n" \ "........................................\n" \ ".....WWWWWWW............................\n" \ ".....W W..............WWWWW.........\n" \ ".....W D--------------D W.........\n" \ ".....W D--------------D W.........\n" \ ".....WWWWWWW..............W W.........\n" \ "..........................W W.........\n" \ "..........................W W.........\n" \ "..........................W W.........\n" \ "..........................WWWWW.........\n" \ "........................................\n" \ "........................................" self.assertEqual(actual, expected, "Room with multiple connections failed.")
def test_hallway_overlap_room_invalid(self): lb = LevelBuilder().create_level(40, 16)\ .add_room((5,1), 7, 5, {(11, 3)})\ .add_room((26,2), 5, 8, {(26,3)})\ .add_room((15,2), 6, 4, {(16,2)}) level = lb.build() actual = str(level) expected = "........................................\n" \ ".....WWWWWWW............................\n" \ ".....W W...WDWWWW.....WWWWW.........\n" \ ".....W D...W W.....D W.........\n" \ ".....W W...W W.....W W.........\n" \ ".....WWWWWWW...WWWWWW.....W W.........\n" \ "..........................W W.........\n" \ "..........................W W.........\n" \ "..........................W W.........\n" \ "..........................WWWWW.........\n" \ "........................................\n" \ "........................................\n" \ "........................................\n" \ "........................................\n" \ "........................................\n" \ "........................................" self.assertEqual(actual, expected, "Three Rooms Creation Failed.") with self.assertRaises(FloorPlanOverlapError): lb.add_hallway((26, 3), (11, 3))
def test_remove_exit(self): level = LevelBuilder().create_level(20, 10)\ .add_item("key", (8,4))\ .add_item("key", (10,2))\ .add_item("exit", (8, 2))\ .add_room((5,1), 7, 5, {(11,3)}).build() actual = str(level) expected = "....................\n" \ ".....WWWWWWW........\n" \ ".....W E KW........\n" \ ".....W D........\n" \ ".....W K W........\n" \ ".....WWWWWWW........\n" \ "....................\n" \ "....................\n" \ "....................\n" \ "...................." level.remove_item("exit", (8, 2)) self.assertEqual(actual, expected, "Room Creation Failed.") actual = str(level) expected = "....................\n" \ ".....WWWWWWW........\n" \ ".....W KW........\n" \ ".....W D........\n" \ ".....W K W........\n" \ ".....WWWWWWW........\n" \ "....................\n" \ "....................\n" \ "....................\n" \ "...................." self.assertEqual(actual, expected, "Exit Removal Failed.")
def test_hallways_overlap_invalid(self): lb = LevelBuilder().create_level(40, 16)\ .add_room((5,5), 7, 5, {(11, 7)})\ .add_room((26,6), 5, 8, {(26,7)})\ .add_hallway((26,7), (11,7))\ .add_room((14,1), 6, 4, {(17,4)})\ .add_room((14,9), 7, 4, {(17,9)}) level = lb.build() actual = str(level) expected = "........................................\n" \ "..............WWWWWW....................\n" \ "..............W W....................\n" \ "..............W W....................\n" \ "..............WWWDWW....................\n" \ ".....WWWWWWW............................\n" \ ".....W W..............WWWWW.........\n" \ ".....W D--------------D W.........\n" \ ".....W W..............W W.........\n" \ ".....WWWWWWW..WWWDWWW.....W W.........\n" \ "..............W W.....W W.........\n" \ "..............W W.....W W.........\n" \ "..............WWWWWWW.....W W.........\n" \ "..........................WWWWW.........\n" \ "........................................\n" \ "........................................" self.assertEqual(actual, expected, "Four Rooms with Hallway Creation Failed.") with self.assertRaises(FloorPlanOverlapError): lb.add_hallway((17, 4), (17, 9))
def test_find_available_room_simple(self): level = LevelBuilder().create_level(20, 10)\ .add_item("key", (8,4))\ .add_item("key", (10,2))\ .add_item("exit", (8, 2))\ .add_room((5,1), 7, 5, {(11,3)}).build() actual = str(level) expected = "....................\n" \ ".....WWWWWWW........\n" \ ".....W E KW........\n" \ ".....W D........\n" \ ".....W K W........\n" \ ".....WWWWWWW........\n" \ "....................\n" \ "....................\n" \ "....................\n" \ "...................." self.assertEqual(actual, expected, "Room Creation Failed.") actual_room = level.find_available_room((0, 0)) actual_origin = actual_room.origin actual_width = actual_room.width actual_height = actual_room.height actual_doors = actual_room.doors self.assertEqual(actual_origin, (5, 1), "Unexpected origin.") self.assertEqual(actual_width, 7, "Unexpected width.") self.assertEqual(actual_height, 5, "Unexpected height.") self.assertEqual(actual_doors, {(11, 3)}, "Unexpected doors.")
def run_test(): """ 'Main' method for the test """ # Gets the tile in the level based on the user input level_json, point = read_user_input() level = LevelBuilder().create_level_from_json(swap_level(level_json)) tile = level.get_tile(*point) # Gets & prints the output output = generate_output(tile) print_json(output)
def test_empty_level(self): level = LevelBuilder().create_level(10, 3).build() actual = str(level) expected = "..........\n" \ "..........\n" \ ".........." self.assertEqual(actual, expected, "Empty Level 10x3 Test Failed.") level = LevelBuilder().create_level(2, 3).build() actual = str(level) expected = "..\n" \ "..\n" \ ".." self.assertEqual(actual, expected, "Empty Level 2x3 Test Failed.")
def test_hallway_with_waypoint(self): level = 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))\ .build() actual = str(level) expected = "........................................\n" \ ".....WWWWWWW............................\n" \ ".....W W......WWWWW.................\n" \ ".....W D------D W.................\n" \ ".....W W......W W.................\n" \ ".....WWWWWWW......W W.................\n" \ "..................W W.................\n" \ "..................W W.................\n" \ "..................W W.................\n" \ "..................WWDWW.................\n" \ "...WWWWWWWWWWWW.....-...................\n" \ "...W W.....-...................\n" \ "...W K D------...................\n" \ "...W W.........................\n" \ "...WWWWWWWWWWWW.........................\n" \ "........................................" self.assertEqual(actual, expected, "Hallway with Waypoint Creation Failed.")
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_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_find_available_room_complex(self): level = LevelBuilder().create_level(40, 16)\ .add_room((5,5), 7, 5, {(11,7)})\ .add_room((26,6), 5, 8, {(26,7)})\ .add_room((14,1), 6, 4, {(17,4)})\ .add_room((14,9), 7, 4, {(17,9)})\ .build() actual = str(level) expected = "........................................\n" \ "..............WWWWWW....................\n" \ "..............W W....................\n" \ "..............W W....................\n" \ "..............WWWDWW....................\n" \ ".....WWWWWWW............................\n" \ ".....W W..............WWWWW.........\n" \ ".....W D..............D W.........\n" \ ".....W W..............W W.........\n" \ ".....WWWWWWW..WWWDWWW.....W W.........\n" \ "..............W W.....W W.........\n" \ "..............W W.....W W.........\n" \ "..............WWWWWWW.....W W.........\n" \ "..........................WWWWW.........\n" \ "........................................\n" \ "........................................" self.assertEqual(actual, expected, "Room Creation Failed.")
def test_hallway_one_room_invalid(self): with self.assertRaises(InvalidHallwayError): level = LevelBuilder()\ .create_level(40, 16)\ .add_room((5,1), 7, 5, {(11,3)})\ .add_hallway((18,3), (11,3))\ .build()
def level_2(): return LevelBuilder()\ .create_level(40, 16)\ .add_room((5, 1), 7, 5, {(11, 3)})\ .add_room((18, 2), 5, 8, {(18, 3), (20, 9)})\ .add_item("key", (19, 5))\ .add_item("exit", (21, 5))\ .build()
def calculate_traversable_tiles(room_json, player_location): """ Returns traversable tiles in (column, row) format raise PointOutsideRoomError if origin is outside room """ # Creates level with this room room_json["origin"] = room_json["origin"][::-1] player_location = player_location[::-1] level = LevelBuilder()\ .create_level(*get_level_dimensions(room_json))\ .add_room_from_json(room_json).build() col, row = player_location if not (0 <= col <= level.num_cols) or not (0 <= row <= level.num_rows): raise PointOutsideRoomError # Checks to see if success/failure return level.get_adjacent_tiles(player_location, 1)
def test_remove_item_invalid_location(self): level = LevelBuilder().create_level(20, 10)\ .add_item("key", (8,4))\ .add_item("key", (10,2))\ .add_item("exit", (8, 2))\ .add_room((5,1), 7, 5, {(11,3)}).build() actual = str(level) expected = "....................\n" \ ".....WWWWWWW........\n" \ ".....W E KW........\n" \ ".....W D........\n" \ ".....W K W........\n" \ ".....WWWWWWW........\n" \ "....................\n" \ "....................\n" \ "....................\n" \ "...................." with self.assertRaises(KeyError): level.remove_item("key", (9, 4))
def level_1(): return 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()
def setup_communication_components(self): """ Sets up initial communication components. Creates a manager, proxy, and players. """ name_list, level_json, _, _, actor_move_list_list = self.user_input level = LevelBuilder().create_level_from_json(swap_level(level_json)) self.proxy = GameProxy() self.manager = GameManager(self.proxy, [level]) self.players = [TestingPlayer(name, actor_move_list, self.proxy) for name, actor_move_list in zip(name_list, actor_move_list_list)]
def __init__(self, level_file, num_players, initial_level, include_observer): """ Creates a Snarl Launcher """ self.levels = LevelBuilder.create_levels_from_levels_file(level_file) self.num_players = num_players self.inital_level = initial_level self.include_observer = include_observer if not constants.MIN_PLAYERS <= self.num_players <= constants.MAX_PLAYERS: print("Invalid player count: {}".format(num_players)) sys.exit()
def test_hallway_loops_back_to_room(self): lb = LevelBuilder().create_level(40, 16)\ .add_room((5,5), 7, 5, {(11, 8), (10, 9)})\ .add_hallway((10, 9), (11,8), [(10, 11), (21, 11), (21, 8)]) level = lb.build() actual = str(level) expected = "........................................\n" \ "........................................\n" \ "........................................\n" \ "........................................\n" \ "........................................\n" \ ".....WWWWWWW............................\n" \ ".....W W............................\n" \ ".....W W............................\n" \ ".....W D----------..................\n" \ ".....WWWWWDW.........-..................\n" \ "..........-..........-..................\n" \ "..........------------..................\n" \ "........................................\n" \ "........................................\n" \ "........................................\n" \ "........................................" self.assertEqual(actual, expected, "Loop back hallway failed.")
def test_level_with_room(self): level = LevelBuilder()\ .create_level(20, 10)\ .add_room((5,1), 7, 5, {(11,3)})\ .build() actual = str(level) expected = "....................\n" \ ".....WWWWWWW........\n" \ ".....W W........\n" \ ".....W D........\n" \ ".....W W........\n" \ ".....WWWWWWW........\n" \ "....................\n" \ "....................\n" \ "....................\n" \ "...................." self.assertEqual(actual, expected, "Room with Door Creation Failed.") level = LevelBuilder()\ .create_level(20, 10)\ .add_room((5,1), 7, 5, {(11,3)})\ .add_room((15,2), 5, 5, {(15,3)})\ .add_hallway((11,3), (15,3))\ .build() actual = str(level) expected = "....................\n" \ ".....WWWWWWW........\n" \ ".....W W...WWWWW\n" \ ".....W D---D W\n" \ ".....W W...W W\n" \ ".....WWWWWWW...W W\n" \ "...............WWWWW\n" \ "....................\n" \ "....................\n" \ "...................." self.assertEqual(actual, expected, "Second Room & Hallway Creation Failed.")
def test_corner_door(self): level = LevelBuilder().create_level(40,7)\ .add_room((5,1), 7, 5, {(11,5)})\ .add_room((15,1), 4, 3, {(15,3)})\ .add_room((22,1), 5, 4, {(22,1)})\ .add_room((32,1), 4, 4, {(35,1)})\ .build() actual = str(level) expected = "........................................\n" \ ".....WWWWWWW...WWWW...DWWWW.....WWWD....\n" \ ".....W W...W W...W W.....W W....\n" \ ".....W W...DWWW...W W.....W W....\n" \ ".....W W..........WWWWW.....WWWW....\n" \ ".....WWWWWWD............................\n" \ "........................................" self.assertEqual(actual, expected, "Corner Door Creation Failed.")
def test_room_with_key(self): level = LevelBuilder()\ .create_level(20, 10)\ .add_item("key", (8,4))\ .add_room((5,1), 7, 5, {(11,3)})\ .build() actual = str(level) expected = "....................\n" \ ".....WWWWWWW........\n" \ ".....W W........\n" \ ".....W D........\n" \ ".....W K W........\n" \ ".....WWWWWWW........\n" \ "....................\n" \ "....................\n" \ "....................\n" \ "...................." self.assertEqual(actual, expected, "Room with Key Creation Failed.")
def test_exits_and_keys(self): level = LevelBuilder()\ .create_level(20, 10)\ .add_item("key", (8,4))\ .add_room((5,1), 7, 5, {(11,3)})\ .add_item("key", (10,2))\ .add_item("exit", (8,2))\ .build() actual = str(level) expected = "....................\n" \ ".....WWWWWWW........\n" \ ".....W E KW........\n" \ ".....W D........\n" \ ".....W K W........\n" \ ".....WWWWWWW........\n" \ "....................\n" \ "....................\n" \ "....................\n" \ "...................." self.assertEqual(actual, expected, "Keys and Exits Failed.")
def __init__(self, level_file, max_players, wait, observe, host, port, max_adversaries=5): """ Creates a game server for a networked game of snarl. """ levels = LevelBuilder.create_levels_from_levels_file(level_file) self.__game_manager = GameManager(levels, observe, get_observation_confirmation=False) self.__timeout = wait self.__max_players = max(min(max_players, 4), 1) self.__max_adversaries = max_adversaries self.__clients = [] self.__host = host self.__port = port
def construct_state(json_object, name) -> GameState: """ Creates a GameState object from a JSON representation """ level = LevelBuilder().create_level_from_json(json_object["level"]) occupant_jsons = json_object["players"] + json_object["adversaries"] occupants = [construct_occupant(oj, level) for oj in occupant_jsons] state_dict = { "current_level": 0, "levels": [level], "is_game_over": False, "is_exit_unlocked": not json_object["exit-locked"], "occupant_order": [o.id for o in occupants], "current_turn": name, "occupants": {o.id: o for o in occupants} } return GameState.from_state_dict(state_dict)
def test_multiple_rooms(self): level = LevelBuilder()\ .create_level(40, 10)\ .add_item("key", (8,4))\ .add_room((5,1), 7, 5, {(11,3)})\ .add_room((18,2), 5, 3, {(18,3), (21,4)})\ .add_room((20,7), 13, 3, {(21,7)})\ .add_hallway((11,3), (18,3))\ .add_hallway((21,4), (21,7))\ .add_item("key", (8, 4))\ .build() actual = str(level) expected = "........................................\n" \ ".....WWWWWWW............................\n" \ ".....W W......WWWWW.................\n" \ ".....W D------D W.................\n" \ ".....W K W......WWWDW.................\n" \ ".....WWWWWWW.........-..................\n" \ ".....................-..................\n" \ "....................WDWWWWWWWWWWW.......\n" \ "....................W W.......\n" \ "....................WWWWWWWWWWWWW......." self.assertEqual(actual, expected, "Multiple Rooms Creation Failed.")
def test_room_overlap_invalid(self): with self.assertRaises(FloorPlanOverlapError): level = LevelBuilder().create_level(40, 16)\ .add_room((5,1), 7, 5, {(11, 5)})\ .add_room((7,4), 7, 5, {(8,4)})\ .build()
from src.Game.models.level.level_builder import LevelBuilder example_level = 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", (6,4))\ .add_item("exit", (7,4))\ .build() simple_level = LevelBuilder()\ .create_level(20,8)\ .add_room((1,1), 3, 5, {(3,4)})\ .add_room((6,1), 7, 6, {(6,4)})\ .add_hallway((3,4), (6,4))\ .add_item("key", (10,2))\ .add_item("exit", (8,4))\ .build()
def test_room_no_door_invalid(self): lb = LevelBuilder().create_level(20, 10) level = lb.build() with self.assertRaises(InvalidRoomError): lb.add_room((5, 1), 7, 5, {})
def test_diagonal_hallway_invalid(self): lb = LevelBuilder().create_level(40, 16)\ .add_room((5,5), 7, 5, {(11, 7)})\ .add_room((26,6), 5, 8, {(26,7)})\ .add_room((14,1), 6, 4, {(17,4)})\ .add_room((14,9), 7, 4, {(17,9)}) level = lb.build() actual = str(level) expected = "........................................\n" \ "..............WWWWWW....................\n" \ "..............W W....................\n" \ "..............W W....................\n" \ "..............WWWDWW....................\n" \ ".....WWWWWWW............................\n" \ ".....W W..............WWWWW.........\n" \ ".....W D..............D W.........\n" \ ".....W W..............W W.........\n" \ ".....WWWWWWW..WWWDWWW.....W W.........\n" \ "..............W W.....W W.........\n" \ "..............W W.....W W.........\n" \ "..............WWWWWWW.....W W.........\n" \ "..........................WWWWW.........\n" \ "........................................\n" \ "........................................" self.assertEqual(actual, expected, "Four Rooms Creation Failed.") with self.assertRaises(InvalidHallwayError): lb.add_hallway((17, 4), (26, 7)) with self.assertRaises(InvalidHallwayError): lb.add_hallway((17, 4), (26, 7), [(18, 6)]) with self.assertRaises(InvalidHallwayError): lb.add_hallway((17, 4), (26, 7), [(16, 7)]) lb.add_hallway((17, 4), (26, 7), [(17, 7)]) level = lb.build() actual = str(level) expected = "........................................\n" \ "..............WWWWWW....................\n" \ "..............W W....................\n" \ "..............W W....................\n" \ "..............WWWDWW....................\n" \ ".....WWWWWWW.....-......................\n" \ ".....W W.....-........WWWWW.........\n" \ ".....W D.....---------D W.........\n" \ ".....W W..............W W.........\n" \ ".....WWWWWWW..WWWDWWW.....W W.........\n" \ "..............W W.....W W.........\n" \ "..............W W.....W W.........\n" \ "..............WWWWWWW.....W W.........\n" \ "..........................WWWWW.........\n" \ "........................................\n" \ "........................................" self.assertEqual(actual, expected, "Hallway with Waypoint Creation Failed.")