RGAlex

O2. Bee.java/19 August 2020

Jun 23rd, 2021
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.72 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Bee {
  4.  
  5.     static int beeRow, beeCol;
  6.     static char[][] matrix;
  7.  
  8.     public static void main(String[] args) {
  9.  
  10.         Scanner scan = new Scanner(System.in);
  11.         readMatrix(scan);
  12.         int neededFlowers = 0;
  13.         String input;
  14.  
  15.         while (!"End".equals(input = scan.nextLine())) {
  16.  
  17.             matrix[beeRow][beeCol] = '.';
  18.  
  19.             switch (input) {
  20.                 case "right":
  21.                     beeCol++;
  22.                     checkForBonus(1, "col");
  23.                     break;
  24.                 case "left":
  25.                     beeCol--;
  26.                     checkForBonus(-1, "col");
  27.                     break;
  28.                 case "down":
  29.                     beeRow++;
  30.                     checkForBonus(1, "row");
  31.                     break;
  32.                 case "up":
  33.                     beeRow--;
  34.                     checkForBonus(-1, "row");
  35.                     break;
  36.             }
  37.             if (indexesInBounds()) {
  38.                 if (matrix[beeRow][beeCol] == 'f') {
  39.                     neededFlowers++;
  40.                 }
  41.             } else {
  42.                 System.out.println("The bee got lost!");
  43.                 break;
  44.             }
  45.             matrix[beeRow][beeCol] = 'B';
  46.         }
  47.         if (neededFlowers < 5) {
  48.             System.out.printf("The bee couldn't pollinate the flowers, she needed %d flowers more%n", 5 - neededFlowers);
  49.         } else {
  50.             System.out.printf("Great job, the bee manage to pollinate %d flowers!%n", neededFlowers);
  51.         }
  52.         printMatrix();
  53.     }
  54.  
  55.     private static void checkForBonus(int i, String dir) {
  56.         while (indexesInBounds() && matrix[beeRow][beeCol] == 'O') {
  57.             matrix[beeRow][beeCol] = '.';
  58.             if (dir.equals("col")) {
  59.                 beeCol = Math.abs(beeCol) + i;
  60.             } else {
  61.                 beeRow = Math.abs(beeRow) + i;
  62.             }
  63.         }
  64.     }
  65.  
  66.     private static boolean indexesInBounds() {
  67.         return (beeRow >= 0 && beeRow < matrix.length) && (beeCol >= 0 && beeCol < matrix[beeRow].length);
  68.     }
  69.  
  70.     private static void readMatrix(Scanner scan) {
  71.         int n = Integer.parseInt(scan.nextLine());
  72.         matrix = new char[n][n];
  73.         for (int row = 0; row < matrix.length; row++) {
  74.             String input = scan.nextLine();
  75.             matrix[row] = input.toCharArray();
  76.             if (input.contains("B")) {
  77.                 beeRow = row;
  78.                 beeCol = input.indexOf('B');
  79.             }
  80.         }
  81.     }
  82.  
  83.     private static void printMatrix() {
  84.         Arrays.stream(matrix).map(row -> Arrays.toString(row)
  85.                 .replaceAll("[\\[\\], ]", "")).forEach(System.out::println);
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment