Advertisement
IvanIYankov

Untitled

Feb 28th, 2020
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.14 KB | None | 0 0
  1. package OnMyOwn;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class ManOWarAgain {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.  
  9.         String[] stringIntoInt1 = scanner.nextLine().split(">");
  10.         int firstSize = stringIntoInt1.length;
  11.         int[] pirateShipSections = new int[firstSize];
  12.         for (int i = 0; i < firstSize; i++) {
  13.              pirateShipSections[i] = Integer.parseInt(stringIntoInt1[i]);
  14.         }
  15.  
  16.         String[] stringIntoInt2 = scanner.nextLine().split(">");
  17.         int secondSize = stringIntoInt2.length;
  18.         int[] enemyShipSections = new int[secondSize];
  19.         for (int i = 0; i < secondSize; i++) {
  20.             enemyShipSections[i] = Integer.parseInt(stringIntoInt2[i]);
  21.         }
  22.  
  23.         int maxSectionHealth = Integer.parseInt(scanner.nextLine());
  24.  
  25.         String command = scanner.nextLine();
  26.  
  27.         boolean pirateShipSunk = false;
  28.         boolean enemyShipSunk = false;
  29.  
  30.  
  31.         while (!command.equals("Retire")){
  32.             String[] parts = command.split(" ");
  33.             String type = parts[0];
  34.  
  35.             switch (type) {
  36.                 case "Fire":
  37.                     int indexFire = Integer.parseInt(parts[1]);
  38.                     int damageFire = Integer.parseInt(parts[2]);
  39.  
  40.                     if (isValidIndex(indexFire, pirateShipSections)){
  41.                         enemyShipSections[indexFire] -= damageFire;
  42.                         if (enemyShipSections[indexFire] <= 0){
  43.                             enemyShipSunk = true;
  44.                         }
  45.                     }
  46.                     break;
  47.                 case "Defend":
  48.                     int fromIndex = Integer.parseInt(parts[1]);
  49.                     int toIndex = Integer.parseInt(parts[2]);
  50.                     int damageDefended = Integer.parseInt(parts[3]);
  51.  
  52.                     if (isValidIndex(fromIndex, pirateShipSections) && isValidIndex(toIndex, pirateShipSections)){
  53.                         for (int index = fromIndex; index <= toIndex; index++) {
  54.                             pirateShipSections[index] -= damageDefended;
  55.                             if (pirateShipSections[index] <= 0){
  56.                                 pirateShipSunk = true;
  57.                                 break;
  58.                             }
  59.                         }
  60.                     }
  61.                     break;
  62.                 case "Repair":
  63.                     int indexRepair = Integer.parseInt(parts[1]);
  64.                     int repair = Integer.parseInt(parts[2]);
  65.  
  66.                     if (isValidIndex(indexRepair, pirateShipSections)){
  67.                         pirateShipSections[indexRepair] += repair;
  68.                         if (pirateShipSections[indexRepair] > maxSectionHealth){
  69.                             pirateShipSections[indexRepair] = maxSectionHealth;
  70.                         }
  71.                     }
  72.                     break;
  73.                 case "Status":
  74.                     int count = 0;
  75.                     for (int section : pirateShipSections) {
  76.                         if (section < 0.2 * maxSectionHealth){
  77.                             count++;
  78.                         }
  79.                     }
  80.                     System.out.println(String.format("%d sections need repair.", count));
  81.  
  82.                     break;
  83.             }
  84.             command = scanner.nextLine();
  85.         }
  86.  
  87.         if (pirateShipSunk){
  88.             System.out.println("You lost! The pirate ship has sunken.");
  89.         }else if (enemyShipSunk){
  90.             System.out.println("You won! The enemy ship has sunken.");
  91.         }else {
  92.             System.out.println(String.format("Pirate ship status: %d", getShipStatus(pirateShipSections)));
  93.             System.out.println(String.format("Warship status: %d", getShipStatus(enemyShipSections)));
  94.  
  95.         }
  96.     }
  97.  
  98.     private static int getShipStatus(int[] pirateShipSections) {
  99.         int sum = 0;
  100.         for (int section: pirateShipSections) {
  101.             sum += section;
  102.         }
  103.         return sum;
  104.     }
  105.  
  106.     private static boolean isValidIndex(int index, int[] array) {
  107.         return index >= 0 && index < array.length;
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement