borovaneca

ManOwar

Apr 4th, 2023
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.37 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Arrays;
  3. import java.util.List;
  4. import java.util.Scanner;
  5. import java.util.stream.Collectors;
  6.  
  7. public class Man_O_War_03 {
  8.     public static void main(String[] args) {
  9.         Scanner scanner = new Scanner(System.in);
  10.  
  11.         List<Integer> ship = Arrays.stream(scanner.nextLine().split(">"))
  12.                 .map(Integer::parseInt)
  13.                 .collect(Collectors.toList());
  14.  
  15.         List<Integer> warship = Arrays.stream(scanner.nextLine().split(">"))
  16.                 .map(Integer::parseInt)
  17.                 .collect(Collectors.toList());
  18.  
  19.         int maxHealthASection = Integer.parseInt(scanner.nextLine());
  20.  
  21.         String command = scanner.nextLine();
  22.  
  23.         while (!command.split(" ")[0].equals("Retire")) {
  24.             String[] actions = command.split(" ");
  25.  
  26.             switch (actions[0]) {
  27.                 case "Fire":
  28.                     if (Integer.parseInt(actions[1]) > warship.size() - 1 || Integer.parseInt(actions[1]) < 0) {
  29.                         command = scanner.nextLine();
  30.                         continue;
  31.                     } else {
  32.                         int damage = Integer.parseInt(actions[2]);
  33.                         int result = warship.get(Integer.parseInt(actions[1])) - damage;
  34.                         warship.set(Integer.parseInt(actions[1]), result);
  35.                         if (result <= 0) {
  36.                             System.out.print("You won! The enemy ship has sunken.");
  37.                             return;
  38.                         }
  39.                     }
  40.                     break;
  41.                 case "Defend":
  42.                     int start = Integer.parseInt(actions[1]);
  43.                     int end = Integer.parseInt(actions[2]);
  44.                     if ((start > ship.size() - 1 || start < 0) || (end > ship.size() - 1 || end < 0)) {
  45.                         command = scanner.nextLine();
  46.                         continue;
  47.                     } else {
  48.                         if (start <= end) {
  49.                             for (int i = start; i <= end; i++) {
  50.                                 int damage = Integer.parseInt(actions[3]);
  51.                                 int result = ship.get(i) - damage;
  52.                                 ship.set(i, result);
  53.                                 if (result <= 0) {
  54.                                     System.out.print("You lost! The pirate ship has sunken.");
  55.                                     return;
  56.                                 }
  57.                             }
  58.                         }
  59.                     }
  60.                     break;
  61.                 case "Repair":
  62.                     if (Integer.parseInt(actions[1]) > ship.size() - 1 || Integer.parseInt(actions[1]) < 0) {
  63.                         command = scanner.nextLine();
  64.                         continue;
  65.                     } else {
  66.                         int health = Integer.parseInt(actions[2]);
  67.                         int repair = health + ship.get(Integer.parseInt(actions[1]));
  68.                         ship.set(Integer.parseInt(actions[1]), repair);
  69.                         if (ship.get(Integer.parseInt(actions[1])) > maxHealthASection) {
  70.                             ship.set(Integer.parseInt(actions[1]), maxHealthASection);
  71.                         }
  72.                     }
  73.                     break;
  74.                 case "Status":
  75.                     int counter = 0;
  76.                     for (int i = 0; i <= ship.size() - 1; i++) {
  77.                         int value = ship.get(i);
  78.                         double percent = (1.0 * value / maxHealthASection) * 100;
  79.                         if (percent < 20) {
  80.                             counter++;
  81.                         }
  82.                     }
  83.                     System.out.printf("%d sections need repair.", counter);
  84.                     System.out.println();
  85.                     break;
  86.             }
  87.  
  88.             command = scanner.nextLine();
  89.         }
  90.  
  91.         int sumShip = 0;
  92.         int sumWarship = 0;
  93.  
  94.         for (int i = 0; i <= ship.size() - 1; i++) {
  95.             sumShip += ship.get(i);
  96.         }
  97.  
  98.         for (int j = 0; j <= warship.size() - 1; j++) {
  99.             sumWarship += warship.get(j);
  100.         }
  101.  
  102.         System.out.printf("Pirate ship status: %d", sumShip);
  103.         System.out.println();
  104.         System.out.printf("Warship status: %d", sumWarship);
  105.  
  106.     }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment