Advertisement
Kisyov

ArcheryChampionFromFundamentals

Mar 28th, 2023 (edited)
677
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.80 KB | None | 0 0
  1. package JavaAdvancedExam23October2021;
  2.  
  3. import java.util.*;
  4.  
  5. public class ArcheryChampionFromFundamentals {
  6.     public static void main(String[] args) {
  7.         Scanner scan = new Scanner(System.in);
  8.  
  9.         int[] archeryField = Arrays.stream(scan.nextLine().split("\\|"))
  10.                 .mapToInt(Integer::parseInt)
  11.                 .toArray();
  12.  
  13.         int collectedTournamentPoints = 0;
  14.         String userInput = scan.nextLine();
  15.  
  16.         while (!userInput.equals("Game over")) {
  17.  
  18.             String command = commandExtractor(userInput);
  19.             switch (command) {
  20.                 case "Shoot":
  21.                     collectedTournamentPoints += executeShootCommand(userInput, archeryField);
  22.                     break;
  23.                 case "Reverse":
  24.                     executeReverseCommand(archeryField);
  25.                     break;
  26.             }
  27.  
  28.             userInput = scan.nextLine();
  29.         }
  30.         /*
  31.          * Обърни внимание, че когато подготяме някакъв String, в него НЕ поставямв нови редове
  32.          * те са си проблем на тоя който вика нашия метод. Ние само осигуряваме String, a caller-а
  33.          * да си го печата/праща/сортира/конкатинира или прави каквото реши за уместно.
  34.          * Ние даваме само plain Sting.
  35.          * */
  36.         System.out.println(convertArcheryFieldToString(archeryField));
  37.         System.out.println("John finished the archery tournament with " + collectedTournamentPoints + " points!");
  38.  
  39.     }
  40.  
  41.     private static String convertArcheryFieldToString(int[] archeryField) {
  42.         StringBuilder sb = new StringBuilder();
  43.         for (int i = 0; i < archeryField.length; i++) {
  44.             sb.append(archeryField[i]);
  45.             if (i + 1 < archeryField.length) {
  46.                 sb.append(" - ");
  47.             }
  48.         }
  49.         return sb.toString();
  50.     }
  51.  
  52.     private static int executeShootCommand(String userInput, int[] archeryField) {
  53.         String[] splitCommand = userInput.split("\\s+");
  54.         String commandInstructions = splitCommand[1]; //commandInstructions now looks like: "Left@0@2" or "Right@6@5"
  55.         String[] tokenizedCommand = commandInstructions.split("@");
  56.         String walkDirections = tokenizedCommand[0];
  57.         int startWalkingAtPosition = Integer.parseInt(tokenizedCommand[1]);
  58.         int stepsToTake = Integer.parseInt(tokenizedCommand[2]);
  59.         int reslut = 0;
  60.  
  61.         if (!(startWalkingAtPosition >= 0 && startWalkingAtPosition < archeryField.length)) {
  62.             //ignore the command and return 0 as we gave promise to the caller that will return an int value
  63.             return 0;
  64.         }
  65.  
  66.         /*
  67.         the startWalkingAtPosition is valid and is inside the archeryField
  68.         We need to do the walk and determine at witch target we need to pint our bow
  69.         and do the rest of the instructions as per task
  70.         */
  71.  
  72.         int targetIndex = startWalkingAtPosition;
  73.         /*
  74.          * The following line of code is not a must but will reduce extra spins in loops
  75.          * caused by the times that you might be forced to walk the archeryField and
  76.          * practically doing nothing but just counting steps while walking. Since we know
  77.          * the exact length of the archeryField and thanks to the Maths magic of Modulo we can tell how many
  78.          * full circles you will have to walk in archeryField and prevent you those extra "k" full spins;
  79.          *
  80.          * */
  81.  
  82.         stepsToTake = stepsToTake % archeryField.length; //optimize the code a bit you can ignore it if you want.
  83.         if (walkDirections.equals("Left")) {
  84.             for (int walkedSteps = 0; walkedSteps < stepsToTake; walkedSteps++) {
  85.                 if (targetIndex == 0) {
  86.                     targetIndex = archeryField.length - 1;
  87.                     continue;
  88.                 }
  89.                 targetIndex--; //Заради факта, че трябва да се местим наляво.
  90.             }
  91.         } else {
  92.             for (int walkedSteps = 0; walkedSteps < stepsToTake; walkedSteps++) {
  93.                 if (targetIndex == archeryField.length - 1) {
  94.                     targetIndex = 0;
  95.                     continue;
  96.                 }
  97.                 targetIndex++; //Защото се движим "надясно" и това изисквам индекса да расте с по една на всяка крачка
  98.             }
  99.         }
  100.         /*
  101.          * Next we gonna do is to extract and target's health status and see how much blood it had :)
  102.          * Based on the result we will update the targets healthLevel and will assign tournamentPoints
  103.          * to our archer. Keep in mind that the max pints the archer can get is +5 per hit. And special
  104.          * hit gain condition applies in case the target's HP is less than 5 points;
  105.          * */
  106.  
  107.         int targetHealthLevel = archeryField[targetIndex];
  108.         if (targetHealthLevel >= 5) {
  109.             archeryField[targetIndex] = targetHealthLevel - 5;
  110.             reslut = 5;
  111.         } else {
  112.             archeryField[targetIndex] = 0;
  113.             reslut = targetHealthLevel;
  114.         }
  115.         return reslut;
  116.     }
  117.  
  118.     private static void executeReverseCommand(int[] archeryField) {
  119.         for (int i = 0; i < archeryField.length / 2; i++) {
  120.             int tempValue = archeryField[archeryField.length - 1 - i];
  121.             archeryField[archeryField.length - 1 - i] = archeryField[i];
  122.             archeryField[i] = tempValue;
  123.         }
  124.     }
  125.  
  126.     private static String commandExtractor(String userInput) {
  127.         String[] command = userInput.split("\\s+");
  128.         return command[0];
  129.     }
  130.  
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement