Advertisement
horozov86

Squirell

Jun 5th, 2023
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.51 KB | None | 0 0
  1. def make_a_move(row_, col_, matrix):
  2.     found_hazelnuts = 0
  3.     if not (0 <= row_ < len(matrix) and 0 <= col_ < len(matrix)):
  4.         print("The squirrel is out of the field.")
  5.         return
  6.     if matrix[row_][col_] == 't':
  7.         print("Unfortunately, the squirrel stepped on a trap...")
  8.         return
  9.     if matrix[row_][col_] == 'h':
  10.         found_hazelnuts += 1
  11.     matrix[row_][col_] = '*'
  12.     return found_hazelnuts, matrix
  13.  
  14.  
  15. rows = int(input())
  16. commands = input().split(", ")
  17. field = []
  18. hazelnuts = 0
  19. die = False
  20.  
  21. for _ in range(rows):
  22.     field.append(list(input()))
  23.  
  24. squirrel_position = []
  25. for row in range(rows):
  26.     for col in range(rows):
  27.         if field[row][col] == 's':
  28.             squirrel_position = [row, col]
  29.  
  30. field[squirrel_position[0]][squirrel_position[1]] = "*"
  31.  
  32. for command in commands:
  33.     if command == 'left':
  34.         squirrel_position[1] -= 1
  35.     elif command == 'right':
  36.         squirrel_position[1] += 1
  37.     elif command == 'down':
  38.         squirrel_position[0] += 1
  39.     elif command == 'up':
  40.         squirrel_position[0] -= 1
  41.     result = make_a_move(squirrel_position[0], squirrel_position[1], field)
  42.     if not result:
  43.         die = True
  44.         break
  45.     count_hazelnuts, field = result[0], result[1]
  46.     hazelnuts += count_hazelnuts
  47.     if hazelnuts == 3:
  48.         print("Good job! You have collected all hazelnuts!")
  49.         break
  50.  
  51. if not die and hazelnuts < 3:
  52.     print("There are more hazelnuts to collect.")
  53.  
  54. print(f"Hazelnuts collected: {hazelnuts}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement