plamenavylcheva

MovingTarget

Jun 14th, 2023
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1. package ThirdTaskLists;
  2.  
  3. import java.util.Arrays;
  4. import java.util.List;
  5. import java.util.Scanner;
  6. import java.util.stream.Collectors;
  7.  
  8. public class MovingTarget {
  9. public static void main(String[] args) {
  10. Scanner scanner = new Scanner(System.in);
  11. List<Integer> targets = Arrays.stream(scanner.nextLine().split("\\s+"))
  12. .map(Integer::parseInt)
  13. .collect(Collectors.toList());
  14. String command = scanner.nextLine();
  15.  
  16. while (!command.equals("End")) {
  17.  
  18. if (command.contains("Shoot")) {
  19. int shootIndex = Integer.parseInt(command.split("\\s+")[1]);
  20. int shootPower = Integer.parseInt(command.split("\\s+")[2]);
  21.  
  22. if (shootPower >= 0 && shootIndex < targets.size()) { //->има ли index
  23. int newValue = targets.get(shootIndex);
  24. newValue = newValue - shootPower;
  25. if (newValue <= 0) {
  26. targets.remove(shootIndex);
  27. } else {
  28. targets.set(shootIndex, newValue);
  29. }
  30. }
  31. } else if (command.contains("Add")) {
  32. int addIndex = Integer.parseInt(command.split("\\s+")[1]);
  33. int addValue = Integer.parseInt(command.split("\\s+")[2]);
  34. if (addIndex >= 0 && addIndex < targets.size()) {
  35. targets.add(addIndex, addValue);
  36. } else {
  37. System.out.println("Invalid placement!");
  38. }
  39. } else if (command.contains("Strike")) {
  40. int strikeIndex = Integer.parseInt(command.split("\\s+")[1]);
  41. int radius = Integer.parseInt(command.split("\\s+")[2]);
  42. if (strikeIndex < 0
  43. || strikeIndex >= targets.size()
  44. || radius == 0
  45. || radius > strikeIndex
  46. || strikeIndex + radius >= targets.size()) {
  47. System.out.println("Strike missed!");
  48.  
  49. } else {
  50. targets.removeAll(targets.subList(strikeIndex - radius, strikeIndex + radius + 1));
  51.  
  52. }
  53. }
  54. command = scanner.nextLine();
  55. }
  56.  
  57. for (int i = 0; i < targets.size() - 1; i++) {
  58. System.out.print(targets.get(i) + "|");
  59. }
  60. System.out.print(targets.get(targets.size() - 1));
  61. }
  62. }
  63.  
Tags: MovingTarget
Add Comment
Please, Sign In to add comment