RGAlex

O2. Selling.java/16 December 2020

Jun 23rd, 2021
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.74 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Selling {
  4.  
  5.     private static char[][] matrix;
  6.     private static final int[] indexes = new int[2];
  7.  
  8.     public static void main(String[] args) {
  9.  
  10.         Scanner scan = new Scanner(System.in);
  11.         readMatrix(scan, Integer.parseInt(scan.nextLine()));
  12.         findIndexes('S');
  13.         int money = 0;
  14.  
  15.         while (money < 50 && isInBounds()) {
  16.  
  17.             String input = scan.nextLine();
  18.             setPosition('-');
  19.  
  20.             if ("up".equals(input)) {
  21.                 indexes[0]--;
  22.             } else if ("down".equals(input)) {
  23.                 indexes[0]++;
  24.             } else if ("left".equals(input)) {
  25.                 indexes[1]--;
  26.             } else if ("right".equals(input)) {
  27.                 indexes[1]++;
  28.             }
  29.             if (isInBounds()) {
  30.                 if (checkPosition() == 'O') {
  31.  
  32.                     setPosition('-');
  33.                     findIndexes('O');
  34.  
  35.                 } else if (Character.isDigit(checkPosition())) {
  36.  
  37.                     money += Integer.parseInt(String.valueOf(matrix[indexes[0]][indexes[1]]));
  38.                 }
  39.                 setPosition('S');
  40.             }
  41.         }
  42.  
  43.         System.out.println(isInBounds()
  44.                 ? "Good news! You succeeded in collecting enough money!"
  45.                 : "Bad news, you are out of the bakery.");
  46.         System.out.println("Money: " + money);
  47.         printMatrix();
  48.  
  49.     }
  50.  
  51.     private static char checkPosition() {
  52.         return matrix[indexes[0]][indexes[1]];
  53.     }
  54.  
  55.     private static void printMatrix() {
  56.         Arrays.stream(matrix).map(row -> Arrays.toString(row).replaceAll("[\\[\\]]", "")
  57.                 .replaceAll(", ", "")).forEach(System.out::println);
  58.     }
  59.  
  60.     private static void readMatrix(Scanner scan, int rows) {
  61.         matrix = new char[rows][];
  62.         for (int row = 0; row < rows; row++) {
  63.             matrix[row] = scan.nextLine().replaceAll("\\s+", "").toCharArray();
  64.         }
  65.     }
  66.  
  67.     private static boolean isInBounds() {
  68.         return indexes[0] >= 0 && indexes[0] < matrix.length && indexes[1] >= 0 && indexes[1] < matrix[indexes[0]].length;
  69.     }
  70.  
  71.     private static void setPosition(char toSet) {
  72.         matrix[indexes[0]][indexes[1]] = toSet;
  73.     }
  74.  
  75.     private static void findIndexes(char toFind) {
  76.         boolean isFind = false;
  77.         for (int i = 0; i < matrix.length; i++) {
  78.             for (int j = 0; j < matrix[0].length; j++) {
  79.                 if (matrix[i][j] == toFind) {
  80.                     indexes[0] = i;
  81.                     indexes[1] = j;
  82.                     isFind = true;
  83.                     break;
  84.                 }
  85.             }
  86.             if (isFind) {
  87.                 break;
  88.             }
  89.         }
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment