Advertisement
Guest User

ManOWar2

a guest
Feb 26th, 2020
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.00 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 E2_ManOWar_06AUG2019 {
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.         List<Integer> pirateShip = Arrays.stream(scanner.nextLine().split(">")).map(Integer::parseInt).collect(Collectors.toList());
  10.         List<Integer> warship = Arrays.stream(scanner.nextLine().split(">")).map(Integer::parseInt).collect(Collectors.toList());
  11.         int maxHealth = Integer.parseInt(scanner.nextLine());
  12.         String command = scanner.nextLine();
  13.         boolean stalemate = true;
  14.  
  15.         while (!command.equals("Retire")) {
  16.             String[] tokens = command.split(" ");
  17.             switch (tokens[0]) {
  18.                 case "Fire":
  19.                     int indexFire = Integer.parseInt(tokens[1]);
  20.                     int damage = Integer.parseInt(tokens[2]);
  21.                     if (isIndexValid(warship, indexFire)) {
  22.                         warship.set(indexFire, warship.get(indexFire) - damage);
  23.                         if (warship.get(indexFire) <= 0) {
  24.                             System.out.println("You won! The enemy ship has sunken.");
  25.                             stalemate = false;
  26.                         }
  27.                     }
  28.                     break;
  29.                 case "Defend":
  30.                     int startIndex = Integer.parseInt(tokens[1]);
  31.                     int endIndex = Integer.parseInt(tokens[2]);
  32.                     int damageMyShip = Integer.parseInt(tokens[3]);
  33.                     if (isIndexValid(pirateShip, startIndex) &&
  34.                             isIndexValid(pirateShip, endIndex) && startIndex <= endIndex) {
  35.                         for (int i = startIndex; i <= endIndex; i++) {
  36.                             pirateShip.set(i, (pirateShip.get(i) - damageMyShip));
  37.                             if ((pirateShip.get(i) <= 0)) {
  38.                                 System.out.println("You lost! The pirate ship has sunken.");
  39.                                 stalemate = false;
  40.                                 break;
  41.                             }
  42.                         }
  43.                     }
  44.                     break;
  45.                 case "Repair":
  46.                     int indexToRepair = Integer.parseInt(tokens[1]);
  47.                     int health = Integer.parseInt(tokens[2]);
  48.                     if (isIndexValid(pirateShip, indexToRepair)) {
  49.                         int sectionRepaired = pirateShip.get(indexToRepair) + health;
  50.                         if (sectionRepaired > maxHealth) {
  51.                             sectionRepaired = maxHealth;
  52.                         }
  53.                         pirateShip.set(indexToRepair, sectionRepaired);
  54.                     }
  55.                     break;
  56.                 case "Status":
  57.                     int counter = 0;
  58.                     double needRepairMinimum = 0.2 * maxHealth;
  59.                     for (int i = 0; i < pirateShip.size(); i++) {
  60.                         if (pirateShip.get(i) < needRepairMinimum) {
  61.                             counter++;
  62.                         }
  63.  
  64.                     }
  65.                     System.out.printf("%d sections need repair.%n", counter);
  66.                     break;
  67.             }
  68.             if (!stalemate) {
  69.                 break;
  70.             }
  71.             command = scanner.nextLine();
  72.         }
  73.         int warshipSum = 0;
  74.         int pirateShipSum = 0;
  75.         if (stalemate) {
  76.             for (int i = 0; i < warship.size(); i++) {
  77.                 warshipSum += warship.get(i);
  78.             }
  79.             for (int i = 0; i < pirateShip.size(); i++) {
  80.                 pirateShipSum += pirateShip.get(i);
  81.             }
  82.             System.out.printf("Pirate ship status: %d%n", pirateShipSum);
  83.             System.out.printf("Warship status: %d%n", warshipSum);
  84.         }
  85.     }
  86.  
  87.     static boolean isIndexValid(List<Integer> list, int index) {
  88.         return index >= 0 && index <= list.size() - 1;
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement