Joreto

P02FishingCompetition

Oct 21st, 2023
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.11 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class P02FishingCompetition {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.  
  7.         int n = Integer.parseInt(scanner.nextLine());
  8.         char[][] fishingArea = new char[n][n];
  9.  
  10.         int fishCaught = 0;
  11.         int playerRow = -1;
  12.         int playerCol = -1;
  13.  
  14.         for (int i = 0; i < n; i++) {
  15.             String row = scanner.nextLine();
  16.             fishingArea[i] = row.toCharArray();
  17.             if (row.contains("S")) {
  18.                 playerRow = i;
  19.                 playerCol = row.indexOf("S");
  20.             }
  21.             if (row.contains("F")) {
  22.                 fishCaught += row.chars().filter(ch -> ch == 'F').count();
  23.             }
  24.         }
  25.  
  26.         int quota = 20;
  27.         boolean quotaReached = fishCaught >= quota;
  28.  
  29.         while (true) {
  30.             String command = scanner.nextLine();
  31.  
  32.             if (command.equals("collect the nets")) {
  33.                 break;
  34.             }
  35.  
  36.             int newRow = playerRow;
  37.             int newCol = playerCol;
  38.  
  39.             switch (command) {
  40.                 case "up":
  41.                     newRow = (newRow - 1 + n) % n;
  42.                     break;
  43.                 case "down":
  44.                     newRow = (newRow + 1) % n;
  45.                     break;
  46.                 case "left":
  47.                     newCol = (newCol - 1 + n) % n;
  48.                     break;
  49.                 case "right":
  50.                     newCol = (newCol + 1) % n;
  51.                     break;
  52.             }
  53.  
  54.  
  55.             if (newRow < 0 || newRow >= n || newCol < 0 || newCol >= n) {
  56.                 newRow = Math.abs(newRow - n) % n;
  57.                 newCol = Math.abs(newCol - n) % n;
  58.             }
  59.  
  60.             if (fishingArea[newRow][newCol] == 'W') {
  61.                 System.out.println("You fell into a whirlpool! The ship sank and you lost the fish you caught. " +
  62.                         "Last coordinates of the ship: [" + newRow + "," + newCol + "]");
  63.                 return;
  64.             }
  65.  
  66.             if (Character.isDigit(fishingArea[newRow][newCol])) {
  67.                 int fishValue = Character.getNumericValue(fishingArea[newRow][newCol]);
  68.                 fishCaught += fishValue;
  69.                 fishingArea[newRow][newCol] = '-';
  70.             }
  71.  
  72.             playerRow = newRow;
  73.             playerCol = newCol;
  74.  
  75.             quotaReached = fishCaught >= quota;
  76.  
  77.             if (quotaReached) {
  78.                 System.out.println("Success! You managed to reach the quota!");
  79.                 System.out.println("Amount of fish caught: " + fishCaught + " tons.");
  80.                 break;
  81.             }
  82.         }
  83.  
  84.         if (!quotaReached) {
  85.             int lackOfFish = quota - fishCaught;
  86.             System.out.println("You didn't catch enough fish and didn't reach the quota!\n" +
  87.                     "You need " + lackOfFish + " tons of fish more.");
  88.         }
  89.  
  90.  
  91.         for (int i = 0; i < n; i++) {
  92.             for (int j = 0; j < n; j++) {
  93.                 System.out.print(fishingArea[i][j]);
  94.             }
  95.             System.out.println();
  96.         }
  97.     }
  98. }
  99.  
Advertisement
Add Comment
Please, Sign In to add comment