Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Excercises;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Scanner;
- public class ManOWar {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String[] pirateShip = scanner.nextLine().split(">");
- List<Integer> pirateSection = new ArrayList<>();
- for (int i = 0; i < pirateShip.length; i++) {
- int section = Integer.parseInt(pirateShip[i]);
- pirateSection.add(section);
- }
- String[] warship = scanner.nextLine().split(">");
- List<Integer> warshipSection = new ArrayList<>();
- for (int i = 0; i < warship.length; i++) {
- int section = Integer.parseInt(warship[i]);
- warshipSection.add(section);
- }
- int maxHealthSection = Integer.parseInt(scanner.nextLine());
- boolean isGameOver = false;
- String input = scanner.nextLine();
- while (!"Retire".equals(input) || isGameOver) {
- String[] token = input.split(" ");
- String command = token[0];
- switch (command) {
- case "Fire": {
- int index = Integer.parseInt(token[1]);
- int damage = Integer.parseInt(token[2]);
- if (index < 0 || index >= warshipSection.size()) {
- break;
- }
- int section = warshipSection.get(index);
- int sectionAfterFire = section - damage;
- warshipSection.set(index, sectionAfterFire);
- if (sectionAfterFire <= 0) {
- System.out.println("You won! The enemy ship has sunken.");
- isGameOver = true;
- }
- break;
- }
- case "Defend": {
- int startIndex = Integer.parseInt(token[1]);
- int endIndex = Integer.parseInt(token[2]);
- int damage = Integer.parseInt(token[3]);
- if (startIndex < 0 || startIndex >= pirateSection.size()) {
- break;
- }
- if (endIndex < 0 || endIndex >= pirateSection.size()) {
- break;
- }
- for (int i = startIndex; i <= endIndex; i++) {
- int section = pirateSection.get(i);
- int pirateShipAfterFire = section - damage;
- pirateSection.set(i, pirateShipAfterFire);
- if (pirateShipAfterFire <= 0) {
- System.out.println("You lost! The pirate ship has sunken.");
- isGameOver = true;
- break;
- }
- }
- break;
- }
- case "Repair": {
- int index = Integer.parseInt(token[1]);
- int health = Integer.parseInt(token[2]);
- if (index < 0 || index >= warshipSection.size()) {
- break;
- }
- int section = pirateSection.get(index);
- int repaired = section + health;
- if (repaired > maxHealthSection) {
- repaired = maxHealthSection;
- }
- pirateSection.set(index, repaired);
- break;
- }
- case "Status": {
- int counter = 0;
- for (int i = 0; i < pirateSection.size(); i++) {
- double percentage = (pirateSection.get(i) * 1.0 / maxHealthSection) * 100;
- if (percentage < 20) {
- counter++;
- }
- }
- System.out.printf("%d sections need repair.%n", counter);
- break;
- }
- }
- input = scanner.nextLine();
- }
- int myShipSum = 0;
- int warshipSum = 0;
- for (int s :
- pirateSection) {
- myShipSum += s;
- }
- for (int z :
- warshipSection) {
- warshipSum += z;
- }
- System.out.printf("Pirate ship status: %d%n", myShipSum);
- System.out.printf("Warship status: %d", warshipSum);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement