Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package midExam;
- import java.util.Arrays;
- import java.util.List;
- import java.util.Scanner;
- import java.util.stream.Collectors;
- public class manOfWar {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- List<Integer> pirateShip = Arrays.stream(scanner.nextLine().split(">")).
- map(Integer::parseInt).collect(Collectors.toList());
- List<Integer> warShip = Arrays.stream(scanner.nextLine().split(">")).
- map(Integer::parseInt).collect(Collectors.toList());
- int maxHealth = Integer.parseInt(scanner.nextLine());
- String comand = scanner.nextLine();
- int countPirate = 0;
- boolean isSunk = false;
- while (!comand.equals("Retire")){
- String[] comandArr = comand.split(" ");
- if (comand.contains("Fire")){
- int index = Integer.parseInt(comandArr[1]);
- int damage = Integer.parseInt(comandArr[2]);
- if (index >= 0 && index <= warShip.size() -1){
- int curentIndexWarShip = warShip.get(index);
- curentIndexWarShip -= damage;
- warShip.set(index, curentIndexWarShip);
- if (curentIndexWarShip <= 0){
- System.out.println("You won! The enemy ship has sunken.");
- break;
- }
- }
- }else if (comand.contains("Defend")){
- int startIndex = Integer.parseInt(comandArr[1]);
- int endIndex = Integer.parseInt(comandArr[2]);
- int damagePirate = Integer.parseInt(comandArr[3]);
- if (startIndex >= 0 && startIndex < pirateShip.size() && endIndex >= 0 && endIndex < pirateShip.size()){
- for (int i = startIndex; i <= endIndex; i++) {
- int curentSection = pirateShip.get(i);
- curentSection -= damagePirate;
- if (curentSection <= 0){
- System.out.println("You lost! The pirate ship has sunken.");
- pirateShip.set(i, curentSection);
- isSunk = true;
- break;
- }
- pirateShip.set(i, curentSection);
- }
- }
- }else if (comand.contains("Repair")){
- int indexSection = Integer.parseInt(comandArr[1]);
- int health = Integer.parseInt(comandArr[2]);
- if (indexSection >= 0 && indexSection < pirateShip.size()){
- int valuePirateSection = pirateShip.get(indexSection);
- if (valuePirateSection + health >= maxHealth){
- valuePirateSection = maxHealth;
- pirateShip.set(indexSection, valuePirateSection);
- }else {
- valuePirateSection += health;
- pirateShip.set(indexSection, valuePirateSection);
- }
- }
- }else if (comand.equals("Status")){
- double status = maxHealth * 0.2;
- for (int item : pirateShip) {
- if (item < status){
- countPirate += 1;
- }
- }
- System.out.printf("%d sections need repair.%n", countPirate);
- }
- comand = scanner.nextLine();
- }
- if (isSunk){
- return;
- }
- int pirate = 0;
- int war = 0;
- for (int item : pirateShip) {
- pirate += item;
- }
- for (int product : warShip) {
- war += product;
- }
- System.out.printf("Pirate ship status: %d%n", pirate);
- System.out.printf("Warship status: %d", war);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement