Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Exam16April;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- public class HelensAbduction {
- public static void main(String[] args) throws IOException {
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- int energy = Integer.parseInt(reader.readLine());
- int rows = Integer.parseInt(reader.readLine());
- String[][] matrix = new String[rows][];
- int parisRow = -1;
- int parisCol = -1;
- int helenRow = -1;
- int helenCol = -1;
- for (int i = 0; i < rows; i++) {
- String[] line = reader.readLine().split("");
- for (int j = 0; j < line.length; j++) {
- String s = line[j];
- if (s.equals("P")) {
- parisRow = i;
- parisCol = j;
- }
- if (s.equals("H")) {
- helenRow = i;
- helenCol = j;
- }
- }
- matrix[i] = line;
- }
- while (true) {
- String[] cmdArgs = reader.readLine().split(" ");
- String cmdMove = cmdArgs[0];
- int rowSpawnEnemy = Integer.parseInt(cmdArgs[1]);
- int colSpawnEnemy = Integer.parseInt(cmdArgs[2]);
- matrix[rowSpawnEnemy][colSpawnEnemy] = "S";
- int parisRowTemp = parisRow;
- int parisColTemp = parisCol;
- if ("up".equals(cmdMove)) {
- parisRow = moveUp(parisRow);
- } else if ("down".equals(cmdMove)) {
- parisRow = moveDown(parisRow, matrix.length-1);
- } else if ("right".equals(cmdMove)) {
- parisCol = moveRight(parisCol, matrix[0].length-1);
- } else if ("left".equals(cmdMove)) {
- parisCol = moveLeft(parisCol);
- }
- energy--;
- if (parisRow != parisRowTemp || parisCol != parisColTemp){
- matrix[parisRowTemp][parisColTemp] = "-";
- if (matrix[parisRow][parisCol].equals("H")) {
- matrix[parisRow][parisCol] = "-";
- System.out.printf("Paris has successfully abducted Helen! Energy left: %d%n", energy);
- printMatrix(matrix);
- return;
- }
- if (energy <= 0) {
- System.out.printf("Paris died at %d;%d.%n", parisRow, parisCol);
- matrix[parisRow][parisCol] = "X";
- printMatrix(matrix);
- return;
- }
- if (matrix[parisRow][parisCol].equals("S")) {
- energy-=2;
- if (energy - 2 <= 0) {
- matrix[parisRow][parisCol] = "X";
- System.out.printf("Paris died at %d;%d.%n", parisRow, parisCol);
- printMatrix(matrix);
- return;
- } else {
- matrix[parisRow][parisCol] = "P";
- }
- }
- matrix[parisRow][parisCol] = "P";
- }
- if (energy <= 0) {
- System.out.printf("Paris died at %d;%d.%n", parisRow, parisCol);
- printMatrix(matrix);
- return;
- }
- }
- }
- private static int moveLeft(int currentCol) {
- if (currentCol == 0) {
- currentCol = 0;
- } else {
- currentCol--;
- }
- return currentCol;
- }
- private static int moveUp(int currentRow) {
- if (currentRow == 0) {
- currentRow = 0;
- } else {
- currentRow--;
- }
- return currentRow;
- }
- private static int moveRight(int currentCol, int matrixColLength) {
- if (currentCol == matrixColLength) {
- currentCol = matrixColLength;
- } else {
- currentCol++;
- }
- return currentCol;
- }
- private static int moveDown(int currentRow, int matrixLength) {
- if (currentRow == matrixLength) {
- currentRow = matrixLength;
- } else {
- currentRow++;
- }
- return currentRow;
- }
- private static void printMatrix(String[][] matrix) {
- for (String[] strings : matrix) {
- for (String string : strings) {
- System.out.print(string);
- }
- System.out.println();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement