Advertisement
Guest User

ManOWar

a guest
Feb 26th, 2020
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.96 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.stream.Collectors;
  3.  
  4. public class ManOWar {
  5.  
  6.     private static boolean isValidIndex(int index, List<Integer> ship) {
  7.         boolean isValid = false;
  8.         if (index >= 0 && index < ship.size()) {
  9.             isValid = true;
  10.         }
  11.         return isValid;
  12.     }
  13.  
  14.     private static void warShipAttacked(List<Integer> warShip, int index, int damage) {
  15.         warShip.set(index, warShip.get(index) - damage);
  16.     }
  17.  
  18.     private static boolean sectionStatus(List<Integer> warShip, int index) {
  19.         boolean isDestroyed = false;
  20.         if (warShip.get(index) <= 0) {
  21.             isDestroyed = true;
  22.         }
  23.         return isDestroyed;
  24.     }
  25.  
  26.     private static void shipAttacked(int startIndex, int endIndex, List<Integer> ship, int damage) {
  27.         for (int i = startIndex; i <= endIndex; i++) {
  28.             int currentIndexValue = ship.get(i);
  29.             ship.set(i, currentIndexValue - damage);
  30.         }
  31.     }
  32.  
  33.     private static int repairSections(List<Integer> ship, int inputHealth) {
  34.         double minimumHealth = inputHealth * 0.20;
  35.         int counter = 0;
  36.         for (int i = 0; i < ship.size(); i++) {
  37.             int section = ship.get(i);
  38.             if (section < minimumHealth) {
  39.                 counter++;
  40.             }
  41.         }
  42.         return counter;
  43.     }
  44.  
  45.     private static boolean shipSectionStatus(List<Integer> ship) {
  46.         boolean isDamaged = false;
  47.         for (int i = 0; i < ship.size(); i++) {
  48.             if (ship.get(i) <= 0) {
  49.                 isDamaged = true;
  50.                 break;
  51.             }
  52.         }
  53.         return isDamaged;
  54.     }
  55.  
  56.     private static int statusShip(List<Integer> ship) {
  57.         int sum = 0;
  58.         for (int i = 0; i < ship.size(); i++) {
  59.             sum += ship.get(i);
  60.         }
  61.         return sum;
  62.     }
  63.  
  64.     public static void main(String[] args) {
  65.         Scanner sc = new Scanner(System.in);
  66.         List<Integer> ship = Arrays.stream(sc.nextLine().split(">")).map(Integer::parseInt).collect(Collectors.toList());
  67.         List<Integer> warShip = Arrays.stream(sc.nextLine().split(">")).map(Integer::parseInt).collect(Collectors.toList());
  68.         int inputHealth = Integer.parseInt(sc.nextLine());
  69.         boolean gameOver = false;
  70.  
  71.         String command = sc.nextLine();
  72.         while (!command.equalsIgnoreCase("Retire")) {
  73.             String[] tokens = command.split("\\s+");
  74.             switch (tokens[0]) {
  75.                 case "Fire": {
  76.                     int index = Integer.parseInt(tokens[1]);
  77.                     int damage = Integer.parseInt(tokens[2]);
  78.                     if (isValidIndex(index, warShip)) {
  79.                         warShipAttacked(warShip, index, damage);
  80.                         if (sectionStatus(warShip, index)) {
  81.                             gameOver = true;
  82.                             System.out.println("You won! The enemy ship has sunken.");
  83.                             break;
  84.                         }
  85.                     }
  86.                 }
  87.                 break;
  88.                 case "Defend": {
  89.                     int startIndex = Integer.parseInt(tokens[1]);
  90.                     int endIndex = Integer.parseInt(tokens[2]);
  91.                     int damage = Integer.parseInt(tokens[3]);
  92.                     if (isValidIndex(startIndex, ship) && isValidIndex(endIndex, ship)) {
  93.                         shipAttacked(startIndex, endIndex, ship, damage);
  94.                         if (shipSectionStatus(ship)) {
  95.                             gameOver = true;
  96.                             System.out.println("You lost! The pirate ship has sunken.");
  97.                             break;
  98.                         }
  99.                     }
  100.                 }
  101.                 break;
  102.                 case "Repair": {
  103.                     int index = Integer.parseInt(tokens[1]);
  104.                     int health = Integer.parseInt(tokens[2]);
  105.                     if (isValidIndex(index, ship)) {
  106.                         int currentHealth = ship.get(index);
  107.                         int newHealth = currentHealth + health;
  108.                         if (newHealth > inputHealth) {
  109.                             newHealth = inputHealth;
  110.                         }
  111.                         ship.set(index, newHealth);
  112.                     }
  113.                 }
  114.                 break;
  115.                 case "Status":
  116.                     int result = repairSections(ship, inputHealth);
  117.                     System.out.printf("%d sections need repair.%n", result);
  118.                     break;
  119.             }
  120.             if (gameOver) {
  121.                 break;
  122.             }
  123.             command = sc.nextLine();
  124.         }
  125.  
  126.         int shipSectionStatus = statusShip(ship);
  127.         int warShipSectionStatus = statusShip(warShip);
  128.  
  129.         if(!gameOver){
  130.             System.out.printf("Pirate ship status: %d%n", shipSectionStatus);
  131.             System.out.printf("Warship status: %d%n", warShipSectionStatus);
  132.         }
  133.  
  134.     }
  135.  
  136.  
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement