borovaneca

FishingCompetition

Oct 21st, 2023
744
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.48 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4.  
  5. public class FishingCompetition {
  6.     private static final char FISHER = 'S';
  7.     private static final int DISCARD = 5;
  8.     private static int CATCH = 0;
  9.     private static final char WHIRLPOOL = 'W';
  10.     private static final char SEA_STORM = '@';
  11.     private static final char EMPTY_POSITION = '-';
  12.     private static boolean FALL_INTO_STORM = false;
  13.     private static boolean FALL_INTO_WHIRLPOOL = false;
  14.     private static boolean SUCCESS = false;
  15.     private static final String FINAL = "collect the nets";
  16.     private static final String UP = "up";
  17.     private static final String DOWN = "down";
  18.     private static final String LEFT = "left";
  19.     private static final String RIGHT = "right";
  20.     private static final String FALL_WHIRLPOOL = "You fell into a whirlpool! The ship sank and you lost the fish you caught. Last coordinates of the ship: [%s,%s]\n";
  21.     private static final String FALL_STORM = "You fell into a sea storm! The ship will be moved to the beginning of the fishing area.\n";
  22.     private static final String SUCCEEDED = "Success! You managed to reach the quota!";
  23.     private static final String NOT_SUCCEEDED = "You didn't catch enough fish and didn't reach the quota! You need %d tons of fish more.\n";
  24.     private static final String AMOUNT_OUTPUT = "Amount of fish caught: %d tons.\n";
  25.  
  26.     public FishingCompetition() {
  27.     }
  28.  
  29.     public static void main(String[] args) throws IOException {
  30.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  31.         int rows = Integer.parseInt(reader.readLine());
  32.         char[][] fishingArea = getSea(rows, reader);
  33.         int[] currentPosition = getCurrentPosition(fishingArea);
  34.         boolean storm = true;
  35.  
  36.         String command;
  37.         while(!"collect the nets".equals(command = reader.readLine()) && storm) {
  38.             fishingArea[currentPosition[0]][currentPosition[1]] = '-';
  39.             int var10002;
  40.             switch (command) {
  41.                 case "up":
  42.                     if (currentPosition[0] - 1 < 0) {
  43.                         currentPosition[0] = fishingArea.length - 1;
  44.                     } else {
  45.                         var10002 = currentPosition[0]--;
  46.                     }
  47.  
  48.                     storm = moveFisher(currentPosition, fishingArea);
  49.                     break;
  50.                 case "down":
  51.                     if (currentPosition[0] + 1 >= fishingArea.length) {
  52.                         currentPosition[0] = 0;
  53.                     } else {
  54.                         var10002 = currentPosition[0]++;
  55.                     }
  56.  
  57.                     storm = moveFisher(currentPosition, fishingArea);
  58.                     break;
  59.                 case "left":
  60.                     if (currentPosition[1] - 1 < 0) {
  61.                         currentPosition[1] = fishingArea[currentPosition[0]].length - 1;
  62.                     } else {
  63.                         var10002 = currentPosition[1]--;
  64.                     }
  65.  
  66.                     storm = moveFisher(currentPosition, fishingArea);
  67.                     break;
  68.                 case "right":
  69.                     if (currentPosition[1] + 1 >= fishingArea[currentPosition[0]].length) {
  70.                         currentPosition[1] = 0;
  71.                     } else {
  72.                         var10002 = currentPosition[1]++;
  73.                     }
  74.  
  75.                     storm = moveFisher(currentPosition, fishingArea);
  76.             }
  77.         }
  78.  
  79.         if (!FALL_INTO_WHIRLPOOL) {
  80.             if (SUCCESS) {
  81.                 System.out.println("Success! You managed to reach the quota!");
  82.             } else {
  83.                 System.out.printf("You didn't catch enough fish and didn't reach the quota! You need %d tons of fish more.\n", 20 - CATCH);
  84.             }
  85.  
  86.             if (CATCH > 0) {
  87.                 System.out.printf("Amount of fish caught: %d tons.\n", CATCH);
  88.             }
  89.  
  90.             if (!FALL_INTO_WHIRLPOOL) {
  91.                 printFishArea(fishingArea);
  92.             }
  93.         }
  94.  
  95.     }
  96.  
  97.     private static void printFishArea(char[][] fishingArea) {
  98.         char[][] var1 = fishingArea;
  99.         int var2 = fishingArea.length;
  100.  
  101.         for(int var3 = 0; var3 < var2; ++var3) {
  102.             char[] row = var1[var3];
  103.             char[] var5 = row;
  104.             int var6 = row.length;
  105.  
  106.             for(int var7 = 0; var7 < var6; ++var7) {
  107.                 char col = var5[var7];
  108.                 System.out.print(col);
  109.             }
  110.  
  111.             System.out.println();
  112.         }
  113.  
  114.     }
  115.  
  116.     private static boolean moveFisher(int[] currentPosition, char[][] fishingArea) {
  117.         if (Character.isDigit(fishingArea[currentPosition[0]][currentPosition[1]])) {
  118.             CATCH += Integer.parseInt(String.valueOf(fishingArea[currentPosition[0]][currentPosition[1]]));
  119.             SUCCESS = CATCH >= 20;
  120.         } else {
  121.             if (fishingArea[currentPosition[0]][currentPosition[1]] == 'W') {
  122.                 FALL_INTO_WHIRLPOOL = true;
  123.                 System.out.printf("You fell into a whirlpool! The ship sank and you lost the fish you caught. Last coordinates of the ship: [%s,%s]\n", currentPosition[0], currentPosition[1]);
  124.                 return false;
  125.             }
  126.  
  127.             if (fishingArea[currentPosition[0]][currentPosition[1]] == '@') {
  128.                 System.out.print("You fell into a sea storm! The ship will be moved to the beginning of the fishing area.\n");
  129.                 fishingArea[currentPosition[0]][currentPosition[1]] = '-';
  130.                 currentPosition[0] = 0;
  131.                 currentPosition[1] = 0;
  132.                 CATCH = Math.max(0, CATCH - 5);
  133.                 SUCCESS = CATCH >= 20;
  134.             }
  135.         }
  136.  
  137.         fishingArea[currentPosition[0]][currentPosition[1]] = 'S';
  138.         return true;
  139.     }
  140.  
  141.     private static int[] getCurrentPosition(char[][] sea) {
  142.         int[] coordinates = new int[2];
  143.  
  144.         for(int r = 0; r < sea.length; ++r) {
  145.             for(int col = 0; col < sea[r].length; ++col) {
  146.                 if (sea[r][col] == 'S') {
  147.                     return new int[]{r, col};
  148.                 }
  149.             }
  150.         }
  151.  
  152.         return coordinates;
  153.     }
  154.  
  155.     private static char[][] getSea(int rows, BufferedReader reader) throws IOException {
  156.         char[][] toReturn = new char[rows][];
  157.  
  158.         for(int i = 0; i < toReturn.length; ++i) {
  159.             toReturn[i] = reader.readLine().toCharArray();
  160.         }
  161.  
  162.         return toReturn;
  163.     }
  164. }
Advertisement
Add Comment
Please, Sign In to add comment