Advertisement
horozov86

Man_O_War

Feb 14th, 2023
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.93 KB | None | 0 0
  1. def is_valid_idx(idx, seq):
  2.     return 0 <= idx < len(seq)
  3.  
  4.  
  5. pirate_ship = [int(x) for x in input().split(">")] # секциите на пиратския кораб като цели числа
  6. warship = [int(x) for x in input().split(">")] # секциите на бойния кораб като цели числа
  7. maximum_section_health = int(input()) # максималкия здравина/здраве на секция
  8.  
  9. flag = True
  10. while flag:
  11.     line = input()
  12.     if line == "Retire":
  13.         break
  14.  
  15.     command_split = line.split()
  16.     command = command_split[0] # извеждаме само командите, които са на индекс 0
  17.  
  18.     # дали индекс е валиден го има и за трите команди, което може да се изведе като обща функция
  19.     if command == "Fire":
  20.         idx = int(command_split[1]) # извеждаме текущия индекс като цяло число
  21.         damage = int(command_split[2]) # извеждаме текущия damage като цяло число
  22.         if not is_valid_idx(idx, warship): # валидирам индекс, т.е проверяваме ако индекса НЕ Е валиден
  23.             continue
  24.         warship[idx] -= damage # от секцията под този индекс на бпойния кораб изважадаме damage
  25.         if warship[idx] <= 0: # по условия, ако секцията стане <= 0, т.е. health <= 0 се принтира съобщения
  26.             flag = False
  27.             print(f"You won! The enemy ship has sunken.")
  28.  
  29.     elif command == "Defend": # разликата е, че се атакуват всички клетки. Трябва да се валидират и двата индекса, начален и краен
  30.         start_idx = int(command_split[1])
  31.         end_inx = int(command_split[2])
  32.         damage = int(command_split[3])
  33.         if not is_valid_idx(start_idx, pirate_ship) or not is_valid_idx(end_inx, pirate_ship):
  34.             continue
  35.         for idx in range(start_idx, end_inx + 1):
  36.             pirate_ship[idx] -= damage
  37.             if pirate_ship[idx] <= 0:
  38.                 print(f"You lost! The pirate ship has sunken.")
  39.                 flag = False
  40.                 break
  41.     elif command == "Repair":
  42.         idx = int(command_split[1])
  43.         health = int(command_split[2])
  44.         if not is_valid_idx(idx, pirate_ship):
  45.             continue
  46.         pirate_ship[idx] = min(maximum_section_health, pirate_ship[idx] + health)
  47.  
  48.     elif command == "Status":
  49.         threshold = maximum_section_health * 0.2
  50.         counter = 0
  51.         for section in pirate_ship:
  52.             if section < threshold:
  53.                 counter += 1
  54.         print(f"{counter} sections need repair.")
  55.  
  56. if flag:
  57.     print(f"Pirate ship status: {sum(pirate_ship)}")
  58.     print(f"Warship status: {sum(warship)}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement