kalin729

mc 55/100

Oct 23rd, 2021 (edited)
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.65 KB | None | 0 0
  1. package Exam;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class MouseAndCheese {
  6.     static int startRow = 0;
  7.     static int startCol = 0;
  8.     static int bonus = 0;
  9.     static int cheese = 0;
  10.     static int cheeseEaten = 0;
  11.     static boolean foundEnd = false;
  12.  
  13.     public static void main(String[] args) {
  14.         Scanner scanner = new Scanner(System.in);
  15.         int territorySize = Integer.parseInt(scanner.nextLine());
  16.         char[][] territory = new char[territorySize][territorySize];
  17.         for (int row = 0; row < territorySize; row++) {
  18.             String line = scanner.nextLine().replaceAll("\\s+", "");
  19.             territory[row] = line.toCharArray();
  20.             if (line.contains("M")) {
  21.                 startRow = row;
  22.                 startCol = line.indexOf("M");
  23.             }
  24.             for (char chars : territory[row]) {
  25.                 if (chars == 'B') {
  26.                     bonus++;
  27.                 }
  28.                 if (chars == 'c') {
  29.                     cheese++;
  30.                 }
  31.             }
  32.         }
  33.  
  34.         String command = scanner.nextLine();
  35.         while (!command.equals("end")) {
  36.             moveCommands(territory, command);
  37. //            printField(territory);
  38. //            System.out.println();
  39.             if (foundEnd) {
  40.                 break;
  41.             }
  42.             command = scanner.nextLine();
  43.         }
  44.  
  45.         if (cheeseEaten>=5) {
  46.             System.out.println("Great job, the mouse is fed " + cheeseEaten + " cheeses!");
  47.         }
  48.         else {
  49.             System.out.printf("The mouse couldn't eat the cheeses, she needed %d cheeses more.", (5-cheeseEaten));
  50.         }
  51.         System.out.println();
  52.         printField(territory);
  53.  
  54.     }
  55.  
  56.     private static void moveCommands(char[][] territory, String command) {
  57.         switch (command) {
  58.             case "up":
  59.                 foundEnd = moveMouse(territory, startRow - 1, startCol, command);
  60.                 break;
  61.             case "down":
  62.                 foundEnd = moveMouse(territory, startRow + 1, startCol, command);
  63.                 break;
  64.             case "left":
  65.                 foundEnd = moveMouse(territory, startRow, startCol - 1, command);
  66.                 break;
  67.             case "right":
  68.                 foundEnd = moveMouse(territory, startRow, startCol + 1, command);
  69.                 break;
  70.         }
  71.     }
  72.  
  73.     private static boolean moveMouse(char[][] territory, int newRow, int newCol, String command) {
  74. //        System.out.println(command);
  75.         int oldRow = startRow;
  76.         int oldCol = startCol;
  77.         startRow = newRow;
  78.         startCol = newCol;
  79.         if (startRow >= territory.length || startRow < 0) {
  80.             territory[oldRow][oldCol] = '-';
  81.             System.out.println("Where is the mouse?");
  82.             return true;
  83.         }
  84.         if (startCol >= territory.length || startCol < 0) {
  85.             territory[oldRow][oldCol] = '-';
  86.             System.out.println("Where is the mouse?");
  87.             return true;
  88.         }
  89.         char current = territory[startRow][startCol];
  90.         territory[oldRow][oldCol] = '-';
  91.         territory[startRow][startCol] = 'M';
  92.         if (current == 'c') {
  93.             cheese--;
  94.             cheeseEaten++;
  95.         }
  96.         if (current == 'B') {
  97.             moveCommands(territory, command);
  98.             territory[startRow][startCol] = '-';
  99.         }
  100.         return false;
  101.     }
  102.  
  103.     private static void printField(char[][] field) {
  104.         for (char[] chars : field) {
  105.             for (int j = 0; j < field.length; j++) {
  106.                 System.out.print(chars[j] + "");
  107.             }
  108.             System.out.println();
  109.         }
  110.     }
  111. }
  112.  
Add Comment
Please, Sign In to add comment