Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ExamPrep.JFundamSept;
- import java.util.List;
- import java.util.Scanner;
- public class ManOWar {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int[] pirateShipSections = readShipSections(scanner);
- int[] enemyShipSections = readShipSections(scanner);
- 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 index = Integer.parseInt(parts[1]);
- int damage = Integer.parseInt(parts[2]);
- if (isValidIndex(index, enemyShipSections)) {
- enemyShipSections[index] -= damage;
- if (enemyShipSections[index] <= 0){
- enemyShipSunk = true;
- break;
- }
- }
- }
- break;
- case "Defend": {
- int fromIndex = Integer.parseInt(parts[1]);
- int toIndex = Integer.parseInt(parts[2]);
- int damage = Integer.parseInt(parts[3]);
- if (isValidIndex(fromIndex, pirateShipSections) && isValidIndex(toIndex, pirateShipSections)) {
- for (int index = fromIndex; index <= toIndex; index++){
- pirateShipSections[index] -= damage;
- if (pirateShipSections[index] <= 0){
- pirateShipSunk = true;
- break;
- }
- }
- if (pirateShipSunk) {
- break;
- }
- }
- break;
- }
- case "Repair": {
- int index = Integer.parseInt(parts[1]);
- int repair = Integer.parseInt(parts[2]);
- if (isValidIndex(index, pirateShipSections)) {
- pirateShipSections[index] += repair;
- if (pirateShipSections[index] > maxSectionHealth) {
- pirateShipSections[index] = maxSectionHealth;
- }
- }
- break;
- }
- case "Status": {
- int count = 0;
- for (int section : pirateShipSections) {
- if (section < 0.2 * maxSectionHealth){
- count++;
- }
- }
- System.out.printf("%d sections need repair.%n", count);
- break;
- }
- default:
- throw new IllegalStateException("Unknown command" + type);
- }
- 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.printf("Pirate ship status: %d%n", getShipStatus(pirateShipSections));
- System.out.printf("Warship status: %d%n", getShipStatus(enemyShipSections));
- }
- }
- private static int getShipStatus(int[] shipSections) {
- int sum = 0;
- for (int section : shipSections) {
- sum += section;
- }
- return sum;
- }
- private static int[] readShipSections(Scanner scanner) {
- String[] parts = scanner.nextLine().split(">");
- int[] ship = new int[parts.length];
- for (int i = 0; i < ship.length; i++) {
- ship[i] = Integer.parseInt(parts[i]);
- }
- return ship;
- }
- private static boolean isValidIndex(int index, int[] array) {
- return index >= 0 && index < array.length;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement