Advertisement
desislava_topuzakova

02. Mouse and cheese

Jan 22nd, 2022
1,156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.70 KB | None | 0 0
  1. package StreamsFilesDirectories_Exercise;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class MouseAndCheese_02 {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.         int n = Integer.parseInt(scanner.nextLine()); //бр. редове = бр. колони
  9.         char [][] matrix = new char[n][n];
  10.         int mouseRow = -1; //ред на мишката
  11.         int mouseCol = -1; //колона на мишката
  12.         int countCheese = 0; //брой изядени сиренца
  13.  
  14.         //пълним матрицата
  15.         fillMatrix(scanner, n, matrix);
  16.  
  17.         //намираме мишката
  18.         for (int row = 0; row < n; row++) {
  19.             for (int col = 0; col < n; col++) {
  20.                 char currentSymbol = matrix[row][col];
  21.                 if (currentSymbol == 'M') {
  22.                     mouseRow = row;
  23.                     mouseCol = col;
  24.                 }
  25.             }
  26.         }
  27.  
  28.         String direction = scanner.nextLine();
  29.         while (!direction.equals("end")) {
  30.             //direction -> "up", "down", "left", "right"
  31.             matrix[mouseRow][mouseCol] = '-';
  32.             switch(direction) {
  33.                 case "up":
  34.                     //намаляме ред с 1
  35.                     mouseRow--;
  36.                     break;
  37.                 case "down":
  38.                     //увеличаваме ред с 1
  39.                     mouseRow++;
  40.                     break;
  41.                 case "left":
  42.                     //намаляме колоната с 1
  43.                     mouseCol--;
  44.                     break;
  45.                 case "right":
  46.                     //увеличаваме колоната с 1
  47.                     mouseCol++;
  48.                     break;
  49.             }
  50.             //преди да поставим мишката на новото и място -> проверка на ред и колоната
  51.             if (mouseCol < 0 || mouseCol >= n || mouseRow < 0 || mouseRow >= n) {
  52.                 System.out.println("Where is the mouse?");
  53.                 break;
  54.             }
  55.  
  56.             //проверим мястото, на което ще отиде мишката
  57.             if (matrix[mouseRow][mouseCol] == 'c') {
  58.                 //сирене
  59.                 countCheese++;
  60.             } else if (matrix[mouseRow][mouseCol] == 'B') {
  61.                 //бонус
  62.                 continue;
  63.             }
  64.             //поставяне на новото място на мишката
  65.             matrix[mouseRow][mouseCol] = 'M';
  66.  
  67.             direction = scanner.nextLine();
  68.         }
  69.  
  70.         //проверка дали е изяла достатъчно сиренца
  71.         if (countCheese >= 5) {
  72.             System.out.printf("Great job, the mouse is fed %d cheeses!", countCheese);
  73.         } else {
  74.             System.out.printf("The mouse couldn't eat the cheeses, she needed %d cheeses more.", 5 - countCheese);
  75.         }
  76.  
  77.         //принтиране на матрицата
  78.         printMatrix(n, matrix);
  79.     }
  80.  
  81.     private static void printMatrix(int n, char[][] matrix) {
  82.         System.out.println();
  83.         for (int row = 0; row < n; row++) {
  84.             for (int col = 0; col < n; col++) {
  85.                 System.out.print(matrix[row][col]);
  86.             }
  87.             System.out.println();
  88.         }
  89.     }
  90.  
  91.     private static void fillMatrix(Scanner scanner, int n, char[][] matrix) {
  92.         for (int row = 0; row < n; row++) {
  93.             String rowContent = scanner.nextLine(); // "M--"
  94.             char [] rowSymbols = rowContent.toCharArray(); // ['M', '-', '-']
  95.             matrix[row] = rowSymbols;
  96.         }
  97.     }
  98. }
  99.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement