Advertisement
This is comment for paste
02. Escape the Maze
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def print_maze(matrix):
- for row in matrix:
- print("".join(row))
- def directions(position, direction, rows):
- row, col = position
- moves = {
- 'up': (-1, 0),
- 'down': (1, 0),
- 'left': (0, -1),
- 'right': (0, 1)
- }
- d_row, d_col = moves.get(direction, (0, 0))
- new_row, new_col = row + d_row, col + d_col
- if 0 <= new_row < rows and 0 <= new_col < rows:
- return new_row, new_col
- return None
- def update_position(maze, position, new_row, new_col, empty, traveller):
- maze[position[0]][position[1]] = empty
- maze[new_row][new_col] = traveller
- position = new_row, new_col
- return maze
- traveller, exit_symbol, monster, health_symbol, empty = "P", 'X', 'M', 'H', '-'
- maze = []
- health_points = 100
- position = (-1, -1)
- exit_position = None
- rows = int(input())
- for row_ in range(rows):
- line = list(input().strip())
- if traveller in line:
- position = (row_, line.index(traveller))
- if exit_symbol in line:
- exit_position = (row_, line.index(exit_symbol))
- maze.append(line)
- while health_points > 0:
- direction = input()
- if not direction:
- break
- new_position = directions(position, direction, rows)
- if new_position is None:
- continue
- new_row, new_col = new_position
- if maze[new_row][new_col] == exit_symbol:
- print("Player escaped the maze. Danger passed!")
- maze = update_position(maze, position, new_row, new_col, empty, traveller)
- break
- elif maze[new_row][new_col] == monster:
- health_points -= 40
- maze = update_position(maze, position, new_row, new_col, empty, traveller)
- if health_points <= 0:
- print("Player is dead. Maze over!")
- health_points = 0
- elif maze[new_row][new_col] == health_symbol:
- health_points += 15
- maze = update_position(maze, position, new_row, new_col, empty, traveller)
- if health_points > 100:
- health_points = 100
- else:
- maze = update_position(maze, position, new_row, new_col, empty, traveller)
- position = new_position
- # maze[position[0]][position[1]] = traveller
- print(f"Player's health: {health_points} units")
- print_maze(maze)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement