예제 #1
0
    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)
예제 #2
0
    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_generate_random_exit(self):
        laby = Labyrinth(4)
        laby.generate_labyrinth()

        e_x, e_y = laby.generate_random_exit()

        self.assertEqual((e_x == 0 or e_x == 8) or (e_y == 8 or e_y == 0),
                         True)
예제 #4
0
    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)
예제 #5
0
class Game:

    def __init__(self):
        print("Welcome to the Labyrinth Game")

    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 start(self):
        """Starts the game loop"""
        command_manager = CommandManager()
        finished = False
        message = ""
        args = []
        cmd = None
        try:
            while not finished:
                user_input = input("$> ")

                cmd, args, message = command_manager.parse_user_input(user_input)
                if cmd == None:
                    print(message)
                    continue
                (finished, message) = CommandManager.eval_command(cmd, args, self.__labyrinth, self.__player)
                self.__bear.move_random(self.__labyrinth)
                if message != "": print(message)
            print("End of Game !")
        except:
            print("Unexpected error:", sys.exc_info()[0])
    def test_generate_labyrinth_and_export(self):
        laby = Labyrinth(4)
        laby.generate_labyrinth()
        export = laby.export_labyrinth()

        print(export.count("*"))
        print(export.count("S"))
        print(export.count("T"))
        self.assertEqual(export.count("S"), 1)
        self.assertEqual(export.count("T"), 1)
        self.assertEqual(export.count("M"), 31)
        self.assertEqual(export.count("*"), 11)
예제 #7
0
    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")
예제 #8
0
    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")
예제 #9
0
    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)
예제 #10
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_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")
예제 #12
0
 def move_down(self, labyrinth: Labyrinth):
     """Make the player move odwn if there are no wall or monolith. If the player has the treasure and moves out
             it triggers the win action"""
     underneath__cell = labyrinth.get_labyrinth()[(self.__pos_y * 2 + 1) +
                                                  1][self.__pos_x * 2 +
                                                     1].get_cell_type()
     if underneath__cell == CellType.WALL or underneath__cell == CellType.MONOLITH:
         return "step impossible", underneath__cell.value, False
     elif underneath__cell == CellType.NO_WALL and self.__pos_y == labyrinth.get_size(
     ) - 1 and self.player_has_treasure():
         return "step executed", "out of labyrinth", True
     elif underneath__cell == CellType.NO_WALL and self.__pos_y == labyrinth.get_size(
     ) - 1 and not self.player_has_treasure():
         return "step impossible", "no treasure in inventory", False
     else:
         self.__pos_y += 1
         return "step executed", self.get_current_cell(
             labyrinth).get_cell_type().value, False
예제 #13
0
 def move_left(self, labyrinth: Labyrinth):
     """Make the player move left if there are no wall or monolith. If the player has the treasure and moves out
             it triggers the win action"""
     left_cell = labyrinth.get_labyrinth()[(self.__pos_y * 2 +
                                            1)][(self.__pos_x * 2 + 1) -
                                                1].get_cell_type()
     if left_cell == CellType.WALL or left_cell == CellType.MONOLITH:
         return "step impossible", left_cell.value, False
     elif left_cell == CellType.NO_WALL and self.__pos_x == 0 and self.player_has_treasure(
     ):
         return "step executed", "out of labyrinth", True
     elif left_cell == CellType.NO_WALL and self.__pos_x == 0 and not self.player_has_treasure(
     ):
         return "step impossible", "no treasure in inventory", False
     else:
         self.__pos_x -= 1
         return "step executed", self.get_current_cell(
             labyrinth).get_cell_type().value, False
    def test_initialize_special_cells(self):
        laby = Labyrinth(4)
        laby.generate_labyrinth()
        self.assertEqual(len(laby.initialize_special_cells()), 5)

        laby = Labyrinth(8)
        laby.generate_labyrinth()
        self.assertEqual(len(laby.initialize_special_cells()), 6)

        laby = Labyrinth(10)
        laby.generate_labyrinth()
        self.assertEqual(len(laby.initialize_special_cells()), 7)
예제 #15
0
 def get_current_cell(self, labyrinth: Labyrinth):
     """Return the cell on which the player is"""
     return labyrinth.get_labyrinth()[(self.__pos_y * 2 +
                                       1)][self.__pos_x * 2 + 1]