Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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<Integer> pirateShipStatus = Arrays.stream(scanner.nextLine().split(">"))
- .map(Integer::parseInt).collect(Collectors.toList());
- List<Integer> warshipStatus = Arrays.stream(scanner.nextLine().split(">"))
- .map(Integer::parseInt).collect(Collectors.toList());
- int maximumHealthCapacityPerSection = Integer.parseInt(scanner.nextLine());
- boolean isSunken = false;
- String command;
- while (!"Retire".equals(command = scanner.nextLine())) {
- String[] tokens = command.split("\\s+");
- if ("Fire".equals(tokens[0])) {
- int index = Integer.parseInt(tokens[1]);
- int damage = Integer.parseInt(tokens[2]);
- if (index >= 0 && index < warshipStatus.size()) {
- int warshipIndexForDamage = warshipStatus.get(index) - damage;
- if (warshipIndexForDamage <= 0) {
- System.out.println("You won! The enemy ship has sunken.");
- isSunken = true;
- break;
- } else {
- warshipStatus.set(index, warshipIndexForDamage);
- }
- }
- } else if ("Defend".equals(tokens[0])) {
- int startIndex = Integer.parseInt(tokens[1]);
- int endIndex = Integer.parseInt(tokens[2]);
- int damage = Integer.parseInt(tokens[3]);
- if (startIndex >= 0 && endIndex < pirateShipStatus.size()) {
- for (int i = startIndex; i <= endIndex; i++) {
- int pirateShipIndexForDamage = pirateShipStatus.get(i) - damage;
- if (pirateShipIndexForDamage <= 0) {
- System.out.println("You lost! The pirate ship has sunken.");
- isSunken = true;
- break;
- } else {
- pirateShipStatus.set(startIndex, pirateShipIndexForDamage);
- startIndex++;
- }
- }
- }
- } else if ("Repair".equals(tokens[0])) {
- int index = Integer.parseInt(tokens[1]);
- int health = Integer.parseInt(tokens[2]);
- if (index >= 0 && index < pirateShipStatus.size()) {
- if (health > maximumHealthCapacityPerSection) {
- health = maximumHealthCapacityPerSection;
- pirateShipStatus.set(index, health);
- } else {
- int repairedSection = pirateShipStatus.get(index) + health;
- pirateShipStatus.set(index, repairedSection);
- }
- }
- } else if ("Status".equals(tokens[0])) {
- double sectionForRepair = maximumHealthCapacityPerSection * 0.2;
- int count = 0;
- for (int currentSection : pirateShipStatus) {
- if (sectionForRepair > currentSection) {
- count++;
- }
- }
- System.out.println(String.format("%d sections need repair.", count));
- }
- }
- if (!isSunken) {
- int pirateShipSectionsSum = 0;
- int warshipSectionsSum = 0;
- for (Integer currentPirateShipSectionStatus : pirateShipStatus) {
- pirateShipSectionsSum += currentPirateShipSectionStatus;
- }
- System.out.println(String.format("Pirate ship status: %d", pirateShipSectionsSum));
- for (Integer currentWarshipSectionStatus : warshipStatus) {
- warshipSectionsSum += currentWarshipSectionStatus;
- }
- System.out.println(String.format("Warship status: %d", warshipSectionsSum));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement