Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!usr/local/bin/python3.8
- # -*- coding: utf-8 -*import
- def find_player(m: list):
- """
- Find the initial position of the player.
- :param m:
- :return: x: int, y: int
- """
- for x in range(matrix_size):
- for y in range(len(m[x])):
- if m[x][y] == 'P':
- return x, y
- def is_valid(i: int):
- return 0 <= i < matrix_size
- def up(x: int, y: int):
- return x - 1, y
- def down(x: int, y: int):
- return x + 1, y
- def left(x: int, y: int):
- return x, y - 1
- def right(x: int, y: int):
- return x, y + 1
- def manage(x: str):
- global matrix, player_row, player_col, text, MOVES
- new_row, new_col = MOVES[x](player_row, player_col)
- if not is_valid(new_row) or not is_valid(new_col):
- if text:
- text = text[:-1]
- else:
- cell = matrix[new_row][new_col]
- if cell != '-':
- text += matrix[new_row][new_col]
- matrix[new_row][new_col] = 'P'
- matrix[player_row][player_col] = '-'
- player_row, player_col = new_row, new_col
- else:
- matrix[new_row][new_col] = 'P'
- matrix[player_row][player_col] = '-'
- player_row, player_col = new_row, new_col
- MOVES = {
- 'up': up,
- 'down': down,
- 'left': left,
- 'right': right,
- }
- text = input()
- matrix_size = int(input())
- matrix = [list(input()) for _ in range(matrix_size)]
- player_row, player_col = find_player(matrix)
- while True:
- move = input()
- if move == 'end':
- break
- manage(move)
- if text:
- print(text)
- [print(''.join(x)) for x in matrix]
Advertisement
Add Comment
Please, Sign In to add comment