Advertisement
go6odn28

Mouse_in_the_kitchen

Oct 19th, 2023
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.55 KB | None | 0 0
  1. def is_valid(value, max_value):
  2.     return 0 <= value < max_value
  3.  
  4. def next_move(command, current_row, current_col):
  5.     if command == 'up' and is_valid(current_row-1, rows):
  6.         return current_row-1, current_col
  7.     if command == 'down' and is_valid(current_row+1, rows):
  8.         return current_row+1, current_col
  9.     if command == 'left' and is_valid(current_col-1, cols):
  10.         return current_row, current_col-1
  11.     if command == 'right' and is_valid(current_col+1, cols):
  12.         return current_row, current_col+1
  13.     return None, None
  14.  
  15.  
  16. rows, cols = [int(x) for x in input().split(',')]
  17. field = []
  18. mouse_row, mouse_col = None, None
  19. boy_row, boy_col = None, None
  20. line = ' '
  21. cheese_coordinates = []
  22.  
  23.  
  24. for r in range(rows):
  25.     row = list(input())
  26.     field.append(row)
  27.     if 'M' in row:
  28.         mouse_row = r
  29.         mouse_col = row.index('M')
  30.         start_row = mouse_row
  31.         start_col = mouse_col
  32.     if "C" in row:
  33.         cheese_row = r
  34.         cheese_col = row.index('C')
  35.         cheese_coordinates.append([cheese_row, cheese_col])
  36.  
  37. while True:
  38.     line = input()
  39.     if line == "danger":
  40.         if len(cheese_coordinates) > 0:
  41.             print("Mouse will come back later!")
  42.             field[mouse_row][mouse_col] = 'M'
  43.             break
  44.         else:
  45.             print("Happy mouse! All the cheese is eaten, good night!")
  46.             field[mouse_row][mouse_col] = 'M'
  47.             break
  48.     next_row, next_col = next_move(line, mouse_row, mouse_col)
  49.     field[mouse_row][mouse_col] = "*"
  50.     if next_row is None or next_col is None:
  51.         print('No more cheese for tonight!')
  52.         field[mouse_row][mouse_col] = 'M'
  53.         break
  54.     if field[next_row][next_col] == "@":
  55.         continue
  56.     if field[next_row][next_col] == 'T':
  57.         field[mouse_row][mouse_col] = '*'
  58.         mouse_row, mouse_col = next_row, next_col
  59.         field[mouse_row][mouse_col] = 'P'
  60.         print("Mouse is trapped!")
  61.         field[mouse_row][mouse_col] = 'M'
  62.         break
  63.     if field[next_row][next_col] == 'C':
  64.         if [next_row, next_col] in cheese_coordinates:
  65.             cheese_coordinates.remove([next_row, next_col])
  66.         if not cheese_coordinates:
  67.             field[next_row][next_col] = 'M'
  68.             print("Happy mouse! All the cheese is eaten, good night!")
  69.             break
  70.         field[mouse_row][mouse_col] = '*'
  71.         mouse_row, mouse_col = next_row, next_col
  72.  
  73.     mouse_row, mouse_col = next_row, next_col
  74.     field[mouse_row][mouse_col] = '*'
  75.  
  76. for row in field:
  77.     print(''.join(row))
  78.    
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement