Advertisement
Guest User

Untitled

a guest
Apr 5th, 2020
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.12 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 ManOwar {
  8.     public static void main(String[] args) {
  9.         Scanner scan = new Scanner(System.in);
  10.  
  11.  
  12.         String[] pirateShip = scan.nextLine().split(">");
  13.         String[] warShip = scan.nextLine().split(">");
  14.  
  15.         List<Integer> pirates = new ArrayList<>();
  16.         List<Integer> warriors = new ArrayList<>();
  17.  
  18.  
  19.         for (int i = 0; i <pirateShip.length ; i++) {
  20.             pirates.add(Integer.parseInt(pirateShip[i]));
  21.         }
  22.  
  23.  
  24.         for (int i = 0; i < warShip.length ; i++) {
  25.             warriors.add(Integer.parseInt(warShip[i]));
  26.         }
  27.  
  28.         int maxHealth=Integer.parseInt(scan.nextLine());
  29.         String inputCommands=scan.nextLine();
  30.         boolean PiratesLost=false;
  31.         while (!inputCommands.equals("Retire")){
  32.             String[]arr=inputCommands.split(" ");
  33.  
  34.             switch (arr[0]){
  35.                 case "Fire":
  36.                     int index=Integer.parseInt(arr[1]);
  37.                     int damage=Integer.parseInt(arr[2]);
  38.  
  39.                     if(index>=0&&index<warriors.size()){
  40.                         int newVariable= warriors.get(index)-damage;
  41.                         if(newVariable<=0){
  42.  
  43.                             System.out.println("You won! The enemy ship has sunken.");
  44.                            PiratesLost=true;
  45.                             break;
  46.                         }else {
  47.                            warriors.set(index, newVariable);
  48.                         }
  49.                     }
  50.                     break;
  51.                 case"Defend":
  52.                     int starIndex=Integer.parseInt(arr[1]);
  53.                     int endIndex=Integer.parseInt(arr[2]);
  54.                     int damageS=Integer.parseInt(arr[3]);
  55.                     if((starIndex>=0&&starIndex<pirates.size())&&
  56.                             (endIndex>0&&endIndex<pirates.size())){
  57.                         for (int i = starIndex; i <=endIndex ; i++) {
  58.  
  59.                           int  newValueS=pirates.get(i)-damageS;
  60.                             if(newValueS<=0){
  61.                                 PiratesLost=true;
  62.                                 System.out.println("You lost! The pirate ship has sunken.");
  63.                             break;
  64.                             }
  65.                                 pirates.set(i, newValueS);
  66.                            
  67.  
  68.                         }
  69.                     }
  70.                     break;
  71.                 case"Repair":
  72.                     int indexT=Integer.parseInt(arr[1]);
  73.                     int health=Integer.parseInt(arr[2]);
  74.                     if(indexT>=0&&indexT<pirates.size()) {
  75.                         int newHealth=pirates.get(indexT)+health;
  76.                         if (newHealth<=maxHealth){
  77.                             pirates.set(indexT,newHealth);
  78.                         }
  79.                     }
  80.  
  81.                     break;
  82.                 case"Status":
  83.                     int count=0;
  84.                     double percent=maxHealth*0.2;
  85.                     for (int i = 0; i <pirates.size() ; i++) {
  86.                             if(pirates.get(i)<percent){//   ind /double
  87.                                 count++;
  88.                             }
  89.                     }if(count>0) {
  90.                     System.out.printf("%d sections need repair.%n", count);//?pri count=0
  91.                 }
  92.                     break;
  93.             }
  94.             if(PiratesLost){
  95.                 break;
  96.             }
  97.  
  98.             inputCommands=scan.nextLine();
  99.         }
  100.         if(!PiratesLost) {
  101.           int sumPirates=0;
  102.           int sumWarriors=0;
  103.  
  104.             for (int i = 0; i < pirates.size(); i++) {
  105.                 sumPirates = sumPirates + pirates.get(i);
  106.             }
  107.             for (int i = 0; i < warriors.size(); i++) {
  108.                 sumWarriors = sumWarriors + warriors.get(i);
  109.             }
  110.             System.out.printf("Pirate ship status: %d%n", sumPirates);
  111.             System.out.printf("Warship status: %d%n", sumWarriors);
  112.         }
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement