Advertisement
Nenogzar

02. Escape the Maze

Jun 19th, 2024
454
0
Never
3
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.00 KB | None | 0 0
  1. # https://judge.softuni.org/Contests/Practice/Index/4528#1
  2.  
  3. def print_maze(matrix):
  4.     for row in matrix:
  5.         print("".join(row))
  6.  
  7. def directions(position, direction, rows):
  8.     row, col = position
  9.     moves = {
  10.         'up': (-1, 0),
  11.         'down': (1, 0),
  12.         'left': (0, -1),
  13.         'right': (0, 1)
  14.     }
  15.  
  16.     d_row, d_col = moves.get(direction, (0, 0))
  17.     new_row, new_col = row + d_row, col + d_col
  18.  
  19.     if 0 <= new_row < rows and 0 <= new_col < rows:
  20.         return new_row, new_col
  21.     return None
  22.  
  23. traveller, exit_symbol, monster, health_symbol, empty = "P", 'X', 'M', 'H', '-'
  24. maze = []
  25. health_points = 100
  26. position = (-1, -1)
  27. exit_position = None
  28.  
  29. rows = int(input())
  30. for row_ in range(rows):
  31.     line = list(input().strip())
  32.     if traveller in line:
  33.         position = (row_, line.index(traveller))
  34.     if exit_symbol in line:
  35.         exit_position = (row_, line.index(exit_symbol))
  36.     maze.append(line)
  37.  
  38. while health_points > 0:
  39.     direction = input()
  40.     if not direction:
  41.         break
  42.  
  43.     new_position = directions(position, direction, rows)
  44.     if new_position is None:
  45.         continue
  46.  
  47.     new_row, new_col = new_position
  48.  
  49.     if maze[new_row][new_col] == exit_symbol:
  50.         print("Player escaped the maze. Danger passed!")
  51.         maze[position[0]][position[1]] = empty
  52.         maze[new_row][new_col] = traveller
  53.         position = new_row, new_col
  54.         break
  55.  
  56.     elif maze[new_row][new_col] == monster:
  57.         health_points -= 40
  58.         if health_points > 0:
  59.             maze[position[0]][position[1]] = empty
  60.             maze[new_row][new_col] = traveller
  61.         else:
  62.             maze[position[0]][position[1]] = empty
  63.             maze[new_row][new_col] = traveller
  64.             print("Player is dead. Maze over!")
  65.             health_points = 0
  66.         position = new_row, new_col
  67.  
  68.     elif maze[new_row][new_col] == health_symbol:
  69.         health_points += 15
  70.         if health_points > 100:
  71.             health_points = 100
  72.         maze[position[0]][position[1]] = empty
  73.         maze[new_row][new_col] = traveller
  74.         position = new_row, new_col
  75.  
  76.     else:
  77.         maze[position[0]][position[1]] = empty
  78.         maze[new_row][new_col] = traveller
  79.         position = new_position
  80.  
  81. # maze[position[0]][position[1]] = traveller
  82. print(f"Player's health: {health_points} units")
  83. print_maze(maze)
  84.  
  85.  
  86. """
  87. maze[position[0]][position[1]] = empty
  88. maze[new_row][new_col] = traveller
  89. """
  90.  
  91. """
  92. Пробвах се да го направя по този начин:
  93.  
  94. def update_position(maze, current_position, new_position, traveller_symbol, empty_symbol):
  95.    current_row, current_col = current_position
  96.    new_row, new_col = new_position
  97.    
  98.    maze[current_row][current_col] = empty_symbol
  99.    
  100.    maze[new_row][new_col] = traveller_symbol
  101.    
  102.    return maze
  103.    
  104.    
  105. и във кода да го метна като:
  106.  
  107. maze = update_position(maze, current_position, new_position, traveller_symbol, empty_symbol)
  108.  
  109. """
  110.  
Advertisement
Comments
  • go6odn28
    93 days
    Comment was deleted
    • Nenogzar
      93 days
      # Python 2.25 KB | 0 0
      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)
  • go6odn28
    93 days
    Comment was deleted
Add Comment
Please, Sign In to add comment
Advertisement