Guest User

Untitled

a guest
Dec 16th, 2023
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.02 KB | None | 0 0
  1. def is_valid_move(row, col, matrix):
  2.     return 0 <= row < 5 and 0 <= col < 5 and matrix[row][col] == '.'
  3.  
  4.  
  5. def is_valid_shot(row, col, matrix):
  6.     return 0 <= row < 5 and 0 <= col < 5 and matrix[row][col] == 'x'
  7.  
  8.  
  9. matrix = []
  10. current_position = []
  11. targets_count = 0
  12. directions = {'up': (-1, 0), 'down': (1, 0), 'right': (0, 1), 'left': (0, -1)}
  13. killed_targets = []
  14.  
  15. for row in range(5):
  16.     matrix.append(input().split())
  17.     for col in range(5):
  18.         if matrix[row][col] == 'A':
  19.             current_position = [row, col]
  20.         elif matrix[row][col] == 'x':
  21.             targets_count += 1
  22.  
  23. initial_targets = targets_count
  24. number_of_commands = int(input())
  25.  
  26. for _ in range(number_of_commands):
  27.     command = input().split()
  28.     direction = command[1]
  29.  
  30.     if command[0] == 'shoot':
  31.         row = current_position[0] + directions[direction][0]
  32.         col = current_position[1] + directions[direction][1]
  33.         while 0 <= row < 5 and 0 <= col < 5:
  34.             if is_valid_shot(row, col, matrix):
  35.                 matrix[row][col] = '.'
  36.                 targets_count -= 1
  37.                 killed_targets.append([row, col])
  38.                 break
  39.             row += directions[direction][0]
  40.             col += directions[direction][1]
  41.  
  42.         if targets_count == 0:
  43.             print(f'Training completed! All {len(killed_targets)} targets hit.')
  44.             break
  45.  
  46.     elif command[0] == 'move':
  47.         steps = int(command[2])
  48.         #        for _ in range(steps):
  49.         row = current_position[0] + directions[direction][0] * steps
  50.         col = current_position[1] + directions[direction][1] * steps
  51.  
  52.         if is_valid_move(row, col, matrix):
  53.             matrix[current_position[0]][current_position[1]] = '.'
  54.             matrix[row][col] = 'A'
  55.             current_position[0] = row
  56.             current_position[1] = col
  57. #            else:
  58. #                break
  59.  
  60. if targets_count > 0:
  61.     print(f'Training not completed! {targets_count} targets left.')
  62.  
  63. [print(x) for x in killed_targets]
  64.  
Advertisement
Add Comment
Please, Sign In to add comment