viligen

collecting_coins

Jan 31st, 2022
961
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.32 KB | None | 0 0
  1. size = int(input())
  2.  
  3. matrix = []
  4. player_row, player_col = None, None
  5. total_coins = 0
  6. path = []
  7. for row in range(size):
  8.     matrix.append(input().split())
  9.     for col in range(size):
  10.         if matrix[row][col] == 'P':
  11.             player_row, player_col = row, col
  12.             path.append([player_row, player_col])
  13.  
  14. while total_coins < 100:
  15.     command = input()
  16.     if command == 'up':
  17.         player_row -= 1
  18.     elif command == 'down':
  19.         player_row += 1
  20.     elif command == 'right':
  21.         player_col += 1
  22.     elif command == 'left':
  23.         player_col -= 1
  24.  
  25.     if player_col < 0:
  26.         player_col = size - 1
  27.     elif player_col >= size:
  28.         player_col = 0
  29.     if player_row < 0:
  30.         player_row = size - 1
  31.     elif player_row >= size:
  32.         player_row = 0
  33.  
  34.     path.append([player_row, player_col])
  35.     if matrix[player_row][player_col] == 'X':
  36.         total_coins //= 2
  37.         break
  38.     elif matrix[player_row][player_col].isdigit():
  39.         total_coins += int(matrix[player_row][player_col])
  40.         matrix[player_row][player_col] = '0'
  41.  
  42. if total_coins >= 100:
  43.     print(f"""You won! You've collected {total_coins} coins.
  44. Your path:""")
  45.     print(*path, sep='\n')
  46. else:
  47.     print(f"""Game over! You've collected {total_coins} coins.
  48. Your path:""")
  49.     print(*path, sep='\n')
  50.  
Advertisement
Add Comment
Please, Sign In to add comment