Advertisement
kalinx

dec-02

Jun 21st, 2021
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.21 KB | None | 0 0
  1. from collections import deque
  2.  
  3.  
  4. def move(row_col):
  5.     global board, player, text
  6.  
  7.     row, col = row_col
  8.     pl_row, pl_col = player
  9.     n_row, n_col = pl_row + row, pl_col + col
  10.     if n_row not in range(size) or n_col not in range(size):
  11.         text.pop()                        # remove last element from text
  12.         return
  13.     if board[n_row][n_col] != "-":
  14.         text.append(board[n_row][n_col])  # add element at position on board to text
  15.     board[pl_row][pl_col] = "-"           # replace "P"layer position with "-" on board
  16.     board[n_row][n_col] = "P"             # replace new position with "P"layer ob  board
  17.     player = (n_row, n_col)               # player get new position-(row, column)
  18.  
  19.  
  20. direction = {'left': (0, -1), 'up': (-1, 0), 'right': (0, 1), 'down': (1, 0)}
  21. board = []
  22.  
  23. text = deque(input())
  24. size = int(input())
  25. for r in range(size):  # read board and player get position-(row, column)
  26.     line = list(input())
  27.     if "P" in line:
  28.         player = (r, line.index("P"))
  29.     board.append(line)
  30.  
  31. m = int(input())
  32. for _ in range(m):
  33.     command = input()
  34.     move(direction.get(command))  # move to position of command
  35.  
  36. print("".join(text))
  37. [print(*r, sep='') for r in board]
  38.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement