Advertisement
svephoto

Man O War [Java]

Feb 7th, 2020
600
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.16 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.List;
  3. import java.util.Scanner;
  4. import java.util.stream.Collectors;
  5.  
  6. public class ManOWar {
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.  
  10.         List<Integer> pirateShipStatus = Arrays.stream(scanner.nextLine().split(">"))
  11.                 .map(Integer::parseInt).collect(Collectors.toList());
  12.  
  13.         List<Integer> warshipStatus = Arrays.stream(scanner.nextLine().split(">"))
  14.                 .map(Integer::parseInt).collect(Collectors.toList());
  15.  
  16.         int maximumHealthCapacityPerSection = Integer.parseInt(scanner.nextLine());
  17.  
  18.         boolean isSunken = false;
  19.  
  20.         String command;
  21.  
  22.         while (!"Retire".equals(command = scanner.nextLine())) {
  23.             String[] tokens = command.split("\\s+");
  24.  
  25.             if ("Fire".equals(tokens[0])) {
  26.                 int index = Integer.parseInt(tokens[1]);
  27.                 int damage = Integer.parseInt(tokens[2]);
  28.  
  29.                 if (index >= 0 && index < warshipStatus.size()) {
  30.                     int warshipIndexForDamage = warshipStatus.get(index) - damage;
  31.  
  32.                     if (warshipIndexForDamage <= 0) {
  33.                         System.out.println("You won! The enemy ship has sunken.");
  34.                         isSunken = true;
  35.                         break;
  36.                     } else {
  37.                         warshipStatus.set(index, warshipIndexForDamage);
  38.                     }
  39.                 }
  40.             } else if ("Defend".equals(tokens[0])) {
  41.                 int startIndex = Integer.parseInt(tokens[1]);
  42.                 int endIndex = Integer.parseInt(tokens[2]);
  43.                 int damage = Integer.parseInt(tokens[3]);
  44.  
  45.                 if (startIndex >= 0 && endIndex < pirateShipStatus.size()) {
  46.                     for (int i = startIndex; i <= endIndex; i++) {
  47.                         int pirateShipIndexForDamage = pirateShipStatus.get(i) - damage;
  48.  
  49.                         if (pirateShipIndexForDamage <= 0) {
  50.                             System.out.println("You lost! The pirate ship has sunken.");
  51.                             isSunken = true;
  52.                             break;
  53.                         } else {
  54.                             pirateShipStatus.set(startIndex, pirateShipIndexForDamage);
  55.                             startIndex++;
  56.                         }
  57.                     }
  58.                 }
  59.             } else if ("Repair".equals(tokens[0])) {
  60.                 int index = Integer.parseInt(tokens[1]);
  61.                 int health = Integer.parseInt(tokens[2]);
  62.  
  63.                 if (index >= 0 && index < pirateShipStatus.size()) {
  64.                     if (health > maximumHealthCapacityPerSection) {
  65.                         health = maximumHealthCapacityPerSection;
  66.                         pirateShipStatus.set(index, health);
  67.                     } else {
  68.                         int repairedSection = pirateShipStatus.get(index) + health;
  69.                         pirateShipStatus.set(index, repairedSection);
  70.                     }
  71.                 }
  72.             } else if ("Status".equals(tokens[0])) {
  73.                 double sectionForRepair = maximumHealthCapacityPerSection * 0.2;
  74.                 int count = 0;
  75.  
  76.                 for (int currentSection : pirateShipStatus) {
  77.                     if (sectionForRepair > currentSection) {
  78.                         count++;
  79.                     }
  80.                 }
  81.  
  82.                 System.out.println(String.format("%d sections need repair.", count));
  83.             }
  84.         }
  85.  
  86.         if (!isSunken) {
  87.             int pirateShipSectionsSum = 0;
  88.             int warshipSectionsSum = 0;
  89.  
  90.             for (Integer currentPirateShipSectionStatus : pirateShipStatus) {
  91.                 pirateShipSectionsSum += currentPirateShipSectionStatus;
  92.             }
  93.  
  94.             System.out.println(String.format("Pirate ship status: %d", pirateShipSectionsSum));
  95.  
  96.             for (Integer currentWarshipSectionStatus : warshipStatus) {
  97.                 warshipSectionsSum += currentWarshipSectionStatus;
  98.             }
  99.  
  100.             System.out.println(String.format("Warship status: %d", warshipSectionsSum));
  101.         }
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement