RGAlex

BW.java2019

Jun 17th, 2021
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.55 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.stream.IntStream;
  3.  
  4. public class BookWorm {
  5.  
  6.     private static char[][] field;
  7.     private static int playerRol;
  8.     private static int playerCol;
  9.  
  10.     public static void main(String[] args) {
  11.  
  12.         Scanner scan = new Scanner(System.in);
  13.         StringBuilder input = new StringBuilder(scan.nextLine());
  14.         readField(scan, Integer.parseInt(scan.nextLine()));
  15.         String command;
  16.  
  17.         while (!"end".equals(command = scan.nextLine())) {
  18.             setPosition('-');
  19.             if ("up".equals(command)) {
  20.                 moveToRow(-1, input);
  21.             } else if ("down".equals(command)) {
  22.                 moveToRow(1, input);
  23.             } else if ("right".equals(command)) {
  24.                 moveToCol(1, input);
  25.             } else if ("left".equals(command)) {
  26.                 moveToCol(-1, input);
  27.             }
  28.             setPosition('P');
  29.         }
  30.         System.out.println(input.toString());
  31.         printField();
  32.     }
  33.  
  34.     private static void moveToRow(int n, StringBuilder input) {
  35.         if (isInBounds(playerRol + n)) {
  36.             playerRol += n;
  37.             chekPosition(input);
  38.         } else {
  39.             cutString(input);
  40.         }
  41.     }
  42.     private static void moveToCol(int n, StringBuilder input) {
  43.         if (isInBounds(playerCol + n)) {
  44.             playerCol += n;
  45.             chekPosition(input);
  46.         } else {
  47.             cutString(input);
  48.         }
  49.     }
  50.     private static void setPosition(char c) {
  51.         field[playerRol][playerCol] = c;
  52.     }
  53.     private static void cutString(StringBuilder input) {
  54.         if (input.length() > 0) {
  55.             input.deleteCharAt(input.length() - 1);
  56.         }
  57.     }
  58.     private static void chekPosition(StringBuilder input) {
  59.         if (field[playerRol][playerCol] != '-') {
  60.             input.append(field[playerRol][playerCol]);
  61.         }
  62.     }
  63.     private static boolean isInBounds(int index) {
  64.         return index < field.length && index >= 0;
  65.     }
  66.     private static void printField() {
  67.         Arrays.stream(field).map(row -> Arrays.toString(row)
  68.                 .replaceAll("[\\[\\], ]","")).forEach(System.out::println);
  69.     }
  70.     private static void readField(Scanner scan, int n) {
  71.         field = new char[n][n];
  72.         IntStream.range(0, n).forEach(i -> {  String input = scan.nextLine();
  73.             if (input.contains("P")) {
  74.                 playerRol = i;
  75.                 playerCol = input.indexOf('P');
  76.             }
  77.             field[i] = input.toCharArray();
  78.         });
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment