skdnknfndskfns

py

School

University of Alberta *

*We aren’t endorsed by this school

Course

174

Subject

Computer Science

Date

Dec 6, 2023

Type

py

Pages

5

Uploaded by UltraStraw12266

Report
''' MAP_FILE = 'cave_map.txt' def load_map(map_file: str) -> list[list[str]]: """ Loads a map from a file as a grid (list of lists) """ with open(map_file) as f: grid = [list(line.strip()) for line in f] return grid def find_start(grid: list[list[str]]) -> list[int, int]: """ Finds the starting position of the player on the map. """ for i in range(len(grid)): for j in range(len(grid[i])): if grid[i][j] == "S": return [i, j] def get_command() -> str: """ Gets a command from the user. """ return input() def display_map(grid: list[list[str]], player_position: list[int, int]) -> None: """ Displays the map. """ for i in range(len(grid)): for j in range(len(grid[i])): if [i, j] == player_position: print('@', end='') else: print(grid[i][j], end='') print() def get_grid_size(grid: list[list[str]]) -> list[int, int]: """ Returns the size of the grid. """ return [len(grid), len(grid[0])] def is_inside_grid(grid: list[list[str]], position: list[int, int]) -> bool: """ Checks if a given position is valid (inside the grid). """ grid_rows, grid_cols = get_grid_size(grid) row, col = position return 0 <= row < grid_rows and 0 <= col < grid_cols def look_around(grid: list[list[str]], player_position: list[int, int]) -> list: """ Returns the allowed directions. """ allowed_objects = ('S', 'F', '*') row, col = player_position directions = []
if is_inside_grid(grid, [row - 1, col]) and grid[row - 1][col] in allowed_objects: directions.append('north') if is_inside_grid(grid, [row + 1, col]) and grid[row + 1][col] in allowed_objects: directions.append('south') if is_inside_grid(grid, [row, col - 1]) and grid[row][col - 1] in allowed_objects: directions.append('west') if is_inside_grid(grid, [row, col + 1]) and grid[row][col + 1] in allowed_objects: directions.append('east') return directions def move(direction: str, player_position: list[int, int], grid: list[list[str]]) -> bool: """ Moves the player in the given direction. """ row, col = player_position if direction == 'north': new_pos = [row-1, col] elif direction == 'south': new_pos = [row+1, col] elif direction == 'west': new_pos = [row, col-1] elif direction == 'east': new_pos = [row, col+1] else: return False if is_inside_grid(grid, new_pos) and grid[new_pos[0]][new_pos[1]] != '-': player_position[0], player_position[1] = new_pos[0], new_pos[1] return True else: return False def main(): """ Main entry point for the game. """ # Load data from a text file into a nested list grid = load_map(MAP_FILE) player_position = find_start(grid) while True: directions = look_around(grid, player_position) print('You can go', ', '.join(directions)) command = get_command() if command == 'escape': break elif command == 'show map': display_map(grid, player_position) elif command[0:3] == 'go ': dir = command[3:] if move(dir, player_position, grid): print(f'You moved {dir}.') else:
print('There is no way there.') else: print("I do not understand.") if __name__ == '__main__': main() ''' MAP_FILE = 'cave_map.txt' HELP_FILE = 'help.txt' def load_map(map_file: str) -> list[list[str]]: """ Loads a map from a file as a grid (list of lists) """ with open(map_file) as f: grid = [list(line.strip()) for line in f] return grid def find_start(grid: list[list[str]]) -> list[int, int]: """ Finds the starting position of the player on the map. """ for i in range(len(grid)): for j in range(len(grid[i])): if grid[i][j] == "S": return [i, j] def get_command() -> str: """ Gets a command from the user. """ return input('>') def display_map(grid: list[list[str]], player_position: list[int, int]) -> None: """ Displays the map. """ emoji_map = {'S': ', 'F': ', '-': ', '*': ', '@': '} ? ''''' '''' '''' '''' '''' '''' '''' '''' '''' '''' '''' '''' ' ' ' ' for i in range(len(grid)): for j in range(len(grid[i])): if [i, j] == player_position: print( ', end='') ' ' else: print(emoji_map[grid[i][j]], end='') print() def get_grid_size(grid: list[list[str]]) -> list[int, int]: """ Returns the size of the grid. """ return [len(grid), len(grid[0])] def is_inside_grid(grid: list[list[str]], position: list[int, int]) -> bool: """ Checks if a given position is valid (inside the grid). """ grid_rows, grid_cols = get_grid_size(grid) row, col = position
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
return 0 <= row < grid_rows and 0 <= col < grid_cols def look_around(grid: list[list[str]], player_position: list[int, int]) -> list: """ Returns the allowed directions. """ allowed_objects = ('S', 'F', '*') row, col = player_position directions = [] if is_inside_grid(grid, [row - 1, col]) and grid[row - 1][col] in allowed_objects: directions.append('north') if is_inside_grid(grid, [row + 1, col]) and grid[row + 1][col] in allowed_objects: directions.append('south') if is_inside_grid(grid, [row, col - 1]) and grid[row][col - 1] in allowed_objects: directions.append('west') if is_inside_grid(grid, [row, col + 1]) and grid[row][col + 1] in allowed_objects: directions.append('east') return directions def move(direction: str, player_position: list[int, int], grid: list[list[str]]) -> bool: """ Moves the player in the given direction. """ row, col = player_position if direction == 'north': new_pos = [row-1, col] elif direction == 'south': new_pos = [row+1, col] elif direction == 'west': new_pos = [row, col-1] elif direction == 'east': new_pos = [row, col+1] else: return False if is_inside_grid(grid, new_pos) and grid[new_pos[0]][new_pos[1]] != '-': player_position[0], player_position[1] = new_pos[0], new_pos[1] return True else: return False def check_finish(grid, player_position): return grid[player_position[0]][player_position[1]] == 'F' def display_help(): with open(HELP_FILE, 'r') as f: print(f.read()) def make_step(grid: list[list[str]], player_position: list[int, int]) -> bool: """ Makes a single step of the game. """ directions = look_around(grid, player_position) print('You can go', ', '.join(directions))
command = get_command() if command == 'escape': return False elif command == 'show map': display_map(grid, player_position) elif command[0:3] == 'go ': dir = command[3:] if move(dir, player_position, grid): print(f'You moved {dir}.') if check_finish(grid, player_position): print('Congratulations! You have reached the exit!') return False else: print('There is no way there.') elif command == 'help': display_help() else: print("I do not understand.") return True def main(): """ Main entry point for the game. """ # Load data from a text file into a nested list grid = load_map(MAP_FILE) player_position = find_start(grid) while make_step(grid, player_position): pass if __name__ == '__main__': main()