Advertisement
Joreto

123

Oct 21st, 2023
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.21 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 == -1) {
  56.                 newRow = n - 1;
  57.             } else if (newRow == n) {
  58.                 newRow = 0;
  59.             }
  60.  
  61.             if (newCol == -1) {
  62.                 newCol = n - 1;
  63.             } else if (newCol == n) {
  64.                 newCol = 0;
  65.             }
  66.  
  67.             if (fishingArea[newRow][newCol] == 'W') {
  68.                 System.out.println("You fell into a whirlpool! The ship sank and you lost the fish you caught. " +
  69.                         "Last coordinates of the ship: [" + newRow + "," + newCol + "]");
  70.                 return;
  71.             }
  72.  
  73.             if (Character.isDigit(fishingArea[newRow][newCol])) {
  74.                 int fishValue = Character.getNumericValue(fishingArea[newRow][newCol]);
  75.                 fishCaught += fishValue;
  76.                 fishingArea[newRow][newCol] = '-';
  77.             }
  78.  
  79.             playerRow = newRow;
  80.             playerCol = newCol;
  81.  
  82.             quotaReached = fishCaught >= quota;
  83.  
  84.             if (quotaReached) {
  85.                 System.out.println("Success! You managed to reach the quota!");
  86.                 System.out.println("Amount of fish caught: " + fishCaught + " tons.");
  87.                 return;
  88.             }
  89.  
  90.         }
  91.  
  92.         if (!quotaReached) {
  93.             int lackOfFish = quota - fishCaught;
  94.             System.out.println("You didn't catch enough fish and didn't reach the quota!\n" +
  95.                     "You need " + lackOfFish + " tons of fish more.");
  96.         }
  97.         System.out.println("Amount of fish caught: " + fishCaught + " tons.");
  98.     }
  99. }
  100.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement