viligen

miner

Jan 23rd, 2022
1,057
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.50 KB | None | 0 0
  1. from collections import deque
  2.  
  3.  
  4. def next_move(minerrow, minercol, command_):
  5.     if command_ == "up":
  6.         if minerrow - 1 >= 0:
  7.             minerrow -= 1
  8.     elif command_ == "down":
  9.         if minerrow + 1 < n:
  10.             minerrow += 1
  11.     elif command_ == "right":
  12.         if minercol + 1 < n:
  13.             minercol += 1
  14.     elif command_ == "left":
  15.         if minercol - 1 >= 0:
  16.             minercol -= 1
  17.     return minerrow, minercol
  18.  
  19.  
  20. n = int(input())
  21. commands = deque(input().split())
  22.  
  23. total_coals = 0
  24. miner_row, miner_col = None, None
  25. matrix = []
  26.  
  27. for row in range(n):
  28.     current_row = input().split()
  29.     matrix.append(current_row)
  30.     for element in current_row:
  31.         if element == "c":
  32.             total_coals += 1
  33.         elif element == 's':
  34.             miner_row = row
  35.             miner_col = current_row.index(element)
  36. game_over = False
  37.  
  38. while total_coals > 0 and commands:
  39.  
  40.     command = commands.popleft()
  41.     miner_row, miner_col = next_move(miner_row, miner_col, command)
  42.     if matrix[miner_row][miner_col] == 'e':
  43.         game_over = True
  44.         break
  45.     elif matrix[miner_row][miner_col] == "c":
  46.         total_coals -= 1
  47.         matrix[miner_row][miner_col] = '*'
  48.         if total_coals == 0:
  49.             break
  50.  
  51. if game_over:
  52.     print(f"Game over! ({miner_row}, {miner_col})")
  53.  
  54. elif total_coals == 0:
  55.     print(f"You collected all coal! ({miner_row}, {miner_col})")
  56.  
  57. else:
  58.     print(f"{total_coals} pieces of coal left. ({miner_row}, {miner_col})")
  59.  
Advertisement
Add Comment
Please, Sign In to add comment