Advertisement
Ivelin_Arsov

Book Worm

Jun 20th, 2021
1,097
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.84 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.regex.Pattern;
  3.  
  4. public class BookWorm {
  5.     private static String input;
  6.  
  7.     public static void main(String[] args) {
  8.         Scanner scan = new Scanner(System.in);
  9.         input = scan.nextLine();
  10.  
  11.         int size = Integer.parseInt(scan.nextLine());
  12.         char[][] matrix = new char[size][size];
  13.  
  14.         int[] position = new int[2];
  15.         fillMatrix(matrix, scan, position);
  16.  
  17.         String command = scan.nextLine();
  18.         while (!command.equals("end")) {
  19.             switch (command) {
  20.                 case "up":
  21.                     moveAction(matrix, position, -1, 0);
  22.                     break;
  23.                 case "down":
  24.                     moveAction(matrix, position, 1, 0);
  25.                     break;
  26.                 case "left":
  27.                     moveAction(matrix, position, 0, -1);
  28.                     break;
  29.                 case "right":
  30.                     moveAction(matrix, position, 0, 1);
  31.                     break;
  32.             }
  33.             command = scan.nextLine();
  34.         }
  35.  
  36.         StringBuilder builder = new StringBuilder();
  37.         builder.append(input).append(System.lineSeparator());
  38.         for (char[] chars : matrix) {
  39.             builder.append(chars).append(System.lineSeparator());
  40.         }
  41.         System.out.println(builder);
  42.  
  43.  
  44.     }
  45.  
  46.     private static void moveAction(char[][] matrix, int[] position, int rowModificator, int colModificator) {
  47.         int row = position[0];
  48.         int col = position[1];
  49.         int newRow = indexCheck(row, rowModificator, matrix.length);
  50.         int newCol = indexCheck(col, colModificator, matrix.length);
  51.         if((row + rowModificator != -1 ) && (col + colModificator != -1)){
  52.             if ((matrix[newRow][newCol] >= 'A' && matrix[newRow][newCol] != 'P' && matrix[newRow][newCol] <= 'Z') || (matrix[newRow][newCol] >= 'a' && matrix[newRow][newCol] <= 'z')) {
  53.                 input += matrix[newRow][newCol];
  54.             }
  55.  
  56.         }
  57.         position[0] = newRow;
  58.         position[1] = newCol;
  59.         matrix[row][col] = '-';
  60.         matrix[newRow][newCol] = 'P';
  61. }
  62.  
  63.     private static int indexCheck(int index, int modifier, int bound) {
  64.         index = index + modifier;
  65.         if (index < 0 || index >= bound) {
  66.             if(input.length() > 0){
  67.                 input = input.substring(0, input.length() - 1);
  68.             }
  69.             return index - modifier;
  70.         }
  71.         return index;
  72.     }
  73.  
  74.     private static void fillMatrix(char[][] matrix, Scanner scan, int[] position) {
  75.         for (int row = 0; row < matrix.length; row++) {
  76.             String line = scan.nextLine();
  77.             matrix[row] = line.toCharArray();
  78.             if (line.contains("P")) {
  79.                 position[0] = row;
  80.                 position[1] = line.indexOf("P");
  81.             }
  82.         }
  83.     }
  84. }
  85.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement