Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.43 KB | None | 0 0
  1. package E_2019_12_10_Mid;
  2.  
  3. import java.util.Arrays;
  4. import java.util.Scanner;
  5.  
  6. public class Task_02 {
  7.     public static void printArrOneLine(int[] arr) {
  8.         for (int i = 0; i < arr.length - 1; i++) {
  9.             System.out.print(String.format("%d - ", arr[i]));
  10.         }
  11.         if (arr.length > 0)
  12.             System.out.println(arr[arr.length - 1]);
  13.     }
  14.     public static int moveCellsArr (int start,int length,int size,String direction){
  15.         int endInd = 0;
  16.         if(direction.equals("Right"))
  17.             endInd = (start + length) % size;
  18.         else if (direction.equals("Left")){
  19.             endInd = start;
  20.             while (length > 0){
  21.                 endInd--;length--;
  22.                 if (endInd == - 1)
  23.                     endInd = size - 1;
  24.             }
  25.         }
  26.         return endInd;
  27.     }
  28.  
  29.  
  30.     public static void main(String[] args) {
  31.         Scanner console = new Scanner(System.in);
  32.         int[] arr = Arrays.stream(console.nextLine().split("\\|")).mapToInt(e -> Integer.parseInt(e)).toArray();
  33.         String command;
  34.         int result = 0;
  35.         while (!(command = console.nextLine()).equals("Game over")) {
  36.             var split = command.split(" |@");
  37.             String action = split[0];
  38.             if (action.equals("Shoot")) {
  39.                 String direction = split[1];
  40.                 int startInd = Integer.parseInt(split[2]);
  41.                 int length = Integer.parseInt(split[3]);
  42.  
  43.                 if (startInd >= 0 && startInd < arr.length) {
  44.                     int newPosition = moveCellsArr(startInd,length,arr.length,direction);
  45.                     if (arr[newPosition] >= 5) {
  46.                         arr[newPosition] -= 5;
  47.                         result += 5;
  48.                     } else if (arr[newPosition] > 0 && arr[newPosition] < 5) {
  49.                         result += arr[newPosition];
  50.                         arr[newPosition] = 0;
  51.                     }
  52.                 }
  53.             } else if (command.equals("Reverse")){
  54.                 int[] reversed = new int[arr.length];
  55.                 int ind = 0;
  56.                 for (int i = arr.length - 1; i >= 0; i--) {
  57.                     reversed[ind] = arr[i];
  58.                     ind++;
  59.                 }
  60.                 arr = reversed;
  61.             }
  62.         }
  63.         printArrOneLine(arr);
  64.         System.out.println(String.format("Iskren finished the archery tournament with %d points!", result));
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement