Advertisement
GeorgiLukanov87

man_o_war_functions100/100

Jun 23rd, 2022
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.30 KB | None | 0 0
  1. def valid_index(i, current_list):
  2.     if 0 <= i < len(current_list):
  3.         return True
  4.     return False
  5.  
  6.  
  7. def fire(enemy_ship):
  8.     index = int(command[1])
  9.     damage = int(command[2])
  10.     game_finished = False
  11.     if valid_index(index, enemy_ship):
  12.         warship[index] -= damage
  13.         if warship[index] <= 0:
  14.             game_finished = True
  15.             print('You won! The enemy ship has sunken.')
  16.             enemy_ship.clear()
  17.     return enemy_ship
  18.  
  19.  
  20. def defend(our_ship):
  21.     start_index = int(command[1])
  22.     end_index = int(command[2])
  23.     damage = int(command[3])
  24.     game_finished = False
  25.     if valid_index(start_index, our_ship) and valid_index(end_index, pirate_ship):
  26.         for s in range(start_index, end_index + 1):
  27.             our_ship[s] -= damage
  28.             if our_ship[s] <= 0:
  29.                 game_finished = True
  30.                 break
  31.         if game_finished:
  32.             print("You lost! The pirate ship has sunken.")
  33.             our_ship.clear()
  34.             return our_ship
  35.  
  36. def repair(our_ship):
  37.     index = int(command[1])
  38.     health = int(command[2])
  39.     if valid_index(index, our_ship):
  40.         if our_ship[index] + health <= max_health_capacity:
  41.             our_ship[index] += health
  42.         else:
  43.             diff = max_health_capacity - our_ship[index]
  44.             our_ship[index] += diff
  45.  
  46.  
  47. def status(our_ship):
  48.     repair_sections = [s for s in our_ship if s < (max_health_capacity * 0.2)]
  49.     return f"{len(repair_sections)} sections need repair."
  50.  
  51.  
  52. pirate_ship = list(map(int, input().split('>')))
  53. warship = list(map(int, input().split('>')))
  54. max_health_capacity = int(input())
  55.  
  56.  
  57. while True:
  58.     game_ended = False
  59.     command = input().split()
  60.     command_name = command[0]
  61.     if command_name == 'Retire':
  62.         if command_name == 'Retire':
  63.             print(f"Pirate ship status: {sum(pirate_ship)}\n"
  64.                   f"Warship status: {sum(warship)}")
  65.             break
  66.     elif command_name == 'Fire':
  67.         fire(warship)
  68.         if len(warship) == 0:
  69.             break
  70.     elif command_name == 'Defend':
  71.         defend(pirate_ship)
  72.         if len(pirate_ship) == 0:
  73.             break
  74.     elif command_name == 'Repair':
  75.         repair(pirate_ship)
  76.     elif command_name == 'Status':
  77.         print(status(pirate_ship))
  78.  
  79.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement