Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.util.List;
- import java.util.Scanner;
- import java.util.stream.Collectors;
- public class E2_ManOWar_06AUG2019 {
- 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 command = scanner.nextLine();
- boolean stalemate = true;
- while (!command.equals("Retire")) {
- String[] tokens = command.split(" ");
- switch (tokens[0]) {
- case "Fire":
- int indexFire = Integer.parseInt(tokens[1]);
- int damage = Integer.parseInt(tokens[2]);
- if (isIndexValid(warship, indexFire)) {
- warship.set(indexFire, warship.get(indexFire) - damage);
- if (warship.get(indexFire) <= 0) {
- System.out.println("You won! The enemy ship has sunken.");
- stalemate = false;
- }
- } else {
- break;
- }
- break;
- case "Defend":
- int startIndex = Integer.parseInt(tokens[1]);
- int endIndex = Integer.parseInt(tokens[2]);
- int damageMyShip = Integer.parseInt(tokens[3]);
- if (isIndexValid(pirateShip, startIndex) &&
- isIndexValid(pirateShip, endIndex) && startIndex <= endIndex) {
- for (int i = startIndex; i <= endIndex; i++) {
- pirateShip.set(i, (pirateShip.get(i) - damageMyShip));
- if ((pirateShip.get(i) <= 0)) {
- System.out.println("You lost! The pirate ship has sunken.");
- stalemate = false;
- break;
- }
- }
- }
- break;
- case "Repair":
- int indexToRepair = Integer.parseInt(tokens[1]);
- int health = Integer.parseInt(tokens[2]);
- if (isIndexValid(pirateShip, indexToRepair)) {
- int sectionRepaired = pirateShip.get(indexToRepair) + health;
- if (sectionRepaired > maxHealth) {
- sectionRepaired = maxHealth;
- }
- pirateShip.set(indexToRepair, sectionRepaired);
- }
- break;
- case "Status":
- int counter = 0;
- double needRepairMinimum = 0.2 * maxHealth;
- for (int i = 0; i < pirateShip.size(); i++) {
- if (pirateShip.get(i) < needRepairMinimum) {
- counter++;
- }
- } System.out.printf("%d sections need repair.%n", counter);
- break;
- }
- command = scanner.nextLine();
- }
- int warshipSum = 0;
- int pirateShipSum = 0;
- if (stalemate) {
- for (int i = 0; i < warship.size(); i++) {
- warshipSum += warship.get(i);
- }
- for (int i = 0; i < pirateShip.size() ; i++) {
- pirateShipSum += pirateShip.get(i);
- }
- System.out.printf("Pirate ship status: %d%n",pirateShipSum);
- System.out.printf("Warship status: %d%n",warshipSum);
- }
- }
- static boolean isIndexValid(List<Integer> list, int index) {
- return index >= 0 && index <= list.size() - 1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement