Advertisement
g_to

ManOWar

Feb 25th, 2020
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.97 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.                     } else {
  28.                         break;
  29.                     }
  30.                     break;
  31.                 case "Defend":
  32.                     int startIndex = Integer.parseInt(tokens[1]);
  33.                     int endIndex = Integer.parseInt(tokens[2]);
  34.                     int damageMyShip = Integer.parseInt(tokens[3]);
  35.                     if (isIndexValid(pirateShip, startIndex) &&
  36.                             isIndexValid(pirateShip, endIndex) && startIndex <= endIndex) {
  37.                         for (int i = startIndex; i <= endIndex; i++) {
  38.                             pirateShip.set(i, (pirateShip.get(i) - damageMyShip));
  39.                             if ((pirateShip.get(i) <= 0)) {
  40.                                 System.out.println("You lost! The pirate ship has sunken.");
  41.                                 stalemate = false;
  42.                                 break;
  43.                             }
  44.                         }
  45.                     }
  46.  
  47.                     break;
  48.                 case "Repair":
  49.                     int indexToRepair = Integer.parseInt(tokens[1]);
  50.                     int health = Integer.parseInt(tokens[2]);
  51.                     if (isIndexValid(pirateShip, indexToRepair)) {
  52.                         int sectionRepaired = pirateShip.get(indexToRepair) + health;
  53.                         if (sectionRepaired > maxHealth) {
  54.                             sectionRepaired = maxHealth;
  55.                         }
  56.                         pirateShip.set(indexToRepair, sectionRepaired);
  57.                     }
  58.                     break;
  59.                 case "Status":
  60.                     int counter = 0;
  61.                     double needRepairMinimum = 0.2 * maxHealth;
  62.                     for (int i = 0; i < pirateShip.size(); i++) {
  63.                         if (pirateShip.get(i) < needRepairMinimum) {
  64.                             counter++;
  65.                         }
  66.  
  67.                     } System.out.printf("%d sections need repair.%n", counter);
  68.                     break;
  69.             }
  70.             command = scanner.nextLine();
  71.         }
  72.         int warshipSum = 0;
  73.         int pirateShipSum = 0;
  74.         if (stalemate) {
  75.             for (int i = 0; i < warship.size(); i++) {
  76.                   warshipSum += warship.get(i);
  77.             }
  78.             for (int i = 0; i < pirateShip.size() ; i++) {
  79.                 pirateShipSum += pirateShip.get(i);
  80.             }
  81.             System.out.printf("Pirate ship status: %d%n",pirateShipSum);
  82.             System.out.printf("Warship status: %d%n",warshipSum);
  83.         }
  84.     }
  85.  
  86.     static boolean isIndexValid(List<Integer> list, int index) {
  87.         return index >= 0 && index <= list.size() - 1;
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement