Advertisement
Guest User

Untitled

a guest
Dec 10th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.86 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Collections;
  3. import java.util.List;
  4. import java.util.Scanner;
  5. import java.util.stream.Collectors;
  6.  
  7. public class Archery {
  8. public static void main(String[] args) {
  9. Scanner scanner = new Scanner(System.in);
  10. List<Integer> list = Arrays.stream(scanner.nextLine().split("\\|")).map(Integer::parseInt).collect(Collectors.toList());
  11. String[] commands = scanner.nextLine().split(" ");
  12. int collectedPoints = 0;
  13. int currentIndex = 0;
  14. int currentValue = 0;//10 10 10 5 10
  15.  
  16. while (!(commands[0].equals("Game") && commands[1].equals("over"))) {
  17.  
  18. if (commands[0].equals("Shoot")) {
  19. String[] shotParameters = commands[1].split("@");
  20. if (shotParameters[0].equals("Right")) {
  21. int startIndex = Integer.parseInt(shotParameters[1]);
  22. if (isValid(list, startIndex)) {
  23.  
  24. int length = Integer.parseInt(shotParameters[2]);
  25. currentIndex = startIndex + length;
  26.  
  27.  
  28. while (!isValid(list, currentIndex)) { // start for
  29. currentIndex = currentIndex - list.size();
  30. }
  31. currentValue = list.get(currentIndex);
  32. if (currentValue == 0) {
  33. //do nothing
  34. }
  35. if (currentValue <= 5) {
  36.  
  37. collectedPoints += currentValue;
  38. currentValue = 0;
  39. list.set(currentIndex, currentValue);
  40. } else {
  41. currentValue = currentValue - 5;
  42. list.set(currentIndex, currentValue);
  43. collectedPoints += 5;
  44. }
  45.  
  46.  
  47. }
  48.  
  49. } else if (shotParameters[0].equals("Left")) {
  50. int startIndex = Integer.parseInt(shotParameters[1]);
  51. if (isValid(list, startIndex)) {
  52. int length = Integer.parseInt(shotParameters[2]);
  53. currentIndex = startIndex - length;
  54.  
  55. while (!isValid(list, currentIndex)) { // start for
  56. currentIndex = currentIndex + list.size();
  57. }
  58. currentValue = list.get(currentIndex);
  59. if (currentValue == 0) {
  60. //do nothing;
  61. } else if (currentValue <= 5) {
  62. collectedPoints += currentValue;
  63. currentValue = 0;
  64. list.set(currentIndex, currentValue);
  65. } else {
  66. currentValue = currentValue - 5;
  67. list.set(currentIndex, currentValue);
  68. collectedPoints += 5;
  69. }
  70.  
  71. }
  72. } //end for
  73.  
  74.  
  75. } else if (commands[0].equals("Reverse")) {
  76. Collections.reverse(list);
  77.  
  78. }
  79.  
  80. commands = scanner.nextLine().split(" ");
  81.  
  82. }
  83.  
  84. for (int i = 0; i < list.size(); i++) {
  85. if (i == list.size() - 1) {
  86. System.out.print(list.get(i));
  87. } else {
  88. System.out.print(list.get(i) + " - ");
  89. }
  90.  
  91. }
  92. System.out.println();
  93. System.out.printf("Iskren finished the archery tournament with %d points!", collectedPoints);
  94.  
  95.  
  96. }
  97.  
  98. static private boolean isValid(List<Integer> list, int index) {
  99. return index >= 0 && index < list.size();
  100.  
  101. }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement