Kumetsa

Untitled

Jun 7th, 2023
240
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.43 KB | None | 0 0
  1. def check_valid_index(row, col):
  2.     if 0 <= row < SIZE and 0 <= col < SIZE:
  3.         return True
  4.     return False
  5.  
  6.  
  7. def player_position(matrix):
  8.     for i in range(len(matrix)):
  9.         for j in range(len(matrix[0])):
  10.             if matrix[i][j] == "A":
  11.                 return i, j
  12.  
  13.     return None
  14.  
  15.  
  16. def move_player(direction, steps):
  17.  
  18.     curr_row, curr_col = player_position(matrix)
  19.     new_row, new_col = curr_row, curr_col
  20.  
  21.     if direction == "left":
  22.         new_row, new_col = curr_row, curr_col - steps
  23.     elif direction == "right":
  24.         new_row, new_col = curr_row, curr_col + steps
  25.     elif direction == "up":
  26.         new_row, new_col = curr_row - steps, curr_col
  27.     elif direction == "down":
  28.         new_row, new_col = curr_row + steps, curr_col
  29.  
  30.     if check_valid_index(new_row, new_col) and matrix[new_row][new_col] == ".":
  31.         matrix[new_row][new_col] = "A"
  32.         matrix[curr_row][curr_col] = "."
  33.  
  34.  
  35. def shoot_target(direction):
  36.     player_row, player_col = player_position(matrix)
  37.     next_row, next_col = player_row + directions[direction][0], player_col + directions[direction][1]
  38.  
  39.     while check_valid_index(next_row, next_col):
  40.         if matrix[next_row][next_col] == "x":
  41.             matrix[next_row][next_col] = "."
  42.             return next_row, next_col
  43.         elif matrix[next_row][next_col] == ".":
  44.             next_row += directions[direction][0]
  45.             next_col += directions[direction][1]
  46.         else:
  47.             break
  48.  
  49.     return None
  50.  
  51.  
  52. SIZE = 5
  53.  
  54. matrix = [[el for el in input().split()] for _ in range(SIZE)]
  55. count_of_targets = sum(row.count("x") for row in matrix)
  56.  
  57.  
  58. n_commands = int(input())
  59.  
  60. directions = {
  61.     "up": (-1, 0),
  62.     "down": (1, 0),
  63.     "left": (0, -1),
  64.     "right": (0, 1),
  65. }
  66.  
  67. targets_shot = []
  68.  
  69. for _ in range(n_commands):
  70.     command, *info = input().split()
  71.  
  72.     if command == "move":
  73.         direction = info[0]
  74.         steps = int(info[1])
  75.         move_player(direction, steps)
  76.  
  77.     elif command == "shoot":
  78.         direction = info[0]
  79.         target = shoot_target(direction)
  80.         if target:
  81.             targets_shot.append(target)
  82.  
  83.  
  84. if count_of_targets == len(targets_shot):
  85.     print(f"Training completed! All {count_of_targets} targets hit.")
  86. else:
  87.     print(f"Training not completed! {count_of_targets - len(targets_shot)} targets left.")
  88.  
  89. if targets_shot:
  90.     for target in targets_shot:
  91.         print(list(target))
Tags: Range Day
Advertisement
Comments
  • g0g00z
    2 years
    # text 0.10 KB | 0 0
    1. Липсва ти тази проверка
    2. "If at any point there are no targets left, end the program"
Add Comment
Please, Sign In to add comment