Advertisement
Guest User

Man O War

a guest
Sep 2nd, 2022
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.52 KB | None | 0 0
  1. package MidExamRetake06;
  2.  
  3. import java.util.Arrays;
  4. import java.util.Scanner;
  5.  
  6. public class ManOWar {
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.  
  10.         int[] pirateShip = Arrays.stream(scanner.nextLine().split(">")).mapToInt(Integer::parseInt).toArray();
  11.         int[] warShip = Arrays.stream(scanner.nextLine().split(">")).mapToInt(Integer::parseInt).toArray();
  12.         int maxHealth = Integer.parseInt(scanner.nextLine());
  13.         boolean stalemate = true;
  14.  
  15.         String input = scanner.nextLine();
  16.  
  17.         while (!input.equals("Retire")) {
  18.             String[] command = input.split(" ");
  19.             switch (command[0]) {
  20.                 case "Fire":
  21.                     int fireIndex = Integer.parseInt(command[1]);
  22.                     int fireDamage = Integer.parseInt(command[2]);
  23.                     if (fireIndex >= 0 && fireIndex < warShip.length){
  24.                         warShip[fireIndex] -= fireDamage;
  25.                         if (warShip[fireIndex] <= 0)    {
  26.                             System.out.println("You won! The enemy ship has sunken.");
  27.                             stalemate = false;
  28.                             break;
  29.                         }
  30.                     }
  31.                     break;
  32.  
  33.                 case "Defend":
  34.                     int startIndex = Integer.parseInt(command[1]);
  35.                     int endIndex = Integer.parseInt(command[2]);
  36.                     int damage = Integer.parseInt(command[3]);
  37.                     if (startIndex >= 0 && startIndex < pirateShip.length && endIndex > 0 && endIndex < pirateShip.length && startIndex >= endIndex){
  38.                         for (int i = startIndex; i <= endIndex; i++) {
  39.                             pirateShip[i] -= damage;
  40.                             if (pirateShip[i] <= 0) {
  41.                                 System.out.println("You lost! The pirate ship has sunken.");
  42.                                 stalemate = false;
  43.                                 break;
  44.                             }
  45.                         }
  46.                     }
  47.                     break;
  48.  
  49.                 case "Repair":
  50.                     int repairIndex = Integer.parseInt(command[1]);
  51.                     int health = Integer.parseInt(command[2]);
  52.                     if (repairIndex >= 0 && repairIndex < pirateShip.length)    {
  53.                         pirateShip[repairIndex] += health;
  54.                         if (pirateShip[repairIndex] > maxHealth)    {
  55.                             pirateShip[repairIndex] = maxHealth;
  56.                         }
  57.                     }
  58.                     break;
  59.  
  60.                 case "Status":
  61.                     int sectionsToRepair = 0;
  62.                     for (int j : pirateShip) {
  63.                         if (j < (maxHealth * 0.2)) {
  64.                             sectionsToRepair++;
  65.                         }
  66.                     }
  67.                     System.out.printf("%d sections need repair.%n", sectionsToRepair);
  68.                     break;
  69.             }
  70.             input = scanner.nextLine();
  71.         }
  72.  
  73.         if (stalemate)  {
  74.             int pirateShipStatus = 0;
  75.             for (int j : pirateShip) {
  76.                 pirateShipStatus += j;
  77.             }
  78.             int warShipStatus = 0;
  79.             for (int j : warShip) {
  80.                 warShipStatus += j;
  81.             }
  82.             System.out.printf("Pirate ship status: %d%n" +
  83.                     "Warship status: %d", pirateShipStatus, warShipStatus);
  84.         }
  85.     }
  86. }
  87.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement