Advertisement
nbzhk

P10TreasureHunt

Mar 4th, 2023
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.41 KB | None | 0 0
  1. package Arrays.Exercise;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class P10TreasureHunt {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.  
  9.         String[] lootArr = scanner.nextLine().split("\\|");
  10.         String command = scanner.nextLine();
  11.  
  12.         while (!command.equals("Yohoho!")) {
  13.             String[] commandArr = command.split(" ");
  14.             if (command.contains("Loot")) {
  15.                 for (int i = 1; i < commandArr.length; i++) {
  16.                     boolean alreadyContained = false;
  17.                     for (int j = 0; j < lootArr.length; j++) {
  18.                         if (commandArr[i].equals(lootArr[j])) {
  19.                             alreadyContained = true;
  20.                             break;
  21.                         }
  22.                     }
  23.                     if (!alreadyContained) {
  24.                         String newItem = commandArr[i] + " " + String.join(" ", lootArr);
  25.                         lootArr = newItem.split(" ");
  26.                     }
  27.                 }
  28.  
  29.             } else if (command.contains("Drop")) {
  30.  
  31.                 int index = Integer.parseInt(commandArr[1]);
  32.                 if (index <= lootArr.length - 1 && index >= 0) {
  33.                     String droppedItem = lootArr[index];
  34.                     for (int j = index; j < lootArr.length - 1; j++) {
  35.                         lootArr[j] = lootArr[j + 1];
  36.                     }
  37.                     lootArr[lootArr.length - 1] = droppedItem;
  38.                 } else {
  39.                     command = scanner.nextLine();
  40.                     continue;
  41.                 }
  42.  
  43.             } else if (command.contains("Steal")) {
  44.                 int index = Integer.parseInt(commandArr[1]);
  45.                 if (index > lootArr.length) {
  46.                     index = lootArr.length;
  47.                 }
  48.                 String[] stealArr = new String[0];
  49.                 for (int i = lootArr.length - 1; i >= lootArr.length - index; i--) {
  50.                     String stolenItem = lootArr[i] + " " + String.join(" ", stealArr);
  51.                     stealArr = stolenItem.split(" ");
  52.                 }
  53.                 for (int j = 0; j < stealArr.length; j++) {
  54.                     String currentItem = stealArr[j];
  55.                     if (j != stealArr.length - 1) {
  56.                         System.out.print(currentItem + ", ");
  57.                     } else {
  58.                         System.out.print(currentItem);
  59.                     }
  60.                 }
  61.                 String [] newChest = new String[lootArr.length - stealArr.length];
  62.                 for (int i = 0; i < newChest.length; i++) {
  63.                     String leftItem = lootArr[i];
  64.                     newChest[i] = leftItem;
  65.  
  66.                 }
  67.                 lootArr = newChest;
  68.             }
  69.             command = scanner.nextLine();
  70.         }
  71.         int totalLength = 0;
  72.         int itemsCount = lootArr.length;
  73.  
  74.         if (itemsCount == 0) {
  75.             System.out.printf("%nFailed treasure hunt.");
  76.         } else {
  77.             for (int i = 0; i < lootArr.length; i++) {
  78.                 String currentItem = lootArr[i];
  79.                 int itemLength = currentItem.length();
  80.                 totalLength+=itemLength;
  81.             }
  82.  
  83.             double averageHunt = (totalLength * 1.0) / itemsCount;
  84.             System.out.printf("%nAverage treasure gain: %.2f pirate credits.", averageHunt);
  85.         }
  86.     }
  87. }
  88.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement