Advertisement
Kaloyankerr

08. Miner

Sep 27th, 2020
1,132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.83 KB | None | 0 0
  1. from collections import deque
  2.  
  3.  
  4. def get_start_position(count: int):
  5.     for x_ind in range(count):
  6.         if "s" in matrix[x_ind]:
  7.             for y_ind in range(count):
  8.                 if matrix[x_ind][y_ind] == "s":
  9.                     return [x_ind, y_ind]
  10.  
  11.  
  12. def make_a_move(move: str):
  13.     global game_over, coals_collected, current_position, row, col
  14.  
  15.     if move == "up":
  16.         if row - 1 >= 0:
  17.             current_position = [row - 1, col]
  18.  
  19.     elif move == "down":
  20.         if row + 1 < len(matrix):
  21.             current_position = [row + 1, col]
  22.  
  23.     elif move == "left":
  24.         if col - 1 >= 0:
  25.             current_position = [row, col - 1]
  26.  
  27.     elif move == "right":
  28.         if col + 1 < len(matrix):
  29.             current_position = [row, col + 1]
  30.  
  31.     if matrix[current_position[0]][current_position[1]] == "e":
  32.         game_over = True
  33.     elif matrix[current_position[0]][current_position[1]] == "c":
  34.         coals_collected += 1
  35.     matrix[current_position[0]][current_position[1]] = "*"
  36.  
  37.  
  38. n = int(input())
  39. move_commands = deque(input().split())
  40. matrix = deque([x for x in input().split()] for i in range(n))
  41. current_position = get_start_position(n)
  42.  
  43. game_over = False
  44. all_coals = sum(list(map(lambda x: x.count("c"), matrix)))
  45. coals_collected = 0
  46.  
  47. while True:
  48.     current_move = move_commands.popleft()
  49.     row = current_position[0]
  50.     col = current_position[1]
  51.  
  52.     make_a_move(current_move)
  53.  
  54.     if game_over:
  55.         print(f"Game over! ({current_position[0]}, {current_position[1]})")
  56.         break
  57.     elif coals_collected == all_coals:
  58.         print(f"You collected all coals! ({current_position[0]}, {current_position[1]})")
  59.         break
  60.     elif not move_commands:
  61.         print(f"{all_coals - coals_collected} coals left. ({current_position[0]}, {current_position[1]})")
  62.         break
  63.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement