Advertisement
go6odn28

2_escape_the_maze

Jun 19th, 2024
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.24 KB | None | 0 0
  1. def is_valid_index(idx, size):
  2.     return 0 <= idx < size
  3.  
  4.  
  5. def next_move(rows, row, col, direction):
  6.     moves = {
  7.         "up": (-1, 0),
  8.         "down": (1, 0),
  9.         "left": (0, -1),
  10.         "right": (0, 1)
  11.     }
  12.  
  13.     d_row, d_col = moves[direction]
  14.     row = (row + d_row)
  15.     col = (col + d_col)
  16.     if is_valid_index(row, rows) and is_valid_index(col, rows):
  17.         return row, col
  18.     return None, None
  19.  
  20.  
  21. def fill_the_matrix_and_take_positions(rows, t_row, t_col):
  22.     matrix = []
  23.     for idx in range(rows):
  24.         row = list(input())
  25.         matrix.append(row)
  26.         if "P" in row:
  27.             t_row = idx
  28.             t_col = row.index("P")
  29.     return matrix, t_row, t_col
  30.  
  31.  
  32. def print_result(matrix, traveler_dead, left_maze, t_health):
  33.     print("Player is dead. Maze over!") \
  34.         if traveler_dead else print("Player escaped the maze. Danger passed!")
  35.  
  36.     print(f"Player's health: {t_health} units")
  37.     [print("".join(element)) for element in matrix]
  38.  
  39.  
  40. def main():
  41.     rows = int(input())
  42.     MAX_HEALTH = 100
  43.     traveler_health = MAX_HEALTH
  44.     traveler_is_dead = False
  45.     traveler_left_maze = False
  46.     traveler_row, traveler_col = 0, 0
  47.     maze, traveler_row, traveler_col, = (
  48.         fill_the_matrix_and_take_positions(rows, traveler_row, traveler_col))
  49.  
  50.     while True:
  51.         if traveler_is_dead or traveler_left_maze:
  52.             break
  53.  
  54.         command = input()
  55.         next_row, next_col = next_move(rows, traveler_row, traveler_col, command)
  56.         if next_row is None or next_col is None:
  57.             continue
  58.  
  59.         elif maze[next_row][next_col] == "X":
  60.             traveler_left_maze = True
  61.  
  62.         elif maze[next_row][next_col] == "M":
  63.             traveler_health -= 40
  64.             if traveler_health <= 0:
  65.                 traveler_health = 0
  66.                 traveler_is_dead = True
  67.  
  68.         elif maze[next_row][next_col] == "H":
  69.             traveler_health = min(traveler_health + 15, MAX_HEALTH)
  70.  
  71.         maze[traveler_row][traveler_col] = "-"
  72.         traveler_row, traveler_col = next_row, next_col
  73.         maze[traveler_row][traveler_col] = "P"
  74.  
  75.     print_result(maze, traveler_is_dead, traveler_left_maze, traveler_health)
  76.  
  77.  
  78. if __name__ == '__main__':
  79.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement