Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.util.stream.Collectors;
- public class ManOWar {
- private static boolean isValidIndex(int index, List<Integer> ship) {
- boolean isValid = false;
- if (index >= 0 && index < ship.size()) {
- isValid = true;
- }
- return isValid;
- }
- private static void warShipAttacked(List<Integer> warShip, int index, int damage) {
- warShip.set(index, warShip.get(index) - damage);
- }
- private static boolean sectionStatus(List<Integer> warShip, int index) {
- boolean isDestroyed = false;
- if (warShip.get(index) <= 0) {
- isDestroyed = true;
- }
- return isDestroyed;
- }
- private static void shipAttacked(int startIndex, int endIndex, List<Integer> ship, int damage) {
- for (int i = startIndex; i <= endIndex; i++) {
- int currentIndexValue = ship.get(i);
- ship.set(i, currentIndexValue - damage);
- }
- }
- private static int repairSections(List<Integer> ship, int inputHealth) {
- double minimumHealth = inputHealth * 0.20;
- int counter = 0;
- for (int i = 0; i < ship.size(); i++) {
- int section = ship.get(i);
- if (section < minimumHealth) {
- counter++;
- }
- }
- return counter;
- }
- private static boolean shipSectionStatus(List<Integer> ship) {
- boolean isDamaged = false;
- for (int i = 0; i < ship.size(); i++) {
- if (ship.get(i) <= 0) {
- isDamaged = true;
- break;
- }
- }
- return isDamaged;
- }
- private static int statusShip(List<Integer> ship) {
- int sum = 0;
- for (int i = 0; i < ship.size(); i++) {
- sum += ship.get(i);
- }
- return sum;
- }
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- List<Integer> ship = Arrays.stream(sc.nextLine().split(">")).map(Integer::parseInt).collect(Collectors.toList());
- List<Integer> warShip = Arrays.stream(sc.nextLine().split(">")).map(Integer::parseInt).collect(Collectors.toList());
- int inputHealth = Integer.parseInt(sc.nextLine());
- boolean gameOver = false;
- String command = sc.nextLine();
- while (!command.equalsIgnoreCase("Retire")) {
- String[] tokens = command.split("\\s+");
- switch (tokens[0]) {
- case "Fire": {
- int index = Integer.parseInt(tokens[1]);
- int damage = Integer.parseInt(tokens[2]);
- if (isValidIndex(index, warShip)) {
- warShipAttacked(warShip, index, damage);
- if (sectionStatus(warShip, index)) {
- gameOver = true;
- System.out.println("You won! The enemy ship has sunken.");
- break;
- }
- }
- }
- break;
- case "Defend": {
- int startIndex = Integer.parseInt(tokens[1]);
- int endIndex = Integer.parseInt(tokens[2]);
- int damage = Integer.parseInt(tokens[3]);
- if (isValidIndex(startIndex, ship) && isValidIndex(endIndex, ship)) {
- shipAttacked(startIndex, endIndex, ship, damage);
- if (shipSectionStatus(ship)) {
- gameOver = true;
- System.out.println("You lost! The pirate ship has sunken.");
- break;
- }
- }
- }
- break;
- case "Repair": {
- int index = Integer.parseInt(tokens[1]);
- int health = Integer.parseInt(tokens[2]);
- if (isValidIndex(index, ship)) {
- int currentHealth = ship.get(index);
- int newHealth = currentHealth + health;
- if (newHealth > inputHealth) {
- newHealth = inputHealth;
- }
- ship.set(index, newHealth);
- }
- }
- break;
- case "Status":
- int result = repairSections(ship, inputHealth);
- System.out.printf("%d sections need repair.%n", result);
- break;
- }
- if (gameOver) {
- break;
- }
- command = sc.nextLine();
- }
- int shipSectionStatus = statusShip(ship);
- int warShipSectionStatus = statusShip(warShip);
- if(!gameOver){
- System.out.printf("Pirate ship status: %d%n", shipSectionStatus);
- System.out.printf("Warship status: %d%n", warShipSectionStatus);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement