Advertisement
mmishanchyk

Retake Exam - 14.02.2021 (problem2)

Jun 16th, 2021
988
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.55 KB | None | 0 0
  1. n = int(input())
  2.  
  3. coins = 0
  4. matrix = []
  5. step_history = []
  6.  
  7. for row in range(n):
  8.     matrix.append([el for el in input().split()])
  9.  
  10. for row in range(n):
  11.     for col in range(n):
  12.         if matrix[row][col] == "P":
  13.             start_row = row
  14.             start_col = col
  15.  
  16. commands = input()
  17.  
  18. while commands:
  19.  
  20.     if commands == "up":
  21.         row = start_row - 1
  22.         col = start_col
  23.         next_point = matrix[row][col]
  24.  
  25.         if next_point != "X" or row not in range(n) or col not in range(n):
  26.             coins += int(next_point)
  27.             step_history.append([row, col])
  28.             if coins >= 100:
  29.                 print(f"You won! You've collected {coins} coins.")
  30.                 break
  31.             break
  32.         else:
  33.             print(f"Game over! You've collected {(coins // 2)} coins.")
  34.             break
  35.  
  36.     elif commands == "down":
  37.         row = start_row + 1
  38.         col = start_col
  39.         next_point = matrix[row][col]
  40.  
  41.         if next_point != "X" or row not in range(n) or col not in range(n):
  42.             coins += int(next_point)
  43.             step_history.append([row, col])
  44.             if coins >= 100:
  45.                 print(f"You won! You've collected {coins} coins.")
  46.                 break
  47.             break
  48.         else:
  49.             print(f"Game over! You've collected {coins // 2} coins.")
  50.             break
  51.     elif commands == "left":
  52.         row = start_row
  53.         col = start_col - 1
  54.         next_point = matrix[row][col]
  55.  
  56.         if next_point != "X" or row not in range(n) or col not in range(n):
  57.             coins += int(next_point)
  58.             step_history.append([row, col])
  59.             if coins >= 100:
  60.                 print(f"You won! You've collected {coins} coins.")
  61.                 break
  62.             break
  63.         else:
  64.             print(f"Game over! You've collected {coins // 2} coins.")
  65.             break
  66.     elif commands == "right":
  67.         row = start_row
  68.         col = start_col + 1
  69.         next_point = matrix[row][col]
  70.  
  71.         if next_point != "X" or row not in range(n) or col not in range(n):
  72.             coins += int(next_point)
  73.             step_history.append([row, col])
  74.             if coins >= 100:
  75.                 print(f"You won! You've collected {coins} coins.")
  76.                 break
  77.             break
  78.         else:
  79.             print(f"Game over! You've collected {coins // 2} coins.")
  80.             break
  81.     else:
  82.         continue
  83.  
  84.     start_row = row
  85.     start_col = col
  86.  
  87.     commands = input()
  88.  
  89.  
  90. print("Your path:")
  91. for el in step_history:
  92.     print(el)
  93.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement