Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package OnMyOwn;
- import java.util.Scanner;
- public class ManOWarAgain {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String[] stringIntoInt1 = scanner.nextLine().split(">");
- int firstSize = stringIntoInt1.length;
- int[] pirateShipSections = new int[firstSize];
- for (int i = 0; i < firstSize; i++) {
- pirateShipSections[i] = Integer.parseInt(stringIntoInt1[i]);
- }
- String[] stringIntoInt2 = scanner.nextLine().split(">");
- int secondSize = stringIntoInt2.length;
- int[] enemyShipSections = new int[secondSize];
- for (int i = 0; i < secondSize; i++) {
- enemyShipSections[i] = Integer.parseInt(stringIntoInt2[i]);
- }
- int maxSectionHealth = Integer.parseInt(scanner.nextLine());
- String command = scanner.nextLine();
- boolean pirateShipSunk = false;
- boolean enemyShipSunk = false;
- while (!command.equals("Retire")){
- String[] parts = command.split(" ");
- String type = parts[0];
- switch (type) {
- case "Fire":
- int indexFire = Integer.parseInt(parts[1]);
- int damageFire = Integer.parseInt(parts[2]);
- if (isValidIndex(indexFire, pirateShipSections)){
- enemyShipSections[indexFire] -= damageFire;
- if (enemyShipSections[indexFire] <= 0){
- enemyShipSunk = true;
- }
- }
- break;
- case "Defend":
- int fromIndex = Integer.parseInt(parts[1]);
- int toIndex = Integer.parseInt(parts[2]);
- int damageDefended = Integer.parseInt(parts[3]);
- if (isValidIndex(fromIndex, pirateShipSections) && isValidIndex(toIndex, pirateShipSections)){
- for (int index = fromIndex; index <= toIndex; index++) {
- pirateShipSections[index] -= damageDefended;
- if (pirateShipSections[index] <= 0){
- pirateShipSunk = true;
- break;
- }
- }
- }
- break;
- case "Repair":
- int indexRepair = Integer.parseInt(parts[1]);
- int repair = Integer.parseInt(parts[2]);
- if (isValidIndex(indexRepair, pirateShipSections)){
- pirateShipSections[indexRepair] += repair;
- if (pirateShipSections[indexRepair] > maxSectionHealth){
- pirateShipSections[indexRepair] = maxSectionHealth;
- }
- }
- break;
- case "Status":
- int count = 0;
- for (int section : pirateShipSections) {
- if (section < 0.2 * maxSectionHealth){
- count++;
- }
- }
- System.out.println(String.format("%d sections need repair.", count));
- break;
- }
- command = scanner.nextLine();
- }
- if (pirateShipSunk){
- System.out.println("You lost! The pirate ship has sunken.");
- }else if (enemyShipSunk){
- System.out.println("You won! The enemy ship has sunken.");
- }else {
- System.out.println(String.format("Pirate ship status: %d", getShipStatus(pirateShipSections)));
- System.out.println(String.format("Warship status: %d", getShipStatus(enemyShipSections)));
- }
- }
- private static int getShipStatus(int[] pirateShipSections) {
- int sum = 0;
- for (int section: pirateShipSections) {
- sum += section;
- }
- return sum;
- }
- private static boolean isValidIndex(int index, int[] array) {
- return index >= 0 && index < array.length;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement