Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- StringBuilder word = new StringBuilder(sc.nextLine());
- int size = Integer.parseInt(sc.nextLine());
- String[][] matrix = new String[size][size];
- for (int rows = 0; rows < matrix.length; rows++) {
- String[] arr = sc.nextLine().split("");
- matrix[rows] = arr;
- }
- String command = sc.nextLine();
- int playerRow = 0;
- int playerCol = 0;
- while (!"end".equals(command)) {
- for (int rows = 0; rows < matrix.length; rows++) {
- for (int cols = 0; cols < matrix[rows].length; cols++) {
- if (matrix[rows][cols].equals("P")) {
- playerRow = rows;
- playerCol = cols;
- }
- }
- }
- int newField = 0;
- switch (command) {
- case "up":
- newField = moveUp(matrix, playerRow, playerCol);
- if (newField == playerRow) {
- word.deleteCharAt(word.length() - 1);
- } else {
- if (!matrix[newField][playerCol].equals("-")) {
- word.append(matrix[newField][playerCol]);
- matrix[playerRow][playerCol] = "-";
- matrix[newField][playerCol] = "P";
- } else {
- matrix[playerRow][playerCol] = "-";
- matrix[newField][playerCol] = "P";
- }
- }
- break;
- case "down":
- newField = moveDown(matrix, playerRow, playerCol);
- if (newField == playerRow) {
- word.deleteCharAt(word.length() - 1);
- } else {
- if (!matrix[newField][playerCol].equals("-")) {
- word.append(matrix[newField][playerCol]);
- matrix[playerRow][playerCol] = "-";
- matrix[newField][playerCol] = "P";
- } else {
- matrix[playerRow][playerCol] = "-";
- matrix[newField][playerCol] = "P";
- }
- }
- break;
- case "left":
- newField = moveLeft(matrix, playerRow, playerCol);
- if (newField == playerCol) {
- word.deleteCharAt(word.length() - 1);
- } else {
- if (!matrix[playerRow][newField].equals("-")) {
- word.append(matrix[playerRow][newField]);
- matrix[playerRow][playerCol] = "-";
- matrix[playerRow][newField] = "P";
- } else {
- matrix[playerRow][playerCol] = "-";
- matrix[playerRow][newField] = "P";
- }
- }
- break;
- case "right":
- newField = moveRight(matrix, playerRow, playerCol);
- if (newField == playerCol) {
- word.deleteCharAt(word.length() - 1);
- } else {
- if (!matrix[playerRow][newField].equals("-")) {
- word.append(matrix[playerRow][newField]);
- matrix[playerRow][playerCol] = "-";
- matrix[playerRow][newField] = "P";
- } else {
- matrix[playerRow][playerCol] = "-";
- matrix[playerRow][newField] = "P";
- }
- }
- break;
- }
- command = sc.nextLine();
- if ("end".equals(command)) {
- System.out.println(word);
- for (int rows = 0; rows < matrix.length; rows++) {
- for (int cols = 0; cols < matrix[rows].length; cols++) {
- System.out.print(matrix[rows][cols]);
- }
- System.out.println();
- }
- }
- }
- }
- public static int moveUp(String[][] matrix, int playerRow, int playerCol) {
- if (playerRow > 0) {
- playerRow -= 1;
- }
- return playerRow;
- }
- public static int moveDown(String[][] matrix, int playerRow, int playerCol) {
- if (playerRow < matrix.length - 1) {
- playerRow += 1;
- }
- return playerRow;
- }
- public static int moveLeft(String[][] matrix, int playerRow, int playerCol) {
- if (playerCol > 0) {
- playerCol -= 1;
- }
- return playerCol;
- }
- public static int moveRight(String[][] matrix, int playerRow, int playerCol) {
- if (playerCol < matrix.length - 1) {
- playerCol += 1;
- }
- return playerCol;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement