Advertisement
mbstanchev

collecting_coins

Feb 16th, 2023
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. # def index_check(row, cow, matrix):
  2. # if row < 0:
  3. # row = len(matrix) - 1
  4. # if cow < 0:
  5. # cow = len(matrix) - 1
  6. #
  7. # if row >= len(matrix):
  8. # row = 0
  9. # if cow >= len(matrix):
  10. # cow = 0
  11. # return row, cow
  12.  
  13.  
  14. n = int(input())
  15. matrix = []
  16. player_pos = ()
  17. coins_collected = 0
  18. path = []
  19. # wall = False
  20. for r in range(n):
  21. row = input().split()
  22. matrix.append(row)
  23. for c in range(n):
  24. if matrix[r][c] == "P":
  25. player_pos = (r, c)
  26. path.append([r, c])
  27. matrix[r][c] = 0
  28.  
  29. directions = {
  30. "up": (-1, 0),
  31. "down": (1, 0),
  32. "left": (0, -1),
  33. "right": (0, 1),
  34. }
  35. while coins_collected < 100:
  36. command = input()
  37. next_step_row = player_pos[0] + directions[command][0]
  38. next_step_col = player_pos[1] + directions[command][1]
  39.  
  40. if next_step_row < 0:
  41. next_step_row = len(matrix) + next_step_row
  42. if next_step_col < 0:
  43. next_step_col = len(matrix) + next_step_col
  44.  
  45. if next_step_row >= len(matrix):
  46. next_step_row = 0
  47. if next_step_col >= len(matrix):
  48. next_step_col = 0
  49.  
  50. # next_step_row, next_step_col = index_check(next_step_row, next_step_col, matrix)
  51. player_pos = next_step_row, next_step_col
  52. path.append([next_step_row, next_step_col])
  53. if matrix[next_step_row][next_step_col] == "X":
  54.  
  55. coins_collected = coins_collected // 2
  56. break
  57. else:
  58. coin = matrix[next_step_row][next_step_col]
  59. coins_collected += int(coin)
  60. matrix[next_step_row][next_step_col] = 0
  61.  
  62. if coins_collected > 100:
  63. print(f"You won! You've collected {coins_collected} coins.")
  64.  
  65. elif coins_collected < 100:
  66. print(f"Game over! You've collected {coins_collected} coins.")
  67.  
  68. print('Your path:')
  69. print(*path, sep='\n')
  70.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement