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);
- int size = Integer.parseInt(sc.nextLine());
- int commands = 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;
- }
- int playerRow = 0;
- int playerCol = 0;
- boolean win = false;
- int previousField = 0;
- for (int i = 0; i < commands; i++) {
- if (win){
- break;
- }
- String command = sc.nextLine();
- for (int k = 0; k < matrix.length; k++) {
- for (int o = 0; o < matrix[k].length; o++) {
- if (matrix[k][o].equals("f")) {
- playerRow = k;
- playerCol = o;
- }
- }
- }
- int nextField = 0;
- switch (command) {
- case "up":
- nextField = moveUp(matrix, playerRow);
- if (matrix[nextField][playerCol].equals("T")){
- break;
- }
- previousField = playerRow;
- playerRow = nextField;
- switch (matrix[playerRow][playerCol]) {
- case "-":
- matrix[previousField][playerCol] = "-";
- matrix[playerRow][playerCol] = "f";
- break;
- case "B":
- matrix[previousField][playerCol] = "-";
- nextField = moveUp(matrix, playerRow);
- playerRow = nextField;
- matrix[playerRow][playerCol] = "f";
- break;
- case "F":
- matrix[previousField][playerCol] = "-";
- matrix[playerRow][playerCol] = "f";
- win = true;
- continue;
- }
- break;
- case "down":
- nextField = moveDown(matrix, playerRow);
- if (matrix[nextField][playerCol].equals("T")){
- break;
- }
- previousField = playerRow;
- playerRow = nextField;
- switch (matrix[playerRow][playerCol]) {
- case "-":
- matrix[previousField][playerCol] = "-";
- matrix[playerRow][playerCol] = "f";
- break;
- case "B":
- matrix[previousField][playerCol] = "-";
- nextField = moveDown(matrix, playerRow);
- playerRow = nextField;
- matrix[playerRow][playerCol] = "f";
- break;
- case "F":
- matrix[previousField][playerCol] = "-";
- matrix[playerRow][playerCol] = "f";
- win = true;
- continue;
- }
- break;
- case "left":
- nextField = moveLeft(matrix, playerCol);
- if (matrix[playerRow][nextField].equals("T")){
- break;
- }
- previousField = playerCol;
- playerCol = nextField;
- switch (matrix[playerRow][playerCol]) {
- case "-":
- matrix[playerRow][previousField] = "-";
- matrix[playerRow][playerCol] = "f";
- break;
- case "B":
- matrix[playerRow][previousField] = "-";
- nextField = moveLeft(matrix, playerCol);
- playerCol = nextField;
- matrix[playerRow][playerCol] = "f";
- break;
- case "F":
- matrix[playerRow][previousField] = "-";
- matrix[playerRow][playerCol] = "f";
- win = true;
- continue;
- }
- break;
- case "right":
- nextField = moveRight(matrix, playerCol);
- if (matrix[playerRow][nextField].equals("T")){
- break;
- }
- previousField = playerCol; // prev
- playerCol = nextField; // row = next
- switch (matrix[playerRow][playerCol]) {
- case "-":
- matrix[playerRow][previousField] = "-";
- matrix[playerRow][playerCol] = "f";
- break;
- case "B":
- matrix[playerRow][previousField] = "-";
- nextField = moveRight(matrix, playerRow);
- playerCol = nextField;
- matrix[playerRow][playerCol] = "f";
- break;
- case "F":
- matrix[playerRow][previousField] = "-";
- matrix[playerRow][playerCol] = "f";
- win = true;
- continue;
- }
- break;
- }
- }
- if (win) {
- System.out.println("Player won!");
- printMatrix(matrix);
- }
- if (!win) {
- System.out.println("Player lost!");
- printMatrix(matrix);
- }
- }
- public static int moveUp(String[][] matrix, int playerRow) {
- if (playerRow > 0) {
- return playerRow - 1;
- } else {
- return matrix.length - 1;
- }
- }
- public static int moveDown(String[][] matrix, int playerRow) {
- if (playerRow < matrix.length - 1) {
- return playerRow + 1;
- } else {
- return 0;
- }
- }
- public static int moveLeft(String[][] matrix, int playerCol) {
- if (playerCol > 0) {
- return playerCol - 1;
- } else {
- return matrix.length - 1;
- }
- }
- public static int moveRight(String[][] matrix, int playerCol) {
- if (playerCol < matrix.length - 1) {
- return playerCol + 1;
- } else {
- return 0;
- }
- }
- public static void printMatrix(String[][] matrix) {
- 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();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement