Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class ONEmBookWorm {
- public static int playerRow = 0;
- public static int playerCol = 0;
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- String initialString = 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")) {
- String currentLetter;
- switch (powerBook) {
- case "up":
- playerRow -= 1;
- playerCol += 0;
- if (checkIndex(matrix, playerRow, playerCol)) {
- if (!matrix[playerRow][playerCol].equals("-")) {
- String toAdd = matrix[playerRow][playerCol];
- initialString += toAdd;
- matrix[playerRow][playerCol] = "P";
- } else {
- initialString += "";
- //matrix[playerRow][playerCol] = "P";
- }
- } else {
- int end = initialString.length() - 1;
- initialString = initialString.substring(0, end);
- }
- break;
- case "down":
- playerRow += 1;
- playerCol += 0;
- if (checkIndex(matrix, playerRow, playerCol)) {
- if (!matrix[playerRow][playerCol].equals("-")) {
- String toAdd = matrix[playerRow][playerCol];
- initialString += toAdd;
- matrix[playerRow][playerCol] = "P";
- } else {
- initialString += "";
- //matrix[playerRow][playerCol] = "P";
- }
- } else {
- int end = initialString.length() - 1;
- initialString = initialString.substring(0, end);
- }
- break;
- case "left":
- playerRow += 0;
- playerCol -= 1;
- if (checkIndex(matrix, playerRow, playerCol)) {
- if (!matrix[playerRow][playerCol].equals("-")) {
- String toAdd = matrix[playerRow][playerCol];
- initialString += toAdd;
- matrix[playerRow][playerCol] = "P";
- } else {
- initialString += "";
- //matrix[playerRow][playerCol] = "P";
- }
- } else {
- int end = initialString.length() - 1;
- initialString = initialString.substring(0, end);
- }
- break;
- case "right":
- playerRow += 0;
- playerCol += 1;
- if (checkIndex(matrix, playerRow, playerCol)) {
- if (!matrix[playerRow][playerCol].equals("-")) {
- String toAdd = matrix[playerRow][playerCol];
- initialString += toAdd;
- matrix[playerRow][playerCol] = "P";
- } else {
- initialString += "";
- //matrix[playerRow][playerCol] = "P";
- }
- } else {
- int end = initialString.length() - 1;
- initialString = initialString.substring(0, end);
- }
- break;
- }
- }
- System.out.println(initialString);
- 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