Advertisement
CuBG

Untitled

Jun 12th, 2023
752
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.19 KB | Software | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4.     public static void main(String[] args) {
  5.         Scanner sc = new Scanner(System.in);
  6.  
  7.         int fieldSize = Integer.parseInt(sc.nextLine());
  8.  
  9.         char[][] field = new char[fieldSize][fieldSize];
  10.  
  11.         int squirrelRow = -1;
  12.         int squirrelCol = -1;
  13.         int hazelnutCount = 0;
  14.         String[] commands = sc.nextLine().split(",\\s+");
  15.         for (int row = 0; row < field.length; row++) {
  16.             char[] lineRow = new char[fieldSize];
  17.             String line = sc.nextLine();
  18.             for (int col = 0; col < line.length(); col++) {
  19.                 lineRow[col] = line.charAt(col);
  20.  
  21.                 if (line.charAt(col) == 's') {
  22.                     squirrelRow = row;
  23.                     squirrelCol = col;
  24.                 }
  25.             }
  26.             field[row] = lineRow;
  27.         }
  28.  
  29.         for (String command : commands) {
  30.             switch (command) {
  31.                 case "left":
  32.                     squirrelCol--;
  33.  
  34.                     if (squirrelStepsOutsideTheField(squirrelRow, squirrelCol, fieldSize)) {
  35.                         System.out.println("The squirrel is out of the field.");
  36.                         System.out.println("Hazelnuts collected: " + hazelnutCount);
  37.                         return;
  38.                     }
  39.  
  40.                     if (field[squirrelRow][squirrelCol] == 't') {
  41.                         System.out.println("Unfortunately, the squirrel stepped on a trap...");
  42.                         System.out.println("Hazelnuts collected: " + hazelnutCount);
  43.                         return;
  44.                     }
  45.  
  46.                     if (field[squirrelRow][squirrelCol] == 'h') {
  47.                         hazelnutCount++;
  48.                         if (hazelnutCount == 3) {
  49.                             System.out.println("Good job! You have collected all hazelnuts!");
  50.                             System.out.println("Hazelnuts collected: " + hazelnutCount);
  51.                             return;
  52.                         } else {
  53.                             field[squirrelRow][squirrelCol] = '*';
  54.                         }
  55.                     }
  56.                     break;
  57.                 case "right":
  58.  
  59.                     squirrelCol++;
  60.                     if (squirrelStepsOutsideTheField(squirrelRow, squirrelCol, fieldSize)) {
  61.                         System.out.println("The squirrel is out of the field.");
  62.                         System.out.println("Hazelnuts collected: " + hazelnutCount);
  63.                         return;
  64.                     }
  65.  
  66.                     if (field[squirrelRow][squirrelCol] == 't') {
  67.                         System.out.println("Unfortunately, the squirrel stepped on a trap...");
  68.                         System.out.println("Hazelnuts collected: " + hazelnutCount);
  69.                         return;
  70.                     }
  71.  
  72.                     if (field[squirrelRow][squirrelCol] == 'h') {
  73.                         hazelnutCount++;
  74.                         if (hazelnutCount == 3) {
  75.                             System.out.println("Good job! You have collected all hazelnuts!");
  76.                             System.out.println("Hazelnuts collected: " + hazelnutCount);
  77.                             return;
  78.                         } else {
  79.                             field[squirrelRow][squirrelCol] = '*';
  80.                         }
  81.                     }
  82.                     break;
  83.                 case "up":
  84.                     squirrelRow--;
  85.  
  86.                     if (squirrelStepsOutsideTheField(squirrelRow, squirrelCol, fieldSize)) {
  87.                         System.out.println("The squirrel is out of the field.");
  88.                         System.out.println("Hazelnuts collected: " + hazelnutCount);
  89.                         return;
  90.                     }
  91.  
  92.                     if (field[squirrelRow][squirrelCol] == 't') {
  93.                         System.out.println("Unfortunately, the squirrel stepped on a trap...");
  94.                         System.out.println("Hazelnuts collected: " + hazelnutCount);
  95.                         return;
  96.                     }
  97.  
  98.                     if (field[squirrelRow][squirrelCol] == 'h') {
  99.                         hazelnutCount++;
  100.                         if (hazelnutCount == 3) {
  101.                             System.out.println("Good job! You have collected all hazelnuts!");
  102.                             System.out.println("Hazelnuts collected: " + hazelnutCount);
  103.                             return;
  104.                         } else {
  105.                             field[squirrelRow][squirrelCol] = '*';
  106.                         }
  107.                     }
  108.                     break;
  109.                 case "down":
  110.                     squirrelRow++;
  111.  
  112.                     if (squirrelStepsOutsideTheField(squirrelRow, squirrelCol, fieldSize)) {
  113.                         System.out.println("The squirrel is out of the field.");
  114.                         System.out.println("Hazelnuts collected: " + hazelnutCount);
  115.                         return;
  116.                     }
  117.  
  118.                     if (field[squirrelRow][squirrelCol] == 't') {
  119.                         System.out.println("Unfortunately, the squirrel stepped on a trap...");
  120.                         System.out.println("Hazelnuts collected: " + hazelnutCount);
  121.                         return;
  122.                     }
  123.  
  124.                     if (field[squirrelRow][squirrelCol] == 'h') {
  125.                         hazelnutCount++;
  126.                         if (hazelnutCount == 3) {
  127.                             System.out.println("Good job! You have collected all hazelnuts!");
  128.                             System.out.println("Hazelnuts collected: " + hazelnutCount);
  129.                             return;
  130.                         } else {
  131.                             field[squirrelRow][squirrelCol] = '*';
  132.                         }
  133.                     }
  134.                     break;
  135.             }
  136.         }
  137.  
  138.         System.out.println("There are more hazelnuts to collect.");
  139.         System.out.println("Hazelnuts collected: " + hazelnutCount);
  140.     }
  141.  
  142.     private static boolean squirrelStepsOutsideTheField(int row, int col, int size) {
  143.         return row < 0 || col < 0 || row >= size || col >= size;
  144.     }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement