Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.util.Collections;
- import java.util.List;
- import java.util.Scanner;
- import java.util.stream.Collectors;
- public class Archery {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- List<Integer> list = Arrays.stream(scanner.nextLine().split("\\|")).map(Integer::parseInt).collect(Collectors.toList());
- String[] commands = scanner.nextLine().split(" ");
- int collectedPoints = 0;
- int currentIndex = 0;
- int currentValue = 0;//10 10 10 5 10
- while (!(commands[0].equals("Game") && commands[1].equals("over"))) {
- if (commands[0].equals("Shoot")) {
- String[] shotParameters = commands[1].split("@");
- if (shotParameters[0].equals("Right")) {
- int startIndex = Integer.parseInt(shotParameters[1]);
- if (isValid(list, startIndex)) {
- int length = Integer.parseInt(shotParameters[2]);
- currentIndex = startIndex + length;
- while (!isValid(list, currentIndex)) { // start for
- currentIndex = currentIndex - list.size();
- }
- currentValue = list.get(currentIndex);
- if (currentValue == 0) {
- //do nothing
- }
- if (currentValue <= 5) {
- collectedPoints += currentValue;
- currentValue = 0;
- list.set(currentIndex, currentValue);
- } else {
- currentValue = currentValue - 5;
- list.set(currentIndex, currentValue);
- collectedPoints += 5;
- }
- }
- } else if (shotParameters[0].equals("Left")) {
- int startIndex = Integer.parseInt(shotParameters[1]);
- if (isValid(list, startIndex)) {
- int length = Integer.parseInt(shotParameters[2]);
- currentIndex = startIndex - length;
- while (!isValid(list, currentIndex)) { // start for
- currentIndex = currentIndex + list.size();
- }
- currentValue = list.get(currentIndex);
- if (currentValue == 0) {
- //do nothing;
- } else if (currentValue <= 5) {
- collectedPoints += currentValue;
- currentValue = 0;
- list.set(currentIndex, currentValue);
- } else {
- currentValue = currentValue - 5;
- list.set(currentIndex, currentValue);
- collectedPoints += 5;
- }
- }
- } //end for
- } else if (commands[0].equals("Reverse")) {
- Collections.reverse(list);
- }
- commands = scanner.nextLine().split(" ");
- }
- for (int i = 0; i < list.size(); i++) {
- if (i == list.size() - 1) {
- System.out.print(list.get(i));
- } else {
- System.out.print(list.get(i) + " - ");
- }
- }
- System.out.println();
- System.out.printf("Iskren finished the archery tournament with %d points!", collectedPoints);
- }
- static private boolean isValid(List<Integer> list, int index) {
- return index >= 0 && index < list.size();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement