Advertisement
Didart

Radioactive Mutant Vampire Bunnies

Jan 14th, 2023
772
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.20 KB | None | 0 0
  1. package MultidimensionalArrays2;
  2.  
  3. import java.util.ArrayDeque;
  4. import java.util.Arrays;
  5. import java.util.Scanner;
  6.  
  7. public class RadioactiveMutantVampireBunnies {
  8.  
  9.     private static final int[] playerPosition = new int[2];
  10.     private static final int[] rowMovement = {1, -1, 0, 0};
  11.     private static final int[] colMovement = {0, 0, 1, -1};
  12.     private static boolean isPlayerDead = false;
  13.  
  14.     public static void main(String[] args) {
  15.         Scanner scanner = new Scanner(System.in);
  16.  
  17.         int[] dimensions = Arrays
  18.                 .stream(scanner.nextLine().split("\\s+"))
  19.                 .mapToInt(Integer::parseInt)
  20.                 .toArray();
  21.  
  22.         int rows = dimensions[0];
  23.         int cols = dimensions[1];
  24.  
  25.         char[][] matrix = new char[rows][cols];
  26.  
  27.         ArrayDeque<Integer> positionQueue = new ArrayDeque<>();
  28.  
  29.         for (int row = 0; row < matrix.length; row++) {
  30.             String[] input = scanner.nextLine().split("");
  31.  
  32.             for (int col = 0; col < matrix[row].length; col++) {
  33.                 matrix[row][col] = input[col].charAt(0);
  34.  
  35.                 if (matrix[row][col] == 'B') {
  36.                     positionQueue.offer(row);
  37.                     positionQueue.offer(col);
  38.                 }
  39.                 if (matrix[row][col] == 'P') {
  40.                     playerPosition[0] = row;
  41.                     playerPosition[1] = col;
  42.                     matrix[row][col] = '.';
  43.                 }
  44.             }
  45.         }
  46.  
  47.         String[] data = scanner.nextLine().split("");
  48.  
  49.         int counter = -1;
  50.         while (++counter < data.length && !isPlayerDead) {
  51.             String command = data[counter];
  52.  
  53.             moveBunny(matrix, positionQueue);
  54.  
  55.             int playerRow = playerPosition[0];
  56.             int playerCol = playerPosition[1];
  57.  
  58.             switch (command) {
  59.                 case "R":
  60.                     playerCol += 1;
  61.                     break;
  62.                 case "L":
  63.                     playerCol -= 1;
  64.                     break;
  65.                 case "U":
  66.                     playerRow -= 1;
  67.                     break;
  68.                 case "D":
  69.                     playerRow += 1;
  70.                     break;
  71.             }
  72.  
  73.             if (inBounds(playerRow, playerCol, matrix)) {
  74.                 break;
  75.             }
  76.             playerPosition[0] = playerRow;
  77.             playerPosition[1] = playerCol;
  78.  
  79.             if (!isCellFree(playerRow, playerCol, matrix)) {
  80.                 isPlayerDead = true;
  81.                 break;
  82.             }
  83.         }
  84.  
  85.         printSymbolMatrix(matrix);
  86.         if (isPlayerDead) {
  87.             System.out.print("dead: ");
  88.         } else {
  89.             System.out.print("won: ");
  90.         }
  91.         System.out.println(playerPosition[0] + " " + playerPosition[1]);
  92.     }
  93.  
  94.     private static void printSymbolMatrix(char[][] finalMatrix) {
  95.         for (char[] matrix : finalMatrix) {
  96.             for (char symbol : matrix) {
  97.                 System.out.print(symbol);
  98.             }
  99.             System.out.println();
  100.         }
  101.     }
  102.  
  103.     private static void moveBunny(char[][] matrix, ArrayDeque<Integer> queueOfPositions) {
  104.         int lengthOperations = queueOfPositions.size() / 2;
  105.         for (int i = 0; i < lengthOperations; i++) {
  106.             int row = queueOfPositions.remove();
  107.             int col = queueOfPositions.remove();
  108.             for (int j = 0; j < rowMovement.length; j++) {
  109.                 int newRow = row + rowMovement[j];
  110.                 int newCol = col + colMovement[j];
  111.  
  112.                 if (inBounds(newRow, newCol, matrix)) {
  113.                     continue;
  114.                 }
  115.                 if (matrix[newRow][newCol] == 'B') {
  116.                     continue;
  117.                 }
  118.                 matrix[newRow][newCol] = 'B';
  119.                 queueOfPositions.offer(newRow);
  120.                 queueOfPositions.offer(newCol);
  121.             }
  122.         }
  123.     }
  124.  
  125.     private static boolean isCellFree(int row, int col, char[][] matrix) {
  126.         return matrix[row][col] == '.';
  127.     }
  128.  
  129.     private static boolean inBounds(int row, int col, char[][] matrix) {
  130.         return row < 0 || row >= matrix.length || col < 0 || col >= matrix[row].length;
  131.     }
  132. }
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement