Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class fbBookWorm {
- public static int playerRow;
- public static int playerCol;
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- StringBuilder initialString = new StringBuilder(sc.nextLine());
- Integer sizeSquareM = Integer.parseInt(sc.nextLine());
- String[][] matrix = new String[sizeSquareM][sizeSquareM];
- readMatrix(matrix, sc);
- String powerBook;
- while (!(powerBook = sc.nextLine()).equals("end")) {
- int moveToRow = playerRow; // --> прехвърляме тук към нови променливи, за да правим проверките, без да местим истинските индекси на играча
- int moveToCol = playerCol;
- String currentLetter;
- switch (powerBook) {
- case "up":
- moveToRow -= 1;
- if (checkIndex(matrix, moveToCol, moveToCol)) {
- if (!matrix[moveToRow][moveToCol].equals("-")) {
- String toAdd = matrix[moveToRow][moveToCol];
- initialString.append(toAdd);
- }
- matrix[moveToRow][moveToCol] = "P";
- matrix[playerRow][playerCol] = "-"; // ---> сетваме там където е бил тиренце, за да му махнем буквата
- playerRow = moveToRow;
- playerCol = moveToCol; // ---> местим му индексите
- //else {
- // initialString += "";
- // //matrix[playerRow][playerCol] = "P"; не ти трябва това махам от всички кейсове
- // }
- } else {
- // int end = initialString.length() - 1;
- //initialString = new StringBuilder(initialString.substring(0, end));
- initialString.deleteCharAt(initialString.length() -1);
- }
- break;
- case "down":
- moveToRow += 1;
- if (checkIndex(matrix, moveToRow, moveToCol)) {
- if (!matrix[moveToRow][moveToCol].equals("-")) {
- String toAdd = matrix[moveToRow][moveToCol];
- initialString.append(toAdd);
- }
- matrix[moveToRow][moveToCol] = "P";
- matrix[playerRow][playerCol] = "-";
- playerRow = moveToRow;
- playerCol = moveToCol;
- } else {
- //int end = initialString.length() - 1;
- // initialString = new StringBuilder(initialString.substring(0, end));
- initialString.deleteCharAt(initialString.length() -1);
- }
- break;
- case "left":
- moveToCol -= 1;
- if (checkIndex(matrix, moveToRow, moveToCol)) {
- if (!matrix[moveToRow][moveToCol].equals("-")) {
- String toAdd = matrix[moveToRow][moveToCol];
- initialString.append(toAdd);
- }
- matrix[moveToRow][moveToCol] = "P";
- matrix[playerRow][playerCol] = "-";
- playerRow = moveToRow;
- playerCol = moveToCol;
- //else {
- // initialString += "";
- // //matrix[playerRow][playerCol] = "P";
- // }
- } else {
- //int end = initialString.length() - 1;
- //initialString = new StringBuilder(initialString.substring(0, end));
- initialString.deleteCharAt(initialString.length() -1);
- }
- break;
- case "right":
- moveToCol += 1;
- if (checkIndex(matrix, moveToRow, moveToCol)) {
- if (!matrix[moveToRow][moveToCol].equals("-")) {
- String toAdd = matrix[moveToRow][moveToCol];
- initialString.append(toAdd);
- }
- matrix[moveToRow][moveToCol] = "P";
- matrix[playerRow][playerCol] = "-";
- playerRow = moveToRow;
- playerCol = moveToCol;
- //else {
- // initialString += "";
- // //matrix[playerRow][playerCol] = "P";
- // }
- } else {
- // int end = initialString.length() - 1;
- //
- initialString.deleteCharAt(initialString.length() -1);
- }
- break;
- }
- }
- System.out.println(initialString.toString());
- printMatrix(matrix);
- }
- private static void readMatrix(String[][] matrix, Scanner scanner) {
- for (int r = 0; r < matrix.length; r++) {
- String input = scanner.nextLine();
- for (int c = 0; c < matrix[r].length; c++) {
- matrix[r][c] = input.charAt(c) + "";
- if (matrix[r][c].equals("P")) {
- playerRow = r;
- playerCol = c;
- // matrix[r][c]="-";//TODO check оставяме си играча с буквата, като го занулиш го губиш, сред другите
- }
- }
- }
- }
- private static void printMatrix(String[][] matrix) {
- for (int i = 0; i < matrix.length; i++) {
- for (int j = 0; j < matrix[i].length; j++) {
- System.out.print(matrix[i][j] + "");
- }
- System.out.println();
- }
- }
- private static boolean checkIndex(String[][] matrix, int a, int b) {
- return a >= 0 && a < matrix.length && b >= 0 && b < matrix[a].length;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement