Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package examFinal22022020;
- import java.util.Scanner;
- public class ReVolt {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int N = Integer.parseInt(scanner.nextLine());
- int numOfCommands = Integer.parseInt(scanner.nextLine());
- String[][] field = new String[N][N];
- boolean playerLost = true;
- for (int row = 0; row < field.length; row++) {
- field[row] = scanner.nextLine().split("");
- }
- int playersRowIndex = getRowIndex(field, "f");
- int playersColIndex = getColIndex(field, "f");
- for (int i = 0; i < numOfCommands; i++) {
- boolean finish = false;
- String command = scanner.nextLine();
- switch (command) {
- case "up":
- if (isValidMove(playersRowIndex - 1, playersColIndex, field)) {
- if (!checkIfTrap(field, playersRowIndex - 1, playersColIndex)) {
- if (field[playersRowIndex - 1][playersColIndex].equals("B")) {
- if (isValidMove(playersRowIndex - 2, playersColIndex, field)) {
- if (checkIfThePlayerFinish(field, playersRowIndex - 2, playersColIndex)) {
- field[playersRowIndex - 2][playersColIndex] = "f";
- field[playersRowIndex][playersColIndex] = "-";
- finish = true;
- break;
- } else {
- playersRowIndex -= 2;
- field[playersRowIndex][playersColIndex] = "f";
- field[playersRowIndex + 2][playersColIndex] = "-";
- if (checkIfThePlayerFinish(field, playersRowIndex, playersColIndex)) {
- finish = true;
- break;
- }
- }
- } else {
- field[playersRowIndex][playersColIndex] = "-";
- playersRowIndex = field.length - 1;
- if (checkIfThePlayerFinish(field, playersRowIndex, playersColIndex)) {
- finish = true;
- break;
- } else {
- field[playersRowIndex][playersColIndex] = "f";
- }
- }
- } else {
- if (checkIfThePlayerFinish(field, playersRowIndex - 1, playersColIndex)) {
- field[playersRowIndex][playersColIndex] = "-";
- field[playersRowIndex - 1][playersColIndex] = "f";
- finish = true;
- break;
- } else {
- field[playersRowIndex][playersColIndex] = "-";
- field[playersRowIndex - 1][playersColIndex] = "f";
- playersRowIndex -= 1;
- }
- }
- }
- } else {
- field[playersRowIndex][playersColIndex] = "-";
- playersRowIndex = field.length - 1;
- if (field[playersRowIndex][playersColIndex].equals("B")) {
- playersRowIndex -= 1;
- }
- if (checkIfThePlayerFinish(field, playersRowIndex, playersColIndex)) {
- field[playersRowIndex][playersColIndex] = "f";
- field[playersRowIndex - 1][playersColIndex] = "-";
- finish = true;
- break;
- }
- field[playersRowIndex][playersColIndex] = "f";
- }
- break;
- case "down":
- if (isValidMove(playersRowIndex + 1, playersColIndex, field)) {
- if (!checkIfTrap(field, playersRowIndex + 1, playersColIndex)) {
- if (field[playersRowIndex + 1][playersColIndex].equals("B")) {
- if (isValidMove(playersRowIndex + 2, playersColIndex, field)) {
- if (checkIfThePlayerFinish(field, playersRowIndex + 2, playersColIndex)) {
- field[playersRowIndex + 2][playersColIndex] = "f";
- field[playersRowIndex][playersColIndex] = "-";
- finish = true;
- break;
- } else {
- playersRowIndex += 2;
- field[playersRowIndex][playersColIndex] = "f";
- field[playersRowIndex - 2][playersColIndex] = "-";
- if (checkIfThePlayerFinish(field, playersRowIndex, playersColIndex)) {
- finish = true;
- break;
- }
- }
- } else {
- field[playersRowIndex][playersColIndex] = "-";
- playersRowIndex = 0;
- if (checkIfThePlayerFinish(field, playersRowIndex, playersColIndex)) {
- finish = true;
- break;
- } else {
- field[playersRowIndex][playersColIndex] = "f";
- }
- }
- } else {
- if (checkIfThePlayerFinish(field, playersRowIndex + 1, playersColIndex)) {
- field[playersRowIndex][playersColIndex] = "-";
- field[playersRowIndex + 1][playersColIndex] = "f";
- finish = true;
- break;
- } else {
- field[playersRowIndex][playersColIndex] = "-";
- field[playersRowIndex + 1][playersColIndex] = "f";
- playersRowIndex += 1;
- }
- }
- }
- } else {
- field[playersRowIndex][playersColIndex] = "-";
- playersRowIndex = 0;
- if (field[playersRowIndex][playersColIndex].equals("B")) {
- playersRowIndex += 1;
- }
- if (checkIfThePlayerFinish(field, playersRowIndex, playersColIndex)) {
- field[playersRowIndex][playersColIndex] = "f";
- field[playersRowIndex + 1][playersColIndex] = "-";
- finish = true;
- break;
- }
- field[playersRowIndex][playersColIndex] = "f";
- }
- break;
- case "left":
- if (isValidMove(playersRowIndex, playersColIndex - 1, field)) {
- if (!checkIfTrap(field, playersRowIndex, playersColIndex - 1)) {
- if (field[playersRowIndex][playersColIndex - 1].equals("B")) {
- if (isValidMove(playersRowIndex, playersColIndex - 2, field)) {
- if (checkIfThePlayerFinish(field, playersRowIndex, playersColIndex - 2)) {
- field[playersRowIndex][playersColIndex - 2] = "f";
- field[playersRowIndex][playersColIndex] = "-";
- finish = true;
- break;
- } else {
- playersColIndex -= 2;
- if (checkIfThePlayerFinish(field, playersRowIndex, playersColIndex)) {
- finish = true;
- break;
- } else {
- field[playersRowIndex][playersColIndex + 2] = "-";
- field[playersRowIndex][playersColIndex] = "f";
- }
- }
- } else {
- field[playersRowIndex][playersColIndex] = "-";
- playersColIndex = field.length - 1;
- if (checkIfThePlayerFinish(field, playersRowIndex, playersColIndex)) {
- finish = true;
- break;
- } else {
- field[playersRowIndex][playersColIndex] = "f";
- }
- }
- } else {
- if (checkIfThePlayerFinish(field, playersRowIndex, playersColIndex + 1)) {
- field[playersRowIndex][playersColIndex] = "-";
- field[playersRowIndex][playersColIndex - 1] = "f";
- finish = true;
- break;
- } else {
- field[playersRowIndex][playersColIndex] = "-";
- field[playersRowIndex][playersColIndex - 1] = "f";
- playersColIndex -= 1;
- }
- }
- }
- } else {
- field[playersRowIndex][playersColIndex] = "-";
- playersColIndex = field.length - 1;
- if (field[playersRowIndex][playersColIndex].equals("B")) {
- playersColIndex -= 1;
- }
- if (checkIfThePlayerFinish(field, playersRowIndex, playersColIndex)) {
- field[playersRowIndex][playersColIndex] = "f";
- field[playersRowIndex][playersColIndex - 1] = "-";
- finish = true;
- break;
- }
- field[playersRowIndex][playersColIndex] = "f";
- }
- break;
- case "right":
- if (isValidMove(playersRowIndex, playersColIndex + 1, field)) {
- if (!checkIfTrap(field, playersRowIndex, playersColIndex + 1)) {
- if (field[playersRowIndex][playersColIndex + 1].equals("B")) {
- if (isValidMove(playersRowIndex, playersColIndex + 2, field)) {
- if (checkIfThePlayerFinish(field, playersRowIndex, playersColIndex + 2)) {
- field[playersRowIndex][playersColIndex + 2] = "f";
- field[playersRowIndex][playersColIndex] = "-";
- finish = true;
- break;
- } else {
- playersColIndex += 2;
- if (checkIfThePlayerFinish(field, playersRowIndex, playersColIndex)) {
- finish = true;
- break;
- } else {
- field[playersRowIndex][playersColIndex - 2] = "-";
- field[playersRowIndex][playersColIndex] = "f";
- }
- }
- } else {
- field[playersRowIndex][playersColIndex] = "-";
- playersColIndex = 0;
- if (checkIfThePlayerFinish(field, playersRowIndex, playersColIndex)) {
- finish = true;
- break;
- } else {
- field[playersRowIndex][playersColIndex] = "f";
- }
- }
- } else {
- if (checkIfThePlayerFinish(field, playersRowIndex, playersColIndex + 1)) {
- field[playersRowIndex][playersColIndex] = "-";
- field[playersRowIndex][playersColIndex + 1] = "f";
- finish = true;
- break;
- } else {
- field[playersRowIndex][playersColIndex] = "-";
- field[playersRowIndex][playersColIndex + 1] = "f";
- playersColIndex += 1;
- }
- }
- }
- } else {
- field[playersRowIndex][playersColIndex] = "-";
- playersColIndex = 0;
- if (field[playersRowIndex][playersColIndex].equals("B")) {
- playersColIndex += 1;
- }
- if (checkIfThePlayerFinish(field, playersRowIndex, playersColIndex)) {
- field[playersRowIndex][playersColIndex] = "f";
- field[playersRowIndex][playersColIndex - 1] = "-";
- finish = true;
- break;
- }
- field[playersRowIndex][playersColIndex] = "f";
- }
- break;
- }
- if (finish) {
- playerLost = false;
- System.out.println("Player won!");
- break;
- }
- }
- if (playerLost) {
- System.out.println("Player lost!");
- }
- printTheMatrix(field);
- }
- private static void printTheMatrix(String[][] matrix) {
- for (int row = 0; row < matrix.length; row++) {
- for (int col = 0; col < matrix[row].length; col++) {
- System.out.print(matrix[row][col]);
- }
- System.out.println();
- }
- }
- private static boolean checkIfTrap(String[][] matrix, int row, int col) {
- return matrix[row][col].equals("T");
- }
- private static boolean checkIfThePlayerFinish(String[][] matrix, int row, int col) {
- if (matrix[row][col].equals("F")) {
- return true;
- }
- return false;
- }
- private static int getColIndex(String[][] matrix, String name) {
- for (int row = 0; row < matrix.length; row++) {
- for (int col = 0; col < matrix[row].length; col++) {
- if (matrix[row][col].equals(name)) {
- return col;
- }
- }
- }
- return -1;
- }
- private static int getRowIndex(String[][] matrix, String name) {
- for (int row = 0; row < matrix.length; row++) {
- for (int col = 0; col < matrix[row].length; col++) {
- if (matrix[row][col].equals(name)) {
- return row;
- }
- }
- }
- return -1;
- }
- private static boolean isValidMove(int row, int col, String[][] matrix) {
- return row >= 0 && row <= matrix.length - 1 && col <= matrix[row].length - 1 && col >= 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement