def __write_move_fail_to_log(self, dir):
        """write fail while moving in specified directoin to log

        Arguments:
            dir str -- move direction
        """

        log_debug(F"failed to move {dir}")
    def load(self):
        """load the game if save file exists

        Returns:
            bool -- true if game has been succesfully loaded,
                false otherwise
        """

        has_loaded = False

        try:
            with open("sav.txt", "rb") as savefile:

                temp_dict = pickle.load(savefile)
                self.__dict__.update(temp_dict)
                log_debug("game loaded")
                has_loaded = True

        except OSError:
            log_info("No save file found :(")

        return has_loaded
    def __init__(self, player_health, win_condition, map_scale=1):
        """initialize game session

        Arguments:
            player_health {int} -- player health
            win_condition {int} -- required number of treasures
                to collect to win the game
            map_scale {int} -- map scale
        """

        self.__win_condition = win_condition
        self.__terrain = Terrain(map_scale)

        start_row, start_col = self.__terrain.start_position
        start_position_message = F"start position: ({start_row}, {start_col})"
        log_debug(start_position_message)
        log_debug("map:")
        log_debug(self.__terrain)

        self.__player = Player(start_row, start_col, player_health)
                scale = int(input("Enter map scale: "))

                if (scale < 1):
                    raise exceptions.TerrainGenerationError(scale)

            except exceptions.TerrainGenerationError as instance:
                log_info(instance)
                continue

            except ValueError:
                log_info(F"Entered scale is not an integer: {scale}")
                continue

            game_session = game.GameSession(3, 3, int(scale))
            log_debug("new game created")
            break

        elif command == "load":

            game_session = game.GameSession(0, 0, 1)
            if game_session.load():
                break

        elif command == "debug":

            if debug_mode:
                log_info("Already in debug mode")

            else:
    def run(self):
        """run the game

            function is proceeded until player finds a treasure or enters a trap

        Arguments:
            start_row int -- start position row
            start_col int -- start position column
            squares_map [str] -- game map
        """

        log_info("Type \"up/down/left/right\" to move")
        log_info("Type \"save\" to save current game")

        border = self.__terrain.scale - 1

        while True:

            curr_row, curr_col = self.__player.position

            if self.__terrain.get_square(curr_row, curr_col) == 't':

                log_debug("player has entered a trap")
                log_info("You have entered a trap!")
                self.__player.damage(1)

            if self.__terrain.get_square(curr_row, curr_col) == '*':

                log_debug("player has found a treasure")
                log_info("You have found a treasure!")
                self.__player.grant_treasure(1)

            if self.__player.health <= 0:
                log_debug("player has died")
                log_debug("game ends")
                log_info("You have died!")
                break

            if self.__player.bag >= self.__win_condition:
                log_debug("player has found enough")
                log_debug("game ends")
                log_info("You have found enogh treasure!")
                break

            if self.__structure_is_near('t'):

                log_debug("trap is within one square")
                log_info('There is a trap within one square!')

            if self.__structure_is_near('*'):

                log_debug("treasure is within one square")
                log_info('There is a treasure within one square!')

            moved = False

            while not moved:

                command = input("What to do?: ")

                if command == 'up':

                    if curr_row == 0:

                        log_info("You are already on top of the map!")
                        self.__write_move_fail_to_log(command)

                    else:

                        moved = True
                        curr_row -= 1
                        log_info("Moving up")

                elif command == 'down':

                    if curr_row == border:

                        log_info("You are already on bottom of the map!")
                        self.__write_move_fail_to_log(command)

                    else:

                        moved = True
                        curr_row += 1
                        log_info("Moving down")

                elif command == 'right':

                    if curr_col == border:

                        msg = "You are already on the right border of the map!"
                        log_info(msg)
                        self.__write_move_fail_to_log(command)

                    else:

                        moved = True
                        curr_col += 1
                        log_info("Moving right")

                elif command == 'left':

                    if curr_col == 0:

                        msg = "You are already on the left border of the map!"
                        log_info(msg)
                        self.__write_move_fail_to_log(command)

                    else:

                        moved = True
                        curr_col -= 1
                        log_info("Moving left")

                elif command == "save":

                    with open("sav.txt", "wb") as savefile:

                        pickle.dump(self.__dict__, savefile, protocol=0)
                        log_info("Saved succesfully")

                else:
                    log_info(F"Unknown command: {command}")

                if moved:

                    self.__player.set_position(curr_row, curr_col)
                    log_debug(F"moved {command}")
                    cur_pos_msg = F"current position: ({curr_row}, {curr_col})"
                    log_debug(cur_pos_msg)

        log_info(self.__terrain)
        log_info("* - stands for a treasure")
        log_info("t - stands for a trap")
        log_info(". - stands for an empty square")
        log_info("s - stands for the start position\n")
Beispiel #6
0
    def __player_move(self):
        """player moving loop
        """

        border = self.__terrain.scale - 1

        while True:

            curr_row, curr_col = self.__player.position

            if self.__terrain.get_square(curr_row, curr_col) == 't':

                log_debug("player has entered a trap")
                log_info("You have entered a trap!")
                self.__player.damage(1)

            if self.__terrain.get_square(curr_row, curr_col) == '*':

                log_debug("player has found a treasure")
                log_info("You have found a treasure!")
                self.__player.grant_treasure(1)

            if self.__player.health <= 0:
                log_debug("player has died")
                log_debug("game ends")
                log_info("You have died!")
                self.__end_of_game.set()

            if self.__player.bag >= self.__win_condition:
                log_debug("player has found enough")
                log_debug("game ends")
                log_info("You have found enogh treasure!")
                self.__end_of_game.set()

            if self.__structure_is_near('t'):

                log_debug("trap is within one square")
                log_info('There is a trap within one square!')

            if self.__structure_is_near('*'):

                log_debug("treasure is within one square")
                log_info('There is a treasure within one square!')

            moved = False

            while not moved:

                command = input("What to do?: ")

                if command == 'up':

                    if curr_row == 0:

                        log_info("You are already on top of the map!")
                        self.__write_move_fail_to_log(command)

                    else:

                        moved = True
                        curr_row -= 1
                        log_info("Moving up")

                elif command == 'down':

                    if curr_row == border:

                        log_info("You are already on bottom of the map!")
                        self.__write_move_fail_to_log(command)

                    else:

                        moved = True
                        curr_row += 1
                        log_info("Moving down")

                elif command == 'right':

                    if curr_col == border:

                        msg = "You are already on the right border of the map!"
                        log_info(msg)
                        self.__write_move_fail_to_log(command)

                    else:

                        moved = True
                        curr_col += 1
                        log_info("Moving right")

                elif command == 'left':

                    if curr_col == 0:

                        msg = "You are already on the left border of the map!"
                        log_info(msg)
                        self.__write_move_fail_to_log(command)

                    else:

                        moved = True
                        curr_col -= 1
                        log_info("Moving left")

                elif command == "save":

                    with open("sav.txt", "wb") as savefile:

                        pickle.dump(self.__dict__, savefile, protocol=0)
                        log_info("Saved succesfully")

                else:
                    log_info(F"Unknown command: {command}")

                if moved:

                    self.__lock.acquire()
                    self.__player.set_position(curr_row, curr_col)
                    self.__lock.release()

                    log_debug(F"moved {command}")
                    cur_pos_msg = F"current position: ({curr_row}, {curr_col})"
                    log_debug(cur_pos_msg)
def run(start_row, start_col, squares_map):
    """run the game

        function is proceeded until player finds a treasure or enters a trap

    Arguments:
        start_row int -- start position row
        start_col int -- start position column
        squares_map [str] -- game map
    """

    log_info("Type \"up/down/left/right\" to move")
    log_info("Type \"save\" to save current game")

    border = len(squares_map) - 1

    curr_row = start_row
    curr_col = start_col

    while True:

        if squares_map[curr_row][curr_col] == 't':

            log_debug("player has found a treasure")
            log_debug("game ends")
            log_info("You have entered a trap!")
            break

        if squares_map[curr_row][curr_col] == '*':

            log_debug("player has entered a trap")
            log_debug("game ends")
            log_info("You have found a treasure!")
            break

        if _structure_is_near('t', curr_row, curr_col, squares_map):

            log_debug("trap is within one square")
            log_info('There is a trap within one square!')

        if _structure_is_near('*', curr_row, curr_col, squares_map):

            log_debug("treasure is within one square")
            log_info('There is a treasure within one square!')

        moved = False

        while not moved:

            command = input("What to do?: ")

            if command == 'up':

                if curr_row == 0:

                    log_info("You are already on top of the map!")
                    _write_move_fail_to_log(command)

                else:

                    moved = True
                    curr_row -= 1
                    log_info("Moving up")

            elif command == 'down':

                if curr_row == border:

                    log_info("You are already on bottom of the map!")
                    _write_move_fail_to_log(command)

                else:

                    moved = True
                    curr_row += 1
                    log_info("Moving down")

            elif command == 'right':

                if curr_col == border:

                    msg = "You are already on the right border of the map!"
                    log_info(msg)
                    _write_move_fail_to_log(command)

                else:

                    moved = True
                    curr_col += 1
                    log_info("Moving right")

            elif command == 'left':

                if curr_col == 0:

                    msg = "You are already on the left border of the map!"
                    log_info(msg)
                    _write_move_fail_to_log(command)

                else:

                    moved = True
                    curr_col -= 1
                    log_info("Moving left")

            elif command == "save":

                with open("sav.txt", "wb") as savefile:

                    data_to_save = [curr_row, curr_col, squares_map]
                    pickle.dump(data_to_save, savefile, protocol=0)
                    log_info("Saved succesfully")

            else:
                log_info(F"Unknown command: {command}")

            if moved:

                log_debug(F"moved {command}")
                cur_pos_msg = F"current position: ({curr_row}, {curr_col})"
                log_debug(cur_pos_msg)
log_info("Welcome to the game!")

while True:

    log_info("Type \"new\" to start new game")
    log_info("Type \"load\" to load saved game")
    log_info("Type \"debug\" to enter debug mode")

    command = input()

    if command == "new":

        scale = input("Enter map scale: ")
        start_row, start_col, squares_map = terrain.generate(int(scale))
        log_debug("new game created")
        break

    elif command == "load":

        if os.path.exists("sav.txt"):

            with open("sav.txt", "rb") as savefile:

                start_row, start_col, squares_map = pickle.load(savefile)
                log_debug("game loaded")
                break

        else:

            log_info("No save file found :(")