import java.util.Arrays; import java.util.List; import java.util.Scanner; import java.util.stream.Collectors; public class ManOWar { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); List pirateShip = Arrays.stream(scanner.nextLine().split(">")).map(Integer::parseInt).collect(Collectors.toList()); List warShip = Arrays.stream(scanner.nextLine().split(">")).map(Integer::parseInt).collect(Collectors.toList()); int maxHealth = Integer.parseInt(scanner.nextLine()); String input = scanner.nextLine(); while (!input.contains("Retire")) { String[] tokens = input.split(" "); String command = tokens[0]; switch (command) { case "Fire": int index = Integer.parseInt(tokens[1]); int damage = Integer.parseInt(tokens[2]); if (index > warShip.size()-1 || index < 0) { input = scanner.nextLine(); continue; } else { int health = warShip.get(index); int currentHeath = health - damage; warShip.set(index, currentHeath); if (currentHeath <= 0) { System.out.println("You won! The enemy ship has sunken."); return; } } break; case "Defend": int startIndex = Integer.parseInt(tokens[1]); int endIndex = Integer.parseInt(tokens[2]); int damageInflicted = Integer.parseInt(tokens[3]); if ((startIndex > pirateShip.size()-1 || startIndex < 0) || (endIndex > pirateShip.size()-1 || endIndex < 0)) { input = scanner.nextLine(); continue; } if (startIndex <= endIndex) { for (int i = startIndex; i <= endIndex; i++) { int newValue = pirateShip.get(i) - damageInflicted; pirateShip.set(i, newValue); if (newValue <= 0) { System.out.println("You lost! The pirate ship has sunken."); return; } } } break; case "Repair": int indexToRepair = Integer.parseInt(tokens[1]); int health = Integer.parseInt(tokens[2]); if (isValidIndex(pirateShip, indexToRepair)) { int newRepairPoints = pirateShip.get(indexToRepair) + health; pirateShip.set(indexToRepair, newRepairPoints); if (newRepairPoints > maxHealth) { pirateShip.set(indexToRepair, maxHealth); } } break; case "Status": printPercent(pirateShip, maxHealth); break; } input = scanner.nextLine(); } printPirateShip(pirateShip); printWarShip(warShip); } public static boolean isValidIndex(List arrayList, int index) { return arrayList.size()-1 >= index && index >=0; } public static void printPercent(List pirateShip, int maxHealth) { int count = 0; for (int i = 0; i <= pirateShip.size()-1; i++) { int value = pirateShip.get(i); double percent = (1.0 *value / maxHealth) * 100; if (percent < 20) { count++; } } System.out.printf("%d sections need repair.", count); System.out.println(); } public static void printPirateShip(List pirateShip) { int count = 0; for (int number : pirateShip) { count += number; } System.out.printf("Pirate ship status: %d", count); System.out.println(); } public static void printWarShip(List warShip) { int count = 0; for (int number : warShip) { count += number; } System.out.printf("Warship status: %d", count); } }