Advertisement
simeonshopov

Revolt

Jun 22nd, 2020
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.12 KB | None | 0 0
  1. size = int(input())
  2. commands_count = int(input())
  3. matrix = [list(input())  for _ in range(size)]
  4.  
  5. MOVES = {
  6.     'up': (-1, 0),
  7.     'right': (0, +1),
  8.     'down': (+1, 0),
  9.     'left': (0, -1),
  10. }
  11. finish = False
  12.  
  13.  
  14. def next_coordinates(r, c, cc, operator):
  15.     if operator == '+':
  16.         next_r = r + MOVES[cc][0]
  17.         next_c = c + MOVES[cc][1]
  18.         return next_r, next_c
  19.     else:
  20.         next_r = r - MOVES[cc][0]
  21.         next_c = c - MOVES[cc][1]
  22.         return next_r, next_c
  23.  
  24.  
  25. def is_valid(n):
  26.     return 0 <= n < size
  27.  
  28.  
  29. def find_player():
  30.     global matrix
  31.  
  32.     for x in range(size):
  33.         for y in range(size):
  34.             if matrix[x][y] == 'f':
  35.                 return x, y
  36.  
  37.  
  38. def check_position(r, c):
  39.     row = r
  40.     col = c
  41.     if not is_valid(row):
  42.         if row < 0:
  43.             row = size -1
  44.         else:
  45.             row = 0
  46.     if not is_valid(col):
  47.         if col < 0:
  48.             col = size -1
  49.         else:
  50.             col = 0
  51.     return row, col
  52.  
  53.  
  54. def move(p: tuple, m: list, c: str):
  55.     global finish
  56.     next_row, next_col = next_coordinates(p[0], p[1], c, '+')
  57.     next_position = check_position(next_row, next_col)
  58.     cell = m[next_position[0]][next_position[1]]
  59.  
  60.     if cell == 'B':
  61.         next_row, next_col = next_coordinates(next_position[0], next_position[1], c, '+')
  62.         next_position = check_position(next_row, next_col)
  63.     elif cell == 'T':
  64.         next_row, next_col = next_coordinates(next_position[0], next_position[1], c, '-')
  65.         next_position = check_position(next_row, next_col)
  66.     elif cell == 'F':
  67.         finish = True
  68.         m[p[0]][p[1]] = '-'
  69.         p = (next_position[0], next_position[1])
  70.         m[p[0]][p[1]] = 'f'
  71.         return p, m
  72.     m[p[0]][p[1]] = '-'
  73.     p = (next_position[0], next_position[1])
  74.     m[p[0]][p[1]] = 'f'
  75.  
  76.     return p, m
  77.  
  78.  
  79. player = find_player()
  80.  
  81.  
  82. for _ in range(commands_count):
  83.     command = input()
  84.     player, matrix = move(player, matrix, command)
  85.     if finish:
  86.         break
  87.  
  88. if finish:
  89.     print('Player won!')
  90. else:
  91.     print('Player lost!')
  92. [print(''.join(x)) for x in matrix]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement