borovaneca

Throne Conquering

May 28th, 2023
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.55 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4.  
  5. public class ThroneConquering {
  6.  
  7.     private static final char PARIS = 'P';
  8.     private static final char ENEMY = 'S';
  9.     private static final char HELEN = 'H';
  10.     private static final char DEAD = 'X';
  11.     private static final char EMPTY_SPOT = '-';
  12.  
  13.     public static void main(String[] args) throws IOException {
  14.         BufferedReader scanner = new BufferedReader(new InputStreamReader(System.in));
  15.  
  16.         int power = Integer.parseInt(scanner.readLine());
  17.         int rows = Integer.parseInt(scanner.readLine());
  18.         char[][] field = new char[rows][];
  19.         for (int i = 0; i < field.length; i++) {
  20.             field[i] = scanner.readLine().toCharArray();
  21.         }
  22.  
  23.         int[] currentPosition = getCurrentPosition(field);
  24.         boolean finished = false;
  25.  
  26.         String command = scanner.readLine();
  27.         while (power != 0 && !finished) {
  28.             String[] tokens = command.split("\\s+");
  29.             field[Integer.parseInt(tokens[1])][Integer.parseInt(tokens[2])] = ENEMY;
  30.             String move = tokens[0];
  31.             power--;
  32.             switch (move) {
  33.                 case "up":
  34.                     if (currentPosition[0] - 1 >= 0) {
  35.                         if (field[currentPosition[0] - 1][currentPosition[1]] == ENEMY) {
  36.                             field[currentPosition[0]][currentPosition[1]] = EMPTY_SPOT;
  37.                             power-=2;
  38.                             if (power <= 0) {
  39.                                 field[currentPosition[0] - 1][currentPosition[1]] = DEAD;
  40.                                 currentPosition[0] = currentPosition[0] - 1;
  41.                             } else {
  42.                                 field[currentPosition[0] - 1][currentPosition[1]] = PARIS;
  43.                                 currentPosition[0] = currentPosition[0] - 1;
  44.                             }
  45.                         } else if (field[currentPosition[0] - 1][currentPosition[1]] == HELEN) {
  46.                             field[currentPosition[0] - 1][currentPosition[1]] = EMPTY_SPOT;
  47.                             field[currentPosition[0]][currentPosition[1]] = EMPTY_SPOT;
  48.                             finished = true;
  49.  
  50.                         } else {
  51.                             field[currentPosition[0] - 1][currentPosition[1]] = PARIS;
  52.                             field[currentPosition[0]][currentPosition[1]] = EMPTY_SPOT;
  53.                             currentPosition[0] = currentPosition[0] - 1;
  54.                             if (power <= 0) {
  55.                                 field[currentPosition[0]][currentPosition[1]] = DEAD;
  56.                             }
  57.                         }
  58.                     }
  59.                     break;
  60.                 case "down":
  61.                     if (currentPosition[0] + 1 < field.length) {
  62.                         if (field[currentPosition[0] + 1][currentPosition[1]] == ENEMY) {
  63.                             field[currentPosition[0]][currentPosition[1]] = EMPTY_SPOT;
  64.                             power-=2;
  65.                             if (power <= 0) {
  66.                                 field[currentPosition[0] + 1][currentPosition[1]] = DEAD;
  67.                                 currentPosition[0] = currentPosition[0] + 1;
  68.                             } else {
  69.                                 field[currentPosition[0] + 1][currentPosition[1]] = PARIS;
  70.                                 currentPosition[0] = currentPosition[0] + 1;
  71.                             }
  72.                         } else if (field[currentPosition[0] + 1][currentPosition[1]] == HELEN) {
  73.                             field[currentPosition[0] + 1][currentPosition[1]] = EMPTY_SPOT;
  74.                             field[currentPosition[0]][currentPosition[1]] = EMPTY_SPOT;
  75.                             finished = true;
  76.  
  77.                         } else {
  78.                             field[currentPosition[0] + 1][currentPosition[1]] = PARIS;
  79.                             field[currentPosition[0]][currentPosition[1]] = EMPTY_SPOT;
  80.                             currentPosition[0] = currentPosition[0] + 1;
  81.                             if (power <= 0) {
  82.                                 field[currentPosition[0]][currentPosition[1]] = DEAD;
  83.                             }
  84.                         }
  85.                     }
  86.                     break;
  87.                 case "left":
  88.                     if (currentPosition[1] - 1 >= 0) {
  89.                         if (field[currentPosition[0]][currentPosition[1] - 1] == ENEMY) {
  90.                             field[currentPosition[0]][currentPosition[1]] = EMPTY_SPOT;
  91.                             power-=2;
  92.                             if (power <= 0) {
  93.                                 field[currentPosition[0]][currentPosition[1] - 1] = DEAD;
  94.                                 currentPosition[1] = currentPosition[1] - 1;
  95.                             } else {
  96.                                 field[currentPosition[0]][currentPosition[1] - 1] = PARIS;
  97.                                 currentPosition[1] = currentPosition[1] - 1;
  98.                             }
  99.                         } else if (field[currentPosition[0]][currentPosition[1] - 1] == HELEN) {
  100.                             field[currentPosition[0]][currentPosition[1] - 1] = EMPTY_SPOT;
  101.                             field[currentPosition[0]][currentPosition[1]] = EMPTY_SPOT;
  102.                             finished = true;
  103.  
  104.                         } else {
  105.                             field[currentPosition[0]][currentPosition[1] - 1] = PARIS;
  106.                             field[currentPosition[0]][currentPosition[1]] = EMPTY_SPOT;
  107.                             currentPosition[1] = currentPosition[1] - 1;
  108.                             if (power <= 0) {
  109.                                 field[currentPosition[0]][currentPosition[1]] = DEAD;
  110.                             }
  111.                         }
  112.                     }
  113.                     break;
  114.                 case "right":
  115.                     if (currentPosition[1] + 1 < field[0].length) {
  116.                         if (field[currentPosition[0]][currentPosition[1] + 1] == ENEMY) {
  117.                             field[currentPosition[0]][currentPosition[1]] = EMPTY_SPOT;
  118.                             power-=2;
  119.                             if (power <= 0) {
  120.                                 field[currentPosition[0]][currentPosition[1] + 1] = DEAD;
  121.                                 currentPosition[1] = currentPosition[1] + 1;
  122.                             } else {
  123.                                 field[currentPosition[0]][currentPosition[1] + 1] = PARIS;
  124.                                 currentPosition[1] = currentPosition[1] + 1;
  125.                             }
  126.                         } else if (field[currentPosition[0]][currentPosition[1] + 1] == HELEN) {
  127.                             field[currentPosition[0]][currentPosition[1] + 1] = EMPTY_SPOT;
  128.                             field[currentPosition[0]][currentPosition[1]] = EMPTY_SPOT;
  129.                             finished = true;
  130.  
  131.                         } else {
  132.                             field[currentPosition[0]][currentPosition[1] + 1] = PARIS;
  133.                             field[currentPosition[0]][currentPosition[1]] = EMPTY_SPOT;
  134.                             currentPosition[1] = currentPosition[1] + 1;
  135.                             if (power <= 0) {
  136.                                 field[currentPosition[0]][currentPosition[1]] = DEAD;
  137.                             }
  138.                         }
  139.                     }
  140.                     break;
  141.             }
  142.             if (!finished && power != 0) {
  143.                 command = scanner.readLine();
  144.             }
  145.         }
  146.  
  147.         if (finished) {
  148.             System.out.println("Paris has successfully abducted Helen! Energy left: " + power);
  149.  
  150.         } else if (power <= 0) {
  151.             System.out.printf("Paris died at %d;%d.%n", currentPosition[0], currentPosition[1]);
  152.         }
  153.         for (char[] chars : field) {
  154.             for (char aChar : chars) {
  155.                 System.out.print(aChar);
  156.             }
  157.             System.out.println();
  158.         }
  159.     }
  160.  
  161.     private static int[] getCurrentPosition(char[][] field) {
  162.         int[] position = new int[2];
  163.         for (int row = 0; row < field.length; row++) {
  164.             for (int col = 0; col < field[row].length; col++) {
  165.                 if (field[row][col] == PARIS) {
  166.                     position[0] = row;
  167.                     position[1] = col;
  168.                     return position;
  169.                 }
  170.             }
  171.         }
  172.         return position;
  173.     }
  174. }
Advertisement
Add Comment
Please, Sign In to add comment