Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Exam20200222;
- import java.util.Scanner;
- public class ReVolt {
- public static int newRow = 0;
- public static int newCol = 0;
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int n = Integer.parseInt(scanner.nextLine());
- int countCommands = Integer.parseInt(scanner.nextLine());
- char[][] matrix = new char[n][n];
- // f - player
- int rowPlayer = 0;
- int colPlayer = 0;
- for (int i = 0; i < matrix.length; i++) {
- String input = scanner.nextLine();
- if (input.contains("f")) {
- rowPlayer = i;
- colPlayer = input.indexOf("f");
- }
- addCharArrayAsRowInMatrix(matrix, i, input);
- }
- while (countCommands-- > 0) { //&& !isOutOfBounds(rowPlayer, colPlayer, matrix)) {
- String command = scanner.nextLine();
- if (command.equals("up")) {
- //row--
- newRow = rowPlayer - 1;
- newCol = colPlayer;
- moveUp(matrix, rowPlayer);
- } else if (command.equals("down")) {
- //row++
- newRow = rowPlayer + 1;
- newCol = colPlayer;
- moveDown(matrix, rowPlayer);
- } else if (command.equals("left")) {
- //col--
- newRow = rowPlayer;
- newCol = colPlayer - 1;
- moveLeft(matrix, rowPlayer, colPlayer);
- } else if (command.equals("right")) {
- //col++
- newRow = rowPlayer;
- newCol = colPlayer + 1;
- moveRight(matrix, colPlayer);
- }
- // B - bonus
- // T - trap
- // F - finish
- if (matrix[newRow][newCol] == 'F') {
- countCommands = 0;
- System.out.println("Player won!");
- }
- if (countCommands == 0 && matrix[newRow][newCol] != 'F') {
- System.out.println("Player lost!");
- }
- matrix[rowPlayer][colPlayer] = '-';
- matrix[newRow][newCol] = 'f';
- rowPlayer = newRow;
- colPlayer = newCol;
- }
- for (int i = 0; i < matrix.length; i++) {
- System.out.println(String.valueOf(matrix[i]));
- }
- }
- public static void moveRight(char[][] matrix, int colPlayer) {
- if (isOutOfBounds(newRow, newCol, matrix)) {
- newCol = 0;
- }
- if (matrix[newRow][newCol] == 'B') {
- newCol = newCol + 1;
- if (isOutOfBounds(newRow, newCol, matrix)) {
- newCol = 0;
- }
- } else if (matrix[newRow][newCol] == 'T') {
- newCol = colPlayer;
- }
- }
- public static void moveLeft(char[][] matrix, int rowPlayer, int colPlayer) {
- if (isOutOfBounds(newRow, newCol, matrix)) {
- newCol = matrix[rowPlayer].length - 1;
- }
- if (matrix[newRow][newCol] == 'B') {
- newCol = newCol - 1;
- if (isOutOfBounds(newRow, newCol, matrix)) {
- newCol = matrix[rowPlayer].length - 1;
- }
- } else if (matrix[newRow][newCol] == 'T') {
- newCol = colPlayer;
- }
- }
- public static void moveUp(char[][] matrix, int rowPlayer) {
- if (newRow< 0){
- newRow = matrix.length - 1;
- }
- if (matrix[newRow][newCol] == 'B') {
- newRow = newRow - 1;
- } else if (matrix[newRow][newCol] == 'T') {
- newRow = rowPlayer;
- }
- }
- public static void moveDown(char[][] matrix, int rowPlayer) {
- if (newRow >= matrix.length) {
- newRow = 0;
- }
- if (matrix[newRow][newCol] == 'B') {
- newRow = newRow + 1;
- if (newRow >= matrix.length) {
- newRow = 0;
- }
- } else if (matrix[newRow][newCol] == 'T') {
- newRow = rowPlayer;
- }
- }
- public static void addCharArrayAsRowInMatrix(char[][] matrix, int i, String input) {
- char[] matrixRow = input.toCharArray();
- matrix[i] = matrixRow;
- }
- private static boolean isOutOfBounds(int row, int col, char[][] matrix) {
- return row < 0 || row >= matrix.length
- || col < 0 || col >= matrix[row].length;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement