Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package JavaAdvancedExam16April2019.ex1;
- import java.util.Scanner;
- public class matrixMainex2 {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int energy = Integer.parseInt(scanner.nextLine());
- int matrixSize = Integer.parseInt(scanner.nextLine());
- int parisRow = 0;
- int parisCol = 0;
- char[][] matrix = new char[matrixSize][];
- for (int i = 0; i < matrix.length; i++) {
- matrix[i] = scanner.nextLine().toCharArray();
- for (int j = 0; j < matrix[i].length; j++) {
- if (matrix[i][j] == 'P') {
- parisRow = i;
- parisCol = j;
- }
- }
- }
- boolean findHelen = true;
- matrix[parisRow][parisCol] = '-';
- while (energy > 0 && findHelen) {
- String input = scanner.nextLine();
- String[] tokens = input.split(" ");
- String direction = tokens[0];
- int spawnRow = Integer.parseInt(tokens[1]);
- int spawnCol = Integer.parseInt(tokens[2]);
- matrix[spawnRow][spawnCol] = 'S';
- switch (direction) {
- case "up":
- if (checkRangeUp(parisRow, matrix)) {
- parisRow -= 1;
- energy -= 1;
- if (energy <= 0) {
- matrix[parisRow][parisCol] = 'X';
- System.out.printf("Paris died at %d;%d.%n", parisRow, parisCol);
- findHelen = false;
- } else if (matrix[parisRow][parisCol] == 'H') {
- System.out.print("Paris has successfully abducted Helen! ");
- System.out.printf("Energy left: %d%n", energy);
- matrix[parisRow][parisCol] = '-';
- findHelen = false;
- } else if (matrix[parisRow][parisCol] == 'S') {
- energy -= 2;
- if (energy <= 0) {
- matrix[parisRow][parisCol] = 'X';
- System.out.printf("Paris died at %d;%d.%n", parisRow, parisCol);
- findHelen = false;
- } else {
- matrix[parisRow][parisCol] = '-';
- }
- }
- } else {
- energy -= 1;
- if (energy <= 0) {
- matrix[parisRow][parisCol] = 'X';
- System.out.printf("Paris died at %d;%d.%n", parisRow, parisCol);
- findHelen = false;
- }
- }
- break;
- case "down":
- if (checkRangeDown(parisRow, matrix)) {
- parisRow += 1;
- energy -= 1;
- if (energy <= 0) {
- matrix[parisRow][parisCol] = 'X';
- System.out.printf("Paris died at %d;%d.%n", parisRow, parisCol);
- findHelen = false;
- } else if (matrix[parisRow][parisCol] == 'H') {
- System.out.print("Paris has successfully abducted Helen! ");
- System.out.printf("Energy left: %d%n", energy);
- matrix[parisRow][parisCol] = '-';
- findHelen = false;
- } else if (matrix[parisRow][parisCol] == 'S') {
- energy -= 2;
- if (energy <= 0) {
- matrix[parisRow][parisCol] = 'X';
- System.out.printf("Paris died at %d;%d.%n", parisRow, parisCol);
- findHelen = false;
- } else {
- matrix[parisRow][parisCol] = '-';
- }
- }
- } else {
- energy -= 1;
- if (energy <= 0) {
- matrix[parisRow][parisCol] = 'X';
- System.out.printf("Paris died at %d;%d.%n", parisRow, parisCol);
- findHelen = false;
- }
- }
- break;
- case "right":
- if (checkRangeRight(parisCol, matrix)) {
- parisCol += 1;
- energy -= 1;
- if (energy <= 0) {
- matrix[parisRow][parisCol] = 'X';
- System.out.printf("Paris died at %d;%d.%n", parisRow, parisCol);
- findHelen = false;
- } else if (matrix[parisRow][parisCol] == 'H') {
- System.out.print("Paris has successfully abducted Helen! ");
- System.out.printf("Energy left: %d%n", energy);
- matrix[parisRow][parisCol] = '-';
- findHelen = false;
- } else if (matrix[parisRow][parisCol] == 'S') {
- energy -= 2;
- if (energy <= 0) {
- matrix[parisRow][parisCol] = 'X';
- System.out.printf("Paris died at %d;%d.%n", parisRow, parisCol);
- findHelen = false;
- } else {
- matrix[parisRow][parisCol] = '-';
- }
- }
- } else {
- energy -= 1;
- if (energy <= 0) {
- matrix[parisRow][parisCol] = 'X';
- System.out.printf("Paris died at %d;%d.%n", parisRow, parisCol);
- findHelen = false;
- }
- }
- break;
- case "left":
- if (checkRangeLeft(parisCol, matrix)) {
- parisCol -= 1;
- energy -= 1;
- if (energy <= 0) {
- matrix[parisRow][parisCol] = 'X';
- System.out.printf("Paris died at %d;%d.%n", parisRow, parisCol);
- findHelen = false;
- } else if (matrix[parisRow][parisCol] == 'H') {
- System.out.print("Paris has successfully abducted Helen! ");
- System.out.printf("Energy left: %d%n", energy);
- matrix[parisRow][parisCol] = '-';
- findHelen = false;
- } else if (matrix[parisRow][parisCol] == 'S') {
- energy -= 2;
- if (energy <= 0) {
- matrix[parisRow][parisCol] = 'X';
- System.out.printf("Paris died at %d;%d.%n", parisRow, parisCol);
- findHelen = false;
- } else {
- matrix[parisRow][parisCol] = '-';
- }
- }
- } else {
- energy -= 1;
- if (energy <= 0) {
- matrix[parisRow][parisCol] = 'X';
- System.out.printf("Paris died at %d;%d.%n", parisRow, parisCol);
- findHelen = false;
- }
- }
- break;
- }
- }
- 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 checkRangeUp(int parisRow, char[][] matirx) {
- parisRow--;
- return parisRow >= 0 && parisRow < matirx.length;
- }
- private static boolean checkRangeDown(int parisRow, char[][] matirx) {
- parisRow++;
- return parisRow >= 0 && parisRow < matirx.length;
- }
- private static boolean checkRangeRight(int parisCol, char[][] matirx) {
- parisCol++;
- return parisCol >= 0 && parisCol < matirx.length;
- }
- private static boolean checkRangeLeft(int parisCol, char[][] matirx) {
- parisCol--;
- return parisCol >= 0 && parisCol < matirx.length;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement