Advertisement
Guest User

03. Moving Target

a guest
Jul 3rd, 2020
405
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.58 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.Scanner;
  4.  
  5. public class MovingTarget_3 {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.  
  9.         String printTargets = scanner.nextLine();
  10.         String[] partsTargets = printTargets.split("\\s+");
  11.  
  12.         List<Integer> targets = new ArrayList<>();
  13.         for (String currentSection : partsTargets) {
  14.             int sectionInt = Integer.parseInt(currentSection);
  15.             targets.add(sectionInt);
  16.         }
  17.  
  18.         String input = scanner.nextLine();
  19.         while (!"End".equals(input)) {
  20.             String[] command = input.split("\\s+");
  21.             String argument = command[0];
  22.             int index = Integer.parseInt(command[1]);
  23.  
  24.             switch (argument) {
  25.                 case "Shoot":
  26.                     int power = Integer.parseInt(command[2]);
  27.                     if (isValid(targets, index)) {
  28.                         int shootSection = targets.get(index);
  29.                         shootSection -= power;
  30.                         if (shootSection <= 0) {
  31.                             targets.remove(index);
  32.                         } else {
  33.                             targets.set(index, shootSection);
  34.                         }
  35.                     }
  36.                     break;
  37.                 case "Add":
  38.                     int value = Integer.parseInt(command[2]);
  39.                     if (isValid(targets, index)) {
  40.                         targets.add(index, value);
  41.                     } else {
  42.                         System.out.println("Invalid placement!");
  43.                     }
  44.                     break;
  45.                 case "Strike":
  46.                     int radius = Integer.parseInt(command[2]);
  47.                     int leftRadius = Math.max(index - radius, -1);
  48.                     int rightRadius = Math.min(index + leftRadius, targets.size() - 1);
  49.                     if (leftRadius >= 0 && rightRadius < targets.size() - 1) {
  50.                         targets.remove(index);
  51.                         targets.subList(leftRadius, rightRadius - leftRadius + 1).clear();
  52.                     } else {
  53.                         System.out.println("Strike missed!");
  54.                     }
  55.                     break;
  56.             }
  57.             input = scanner.nextLine();
  58.         }
  59.         System.out.println(targets.toString().replaceAll("[\\[\\],]", "")
  60.                 .replaceAll(" ", "|"));
  61.     }
  62.  
  63.     private static boolean isValid(List<Integer> targets, Integer index) {
  64.         return index >= 0 && index < targets.size();
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement