def initialize_game(self): """ Initialize the game by asking to choose to create a new game or to load a previous one.""" game_init_method = "" while game_init_method != "load" and game_init_method != "new": game_init_method = input("Do you want to load or start a new game ? [load/new]: ") if game_init_method == "new": labyrinth_size = "" while (not labyrinth_size.isnumeric()) or int(labyrinth_size) < 4 or int(labyrinth_size) > 10: labyrinth_size = input("Please enter labyrinth size (4 to 10) :") labyrinth_size = int(labyrinth_size) self.__labyrinth = Labyrinth(labyrinth_size) player_pos_x, player_pos_y = self.__labyrinth.generate_labyrinth() self.__player = Player(int(player_pos_x), int(player_pos_y)) bear_y, bear_x = self.__labyrinth.get_bear_coord() self.__bear = Bear(int(bear_x), int(bear_y)) else: success = False while success != True: self.__player = Player(0, 0) self.__labyrinth = Labyrinth(4) filename = input("Enter the filename (without the extension): ") try: success = GameLoader().load_game([filename], self.__player, self.__labyrinth) print("loaded successfully") except: print("error while loading, verify the file name.")
def test_get_current_cell(self): labyrinth = Labyrinth(4) player_pos_x, player_pos_y = labyrinth.generate_labyrinth() player = Player(int(player_pos_x), int(player_pos_y)) self.assertEqual(isinstance(player.get_current_cell(labyrinth), Cell), True)
def test_add_objects_and_get_objects_and_remove_object(self): player = Player(0, 0) t = Treasure() self.assertEqual(len(player.get_objects()), 0) player.add_object(t) self.assertEqual(len(player.get_objects()), 1) player.remove_object(t) self.assertEqual(len(player.get_objects()), 0)
def test_treasure_execution(self): cell_treasure = CellTreasure() labyrinth = Labyrinth(4) player_pos_x, player_pos_y = labyrinth.generate_labyrinth() player = Player(int(player_pos_x), int(player_pos_y)) x, y = find_treasure(labyrinth) player.set_pos(x, y) print(has_treasure(labyrinth)) self.assertEqual(has_treasure(labyrinth), True) cell_treasure.execute_action(labyrinth, player) self.assertEqual(has_treasure(labyrinth), False) self.assertEqual(isinstance(player.get_objects()[0], Treasure), True)
def test_load(self): cmd = LoadGame() labyrinth = Labyrinth(4) player_pos_x, player_pos_y = labyrinth.generate_labyrinth() player = Player(int(player_pos_x), int(player_pos_y)) self.assertEqual(cmd.get_command_tag(), "load") self.assertEqual(cmd.get_args_count(), 1)
def test_quit(self): cmd = Quit() labyrinth = Labyrinth(4) player_pos_x, player_pos_y = labyrinth.generate_labyrinth() player = Player(int(player_pos_x), int(player_pos_y)) self.assertEqual(cmd.get_command_tag(), "quit") self.assertEqual(cmd.get_args_count(), 0) finished, message = cmd.evaluate([], labyrinth, player) self.assertEqual(finished, True) self.assertEqual(message, "Finished")
def test_show(self): cmd = ShowLabyrinth() labyrinth = Labyrinth(4) player_pos_x, player_pos_y = labyrinth.generate_labyrinth() player = Player(int(player_pos_x), int(player_pos_y)) self.assertEqual(cmd.get_command_tag(), "show") self.assertEqual(cmd.get_args_count(), 0) won, message = cmd.evaluate([], labyrinth, player) self.assertEqual(won, False) self.assertEqual(message, "labyrinth shown")
def test_go_right(self): cmd = GoRight() labyrinth = Labyrinth(4) player_pos_x, player_pos_y = labyrinth.generate_labyrinth() player = Player(int(player_pos_x), int(player_pos_y)) self.assertEqual(cmd.get_command_tag(), "go-right") self.assertEqual(cmd.get_args_count(), 0) won, message = cmd.evaluate([], labyrinth, player) self.assertEqual(isinstance(won, bool), True) self.assertEqual(isinstance(message, str), True)
def test_get_set_pos(self): player = Player(0, 0) x, y = player.get_pos() self.assertEqual(x == 0, True) self.assertEqual(y == 0, True) player.set_pos(1, 1) x, y = player.get_pos() self.assertEqual(y == 1, True) self.assertEqual(x == 1, True)
def test_eval_command(self): command_manager = CommandManager() labyrinth = Labyrinth(4) player_pos_x, player_pos_y = labyrinth.generate_labyrinth() player = Player(int(player_pos_x), int(player_pos_y)) finished, message = command_manager.eval_command( Quit(), [], labyrinth, player) self.assertEqual(finished, True) self.assertEqual(message, "Finished") finished, message = command_manager.eval_command( Quit(), ["a"], labyrinth, player) self.assertEqual(finished, False) self.assertEqual(message, "Invalid number of args. Expected: 0, got: 1")