Advertisement
Ligh7_of_H3av3n

03. Moving Target Corrected

Feb 12th, 2024
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.09 KB | None | 0 0
  1. package MoreExercise;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Scanner;
  6.  
  7. public class Main {
  8.  
  9.     public static void main(String[] args) {
  10.         Scanner scanner = new Scanner(System.in);
  11.  
  12.         List<Integer> targets = new ArrayList<>();
  13.         String[] initialTargets = scanner.nextLine().split(" ");
  14.         for (String target : initialTargets) {
  15.             targets.add(Integer.parseInt(target));
  16.         }
  17.  
  18.         while (true) {
  19.             String input = scanner.nextLine();
  20.  
  21.             if (input.equals("End")) {
  22.                 break;
  23.             }
  24.  
  25.             String[] commandInput = input.split(" ");
  26.  
  27.             String command = commandInput[0];
  28.             int index = Integer.parseInt(commandInput[1]);
  29.  
  30.             if (command.equals("Shoot")) {
  31.                 int power = Integer.parseInt(commandInput[2]);
  32.  
  33.                 if (index >= 0 && index < targets.size()) {
  34.                     targets.set(index, targets.get(index) - power);
  35.  
  36.                     if (targets.get(index) <= 0) {
  37.                         targets.remove(index);
  38.                     }
  39.                 }
  40.             } else if (command.equals("Add")) {
  41.                 int value = Integer.parseInt(commandInput[2]);
  42.  
  43.                 if (index >= 0 && index < input.length()) {
  44.                     targets.add(index, value);
  45.                 } else {
  46.                     System.out.println("Invalid placement!");
  47.                 }
  48.             } else if (command.equals("Strike")) {
  49.                 int radius = Integer.parseInt(commandInput[2]);
  50.  
  51.                 if (index - radius >= 0 && index + radius < targets.get(targets.size() - 1)) {
  52.                     targets.subList(index - radius, index + radius + 1).clear();
  53.                 } else {
  54.                     System.out.println("Strike missed!");
  55.                 }
  56.             }
  57.         }
  58.  
  59.         for (int i = 0; i < targets.size(); i++) {
  60.             System.out.print(targets.get(i));
  61.             if (i != targets.size() - 1) {
  62.                 System.out.print("|");
  63.             }
  64.         }
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement