Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
- import java.util.Scanner;
- import java.util.stream.Collectors;
- public class ManOwar {
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- String[] pirateShip = scan.nextLine().split(">");
- String[] warShip = scan.nextLine().split(">");
- List<Integer> pirates = new ArrayList<>();
- List<Integer> warriors = new ArrayList<>();
- for (int i = 0; i <pirateShip.length ; i++) {
- pirates.add(Integer.parseInt(pirateShip[i]));
- }
- for (int i = 0; i < warShip.length ; i++) {
- warriors.add(Integer.parseInt(warShip[i]));
- }
- int maxHealth=Integer.parseInt(scan.nextLine());
- String inputCommands=scan.nextLine();
- boolean PiratesLost=false;
- while (!inputCommands.equals("Retire")){
- String[]arr=inputCommands.split(" ");
- switch (arr[0]){
- case "Fire":
- int index=Integer.parseInt(arr[1]);
- int damage=Integer.parseInt(arr[2]);
- if(index>=0&&index<warriors.size()){
- int newVariable= warriors.get(index)-damage;
- if(newVariable<=0){
- System.out.println("You won! The enemy ship has sunken.");
- PiratesLost=true;
- break;
- }else {
- warriors.set(index, newVariable);
- }
- }
- break;
- case"Defend":
- int starIndex=Integer.parseInt(arr[1]);
- int endIndex=Integer.parseInt(arr[2]);
- int damageS=Integer.parseInt(arr[3]);
- if((starIndex>=0&&starIndex<pirates.size())&&
- (endIndex>0&&endIndex<pirates.size())){
- for (int i = starIndex; i <=endIndex ; i++) {
- int newValueS=pirates.get(i)-damageS;
- if(newValueS<=0){
- PiratesLost=true;
- System.out.println("You lost! The pirate ship has sunken.");
- break;
- }
- pirates.set(i, newValueS);
- }
- }
- break;
- case"Repair":
- int indexT=Integer.parseInt(arr[1]);
- int health=Integer.parseInt(arr[2]);
- if(indexT>=0&&indexT<pirates.size()) {
- int newHealth=pirates.get(indexT)+health;
- if (newHealth<=maxHealth){
- pirates.set(indexT,newHealth);
- }
- }
- break;
- case"Status":
- int count=0;
- double percent=maxHealth*0.2;
- for (int i = 0; i <pirates.size() ; i++) {
- if(pirates.get(i)<percent){// ind /double
- count++;
- }
- }if(count>0) {
- System.out.printf("%d sections need repair.%n", count);//?pri count=0
- }
- break;
- }
- if(PiratesLost){
- break;
- }
- inputCommands=scan.nextLine();
- }
- if(!PiratesLost) {
- int sumPirates=0;
- int sumWarriors=0;
- for (int i = 0; i < pirates.size(); i++) {
- sumPirates = sumPirates + pirates.get(i);
- }
- for (int i = 0; i < warriors.size(); i++) {
- sumWarriors = sumWarriors + warriors.get(i);
- }
- System.out.printf("Pirate ship status: %d%n", sumPirates);
- System.out.printf("Warship status: %d%n", sumWarriors);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement