Advertisement
Nenogzar
Jun 19th, 2024
23
0
Never
This is comment for paste 02. Escape the Maze
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. def print_maze(matrix):
  2.     for row in matrix:
  3.         print("".join(row))
  4.  
  5. def directions(position, direction, rows):
  6.     row, col = position
  7.     moves = {
  8.         'up': (-1, 0),
  9.         'down': (1, 0),
  10.         'left': (0, -1),
  11.         'right': (0, 1)
  12.     }
  13.  
  14.     d_row, d_col = moves.get(direction, (0, 0))
  15.     new_row, new_col = row + d_row, col + d_col
  16.  
  17.     if 0 <= new_row < rows and 0 <= new_col < rows:
  18.         return new_row, new_col
  19.     return None
  20.  
  21. def update_position(maze, position, new_row, new_col, empty, traveller):
  22.     maze[position[0]][position[1]] = empty
  23.     maze[new_row][new_col] = traveller
  24.     position = new_row, new_col
  25.     return maze
  26.  
  27.  
  28. traveller, exit_symbol, monster, health_symbol, empty = "P", 'X', 'M', 'H', '-'
  29. maze = []
  30. health_points = 100
  31. position = (-1, -1)
  32. exit_position = None
  33.  
  34. rows = int(input())
  35. for row_ in range(rows):
  36.     line = list(input().strip())
  37.     if traveller in line:
  38.         position = (row_, line.index(traveller))
  39.     if exit_symbol in line:
  40.         exit_position = (row_, line.index(exit_symbol))
  41.     maze.append(line)
  42.  
  43. while health_points > 0:
  44.     direction = input()
  45.     if not direction:
  46.         break
  47.  
  48.     new_position = directions(position, direction, rows)
  49.     if new_position is None:
  50.         continue
  51.  
  52.     new_row, new_col = new_position
  53.  
  54.     if maze[new_row][new_col] == exit_symbol:
  55.         print("Player escaped the maze. Danger passed!")
  56.         maze = update_position(maze, position, new_row, new_col, empty, traveller)
  57.         break
  58.  
  59.     elif maze[new_row][new_col] == monster:
  60.         health_points -= 40
  61.         maze = update_position(maze, position, new_row, new_col, empty, traveller)
  62.  
  63.         if health_points <= 0:
  64.             print("Player is dead. Maze over!")
  65.             health_points = 0
  66.  
  67.  
  68.     elif maze[new_row][new_col] == health_symbol:
  69.         health_points += 15
  70.         maze = update_position(maze, position, new_row, new_col, empty, traveller)
  71.         if health_points > 100:
  72.             health_points = 100
  73.  
  74.  
  75.     else:
  76.         maze = update_position(maze, position, new_row, new_col, empty, traveller)
  77.     position = new_position
  78.  
  79. # maze[position[0]][position[1]] = traveller
  80. print(f"Player's health: {health_points} units")
  81. print_maze(maze)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement