Advertisement
Guest User

Untitled

a guest
Feb 28th, 2020
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.35 KB | None | 0 0
  1.  
  2. import java.util.ArrayList;
  3. import java.util.Collections;
  4. import java.util.Scanner;
  5.  
  6. public class TwoArcheryNEW {
  7.     public static void main(String[] args) {
  8.         Scanner scan = new Scanner(System.in);
  9.         String[] input = scan.nextLine().split("\\|");
  10.         ArrayList<Integer> targets = new ArrayList<>();
  11.         for (int i = 0; i < input.length; i++) {
  12.             int targetToAdd = Integer.parseInt(input[i]);
  13.             targets.add(targetToAdd);
  14.         }
  15.  
  16.         int pointsWon = 0;
  17.  
  18.         String command = scan.nextLine();
  19.         while (!command.equals("Game over")) {
  20.             String[] tokens = command.split(" ");
  21.             switch (tokens[0]) {
  22.                 case "Shoot":
  23.                     String[] shotMade = tokens[1].split("\\@");
  24.                     String direction = shotMade[0];
  25.                     int startIndex = Integer.parseInt(shotMade[1]);
  26.                     //prowerka za walidnost na podadeniq index
  27.                     if (startIndex> (targets.size()-1)){
  28.                         break;
  29.                 }
  30.                     int length = Integer.parseInt(shotMade[2]);
  31.                     //sledwashiq red e za namirane na indexa, w koito se "celim"
  32.                     int targetToShoot = findTarget(targets, direction, startIndex, length);
  33.                     //smqtane na spechelenite tochki i modificirane na targets
  34.                     if (targets.get(targetToShoot) <= 5) {
  35.                         pointsWon += targets.get(targetToShoot);
  36.                         targets.set(targetToShoot, 0);
  37.                     } else {
  38.                         pointsWon += 5;
  39.                         int newTargetPoints = targets.get(targetToShoot) - 5;
  40.                         targets.set(targetToShoot, newTargetPoints);
  41.                     }
  42.                     break;
  43.                 case "Reverse":
  44.                     Collections.reverse(targets);
  45.                     break;
  46.  
  47.             }
  48.             command = scan.nextLine();
  49.         }
  50.         System.out.print(targets.get(0));
  51.         for (int i = 1; i < targets.size(); i++) {
  52.             System.out.print(" - " + targets.get(i));
  53.         }
  54.         System.out.println();
  55.         System.out.printf("Iskren finished the archery tournament with %d points!", pointsWon);
  56.  
  57.     }
  58.  
  59.     private static int findTarget(ArrayList<Integer> targets, String direction, int startIndex, int length) {
  60.         int indexToShoot = startIndex;
  61.         while (length>=targets.size()){
  62.             length = length - targets.size();
  63.         }
  64.         if (direction.equals("Left")) {
  65.             while (!(indexToShoot == 0)) {
  66.                 indexToShoot--;
  67.                 length--;
  68.             }
  69.             if (length > 0) {
  70.                 indexToShoot = targets.size()-1;
  71.                 length--;
  72.             }
  73.             while (!(length == 0)) {
  74.                 indexToShoot--;
  75.                 length--;
  76.             }
  77.  
  78.         } else if (direction.equals("Right")) {
  79.             while(!(indexToShoot==(targets.size()-1))){
  80.                 indexToShoot++;
  81.                 length--;
  82.             }
  83.             if (length>0){
  84.                 indexToShoot=0;
  85.                 length--;
  86.             }
  87.             while (!(length==0)){
  88.                 indexToShoot++;
  89.                 length--;
  90.             }
  91.         }
  92.         return indexToShoot;
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement