Advertisement
IvanIYankov

Untitled

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