Advertisement
emodev

Untitled

Apr 16th, 2019
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.52 KB | None | 0 0
  1. package Exam16April;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6.  
  7. public class HelensAbduction {
  8.     public static void main(String[] args) throws IOException {
  9.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  10.         int energy = Integer.parseInt(reader.readLine());
  11.  
  12.         int rows = Integer.parseInt(reader.readLine());
  13.  
  14.         String[][] matrix = new String[rows][];
  15.  
  16.         int parisRow = -1;
  17.         int parisCol = -1;
  18.  
  19.         int helenRow = -1;
  20.         int helenCol = -1;
  21.  
  22.  
  23.         for (int i = 0; i < rows; i++) {
  24.             String[] line = reader.readLine().split("");
  25.             for (int j = 0; j < line.length; j++) {
  26.                 String s = line[j];
  27.                 if (s.equals("P")) {
  28.                     parisRow = i;
  29.                     parisCol = j;
  30.                 }
  31.                 if (s.equals("H")) {
  32.                     helenRow = i;
  33.                     helenCol = j;
  34.                 }
  35.             }
  36.             matrix[i] = line;
  37.         }
  38.  
  39.  
  40.         while (true) {
  41.             String[] cmdArgs = reader.readLine().split(" ");
  42.             String cmdMove = cmdArgs[0];
  43.             int rowSpawnEnemy = Integer.parseInt(cmdArgs[1]);
  44.             int colSpawnEnemy = Integer.parseInt(cmdArgs[2]);
  45.  
  46.             matrix[rowSpawnEnemy][colSpawnEnemy] = "S";
  47.  
  48.             int parisRowTemp = parisRow;
  49.             int parisColTemp = parisCol;
  50.  
  51.             if ("up".equals(cmdMove)) {
  52.                 parisRow = moveUp(parisRow);
  53.  
  54.             } else if ("down".equals(cmdMove)) {
  55.                 parisRow = moveDown(parisRow, matrix.length-1);
  56.             } else if ("right".equals(cmdMove)) {
  57.                 parisCol = moveRight(parisCol, matrix[0].length-1);
  58.             } else if ("left".equals(cmdMove)) {
  59.                 parisCol = moveLeft(parisCol);
  60.             }
  61.                 energy--;
  62.             if (parisRow != parisRowTemp || parisCol != parisColTemp){
  63.                 matrix[parisRowTemp][parisColTemp] = "-";
  64.                 if (matrix[parisRow][parisCol].equals("H")) {
  65.                     matrix[parisRow][parisCol] = "-";
  66.                     System.out.printf("Paris has successfully abducted Helen! Energy left: %d%n", energy);
  67.                     printMatrix(matrix);
  68.                     return;
  69.                 }
  70.                 if (energy <= 0) {
  71.                     System.out.printf("Paris died at %d;%d.%n", parisRow, parisCol);
  72.                     matrix[parisRow][parisCol] = "X";
  73.                     printMatrix(matrix);
  74.                     return;
  75.                 }
  76.                 if (matrix[parisRow][parisCol].equals("S")) {
  77.                     energy-=2;
  78.                     if (energy - 2 <= 0) {
  79.                         matrix[parisRow][parisCol] = "X";
  80.                         System.out.printf("Paris died at %d;%d.%n", parisRow, parisCol);
  81.                         printMatrix(matrix);
  82.                         return;
  83.                     } else {
  84.                         matrix[parisRow][parisCol] = "P";
  85.                     }
  86.                 }
  87.                 matrix[parisRow][parisCol] = "P";
  88.             }
  89.  
  90.  
  91.             if (energy <= 0) {
  92.                 System.out.printf("Paris died at %d;%d.%n", parisRow, parisCol);
  93.                 printMatrix(matrix);
  94.                 return;
  95.             }
  96.  
  97.         }
  98.  
  99.     }
  100.  
  101.  
  102.     private static int moveLeft(int currentCol) {
  103.         if (currentCol == 0) {
  104.             currentCol = 0;
  105.         } else {
  106.             currentCol--;
  107.         }
  108.         return currentCol;
  109.     }
  110.  
  111.     private static int moveUp(int currentRow) {
  112.         if (currentRow == 0) {
  113.             currentRow = 0;
  114.         } else {
  115.             currentRow--;
  116.         }
  117.         return currentRow;
  118.     }
  119.  
  120.     private static int moveRight(int currentCol, int matrixColLength) {
  121.         if (currentCol == matrixColLength) {
  122.             currentCol = matrixColLength;
  123.         } else {
  124.             currentCol++;
  125.         }
  126.         return currentCol;
  127.     }
  128.  
  129.     private static int moveDown(int currentRow, int matrixLength) {
  130.         if (currentRow == matrixLength) {
  131.             currentRow = matrixLength;
  132.         } else {
  133.             currentRow++;
  134.         }
  135.         return currentRow;
  136.     }
  137.  
  138.     private static void printMatrix(String[][] matrix) {
  139.         for (String[] strings : matrix) {
  140.             for (String string : strings) {
  141.                 System.out.print(string);
  142.             }
  143.             System.out.println();
  144.         }
  145.     }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement