Advertisement
pacho_the_python

Collecting Coins

May 31st, 2022
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.75 KB | None | 0 0
  1. from math import floor
  2.  
  3.  
  4. def next_position(x, y, word):
  5.     if word == "up":
  6.         return x - 1, y
  7.     if word == "down":
  8.         return x + 1, y
  9.     if word == "left":
  10.         return x, y - 1
  11.     if word == "right":
  12.         return x, y + 1
  13.  
  14.  
  15. def is_inside(x, y, num):
  16.     return 0 <= x < num and 0 <= y < num
  17.  
  18.  
  19. def opposite_side(x, y, direction, number):
  20.     if direction == "up":
  21.         return number - 1, y
  22.     if direction == "down":
  23.         return 0, y
  24.     if direction == "left":
  25.         return x, number - 1
  26.     if direction == "right":
  27.         return x, 0
  28.  
  29.  
  30. size = int(input())
  31.  
  32. matrix = []
  33.  
  34. for _ in range(size):
  35.  
  36.     matrix.append([x for x in input().split()])
  37.  
  38. player_row = 0
  39. player_col = 0
  40.  
  41. for row in range(size):
  42.     condition = False
  43.     for col in range(size):
  44.         if matrix[row][col] == "P":
  45.             player_row = row
  46.             player_col = col
  47.             condition = True
  48.             break
  49.     if condition:
  50.         break
  51.  
  52. player_path = [[player_row, player_col]]
  53. coins = 0
  54. win_condition = False
  55. command_list = ["up", "left", "down", "right"]
  56. while True:
  57.     command = input()
  58.     if command not in command_list:
  59.         continue
  60.     next_row, next_col = next_position(player_row, player_col, command)
  61.     if is_inside(next_row, next_col, size):
  62.         if matrix[next_row][next_col] != "P" and matrix[next_row][next_col] != "X":
  63.             coins += int(matrix[next_row][next_col])
  64.             matrix[next_row][next_col] = "P"
  65.             player_path.append([next_row, next_col])
  66.         elif matrix[next_row][next_col] == "P":
  67.             player_path.append([next_row, next_col])
  68.         elif matrix[next_row][next_col] == "X":
  69.             player_path.append([next_row, next_col])
  70.             coins = floor(coins - coins * 0.5)
  71.             break
  72.         player_row, player_col = next_row, next_col
  73.     else:
  74.         exit_row, exit_col = opposite_side(next_row, next_col, command, size)
  75.         if matrix[exit_row][exit_col] != "P" and matrix[exit_row][exit_col] != "X":
  76.             coins += int(matrix[exit_row][exit_col])
  77.             matrix[exit_row][exit_col] = "P"
  78.             player_path.append([exit_row, exit_col])
  79.         elif matrix[exit_row][exit_col] == "P":
  80.             player_path.append([exit_row, exit_col])
  81.         elif matrix[exit_row][exit_col] == "X":
  82.             player_path.append([exit_row, exit_col])
  83.             coins = coins - coins * 0.5
  84.             break
  85.         player_row, player_col = exit_row, exit_col
  86.     if coins >= 100:
  87.         win_condition = True
  88.         break
  89.  
  90. if win_condition:
  91.     print(f"You won! You've collected {coins} coins.")
  92. else:
  93.     print(f"Game over! You've collected {coins} coins.")
  94. print("Your path:")
  95. for k in player_path:
  96.     print(k)
  97.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement