Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Scanner;
- public class TwoArcheryNEW {
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- String[] input = scan.nextLine().split("\\|");
- ArrayList<Integer> targets = new ArrayList<>();
- for (int i = 0; i < input.length; i++) {
- int targetToAdd = Integer.parseInt(input[i]);
- targets.add(targetToAdd);
- }
- int pointsWon = 0;
- String command = scan.nextLine();
- while (!command.equals("Game over")) {
- String[] tokens = command.split(" ");
- switch (tokens[0]) {
- case "Shoot":
- String[] shotMade = tokens[1].split("\\@");
- String direction = shotMade[0];
- int startIndex = Integer.parseInt(shotMade[1]);
- //prowerka za walidnost na podadeniq index
- if (startIndex> (targets.size()-1)){
- break;
- }
- int length = Integer.parseInt(shotMade[2]);
- //sledwashiq red e za namirane na indexa, w koito se "celim"
- int targetToShoot = findTarget(targets, direction, startIndex, length);
- //smqtane na spechelenite tochki i modificirane na targets
- if (targets.get(targetToShoot) <= 5) {
- pointsWon += targets.get(targetToShoot);
- targets.set(targetToShoot, 0);
- } else {
- pointsWon += 5;
- int newTargetPoints = targets.get(targetToShoot) - 5;
- targets.set(targetToShoot, newTargetPoints);
- }
- break;
- case "Reverse":
- Collections.reverse(targets);
- break;
- }
- command = scan.nextLine();
- }
- System.out.print(targets.get(0));
- for (int i = 1; i < targets.size(); i++) {
- System.out.print(" - " + targets.get(i));
- }
- System.out.println();
- System.out.printf("Iskren finished the archery tournament with %d points!", pointsWon);
- }
- private static int findTarget(ArrayList<Integer> targets, String direction, int startIndex, int length) {
- int indexToShoot = startIndex;
- while (length>=targets.size()){
- length = length - targets.size();
- }
- if (direction.equals("Left")) {
- while (!(indexToShoot == 0)) {
- indexToShoot--;
- length--;
- }
- if (length > 0) {
- indexToShoot = targets.size()-1;
- length--;
- }
- while (!(length == 0)) {
- indexToShoot--;
- length--;
- }
- } else if (direction.equals("Right")) {
- while(!(indexToShoot==(targets.size()-1))){
- indexToShoot++;
- length--;
- }
- if (length>0){
- indexToShoot=0;
- length--;
- }
- while (!(length==0)){
- indexToShoot++;
- length--;
- }
- }
- return indexToShoot;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement