Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- size = int(input())
- commands_count = int(input())
- matrix = [list(input()) for _ in range(size)]
- MOVES = {
- 'up': (-1, 0),
- 'right': (0, +1),
- 'down': (+1, 0),
- 'left': (0, -1),
- }
- finish = False
- def next_coordinates(r, c, cc, operator):
- if operator == '+':
- next_r = r + MOVES[cc][0]
- next_c = c + MOVES[cc][1]
- return next_r, next_c
- else:
- next_r = r - MOVES[cc][0]
- next_c = c - MOVES[cc][1]
- return next_r, next_c
- def is_valid(n):
- return 0 <= n < size
- def find_player():
- global matrix
- for x in range(size):
- for y in range(size):
- if matrix[x][y] == 'f':
- return x, y
- def check_position(r, c):
- row = r
- col = c
- if not is_valid(row):
- if row < 0:
- row = size -1
- else:
- row = 0
- if not is_valid(col):
- if col < 0:
- col = size -1
- else:
- col = 0
- return row, col
- def move(p: tuple, m: list, c: str):
- global finish
- next_row, next_col = next_coordinates(p[0], p[1], c, '+')
- next_position = check_position(next_row, next_col)
- cell = m[next_position[0]][next_position[1]]
- if cell == 'B':
- next_row, next_col = next_coordinates(next_position[0], next_position[1], c, '+')
- next_position = check_position(next_row, next_col)
- elif cell == 'T':
- next_row, next_col = next_coordinates(next_position[0], next_position[1], c, '-')
- next_position = check_position(next_row, next_col)
- elif cell == 'F':
- finish = True
- m[p[0]][p[1]] = '-'
- p = (next_position[0], next_position[1])
- m[p[0]][p[1]] = 'f'
- return p, m
- m[p[0]][p[1]] = '-'
- p = (next_position[0], next_position[1])
- m[p[0]][p[1]] = 'f'
- return p, m
- player = find_player()
- for _ in range(commands_count):
- command = input()
- player, matrix = move(player, matrix, command)
- if finish:
- break
- if finish:
- print('Player won!')
- else:
- print('Player lost!')
- [print(''.join(x)) for x in matrix]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement