borovaneca

Blind Man's Buff

Jun 3rd, 2023
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.74 KB | None | 0 0
  1. package Advance.Exams.JavaAdvancedExam.February2023;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6.  
  7. public class BlindManBuff {
  8.     private static final char PLAYER = 'B';
  9.     private static final char OPPONENTS = 'P';
  10.     private static final char OBSTACLE = 'O';
  11.     private static final char EMPTY_SPOT = '-';
  12.     public static void main(String[] args) throws IOException {
  13.         BufferedReader scanner = new BufferedReader(new InputStreamReader(System.in));
  14.  
  15.         char[][] field = new char[Integer.parseInt(scanner.readLine().split("\\s")[0])][];
  16.         fillField(field, scanner);
  17.         int[] currentPosition = getCurrentPosition(field);
  18.         int moves = 0;
  19.         int touchedPlayers = 0;
  20.  
  21.         String command;
  22.         while (touchedPlayers != 3 && !"Finish".equals(command = scanner.readLine())) {
  23.  
  24.             switch (command) {
  25.                 case "up":
  26.                     if (!(currentPosition[0]-1 < 0)) {
  27.                         if (field[currentPosition[0] - 1][currentPosition[1]] == OPPONENTS) {
  28.                             touchedPlayers++;
  29.                             moves++;
  30.                             field[currentPosition[0]][currentPosition[1]] = EMPTY_SPOT;
  31.                             field[currentPosition[0] -1][currentPosition[1]] = PLAYER;
  32.                             currentPosition[0] = currentPosition[0] - 1;
  33.  
  34.                         } else if (field[currentPosition[0] - 1][currentPosition[1]] == EMPTY_SPOT) {
  35.                             moves++;
  36.                             field[currentPosition[0]][currentPosition[1]] = EMPTY_SPOT;
  37.                             field[currentPosition[0] - 1][currentPosition[1]] = PLAYER;
  38.                             currentPosition[0] = currentPosition[0] - 1;
  39.  
  40.                         }
  41.                     }
  42.  
  43.                     break;
  44.                 case "down":
  45.                     if (!(currentPosition[0]+1 > field.length - 1)) {
  46.                         if (field[currentPosition[0] + 1][currentPosition[1]] == OPPONENTS) {
  47.                             touchedPlayers++;
  48.                             moves++;
  49.                             field[currentPosition[0]][currentPosition[1]] = EMPTY_SPOT;
  50.                             field[currentPosition[0] + 1][currentPosition[1]] = PLAYER;
  51.                             currentPosition[0] = currentPosition[0] + 1;
  52.  
  53.                         } else if (field[currentPosition[0] + 1][currentPosition[1]] == EMPTY_SPOT) {
  54.                             moves++;
  55.                             field[currentPosition[0]][currentPosition[1]] = EMPTY_SPOT;
  56.                             field[currentPosition[0] + 1][currentPosition[1]] = PLAYER;
  57.                             currentPosition[0] = currentPosition[0] + 1;
  58.                         }
  59.                     }
  60.                     break;
  61.                 case "right":
  62.                     if (!(currentPosition[1]+1 > field[0].length - 1)) {
  63.                         if (field[currentPosition[0]][currentPosition[1] + 1] == OPPONENTS) {
  64.                             touchedPlayers++;
  65.                             moves++;
  66.                             field[currentPosition[0]][currentPosition[1]] = EMPTY_SPOT;
  67.                             field[currentPosition[0]][currentPosition[1] + 1] = PLAYER;
  68.                             currentPosition[1] = currentPosition[1] + 1;
  69.  
  70.                         } else if (field[currentPosition[0]][currentPosition[1] + 1] == EMPTY_SPOT) {
  71.                             moves++;
  72.                             field[currentPosition[0]][currentPosition[1]] = EMPTY_SPOT;
  73.                             field[currentPosition[0]][currentPosition[1] + 1] = PLAYER;
  74.                             currentPosition[1] = currentPosition[1] + 1;
  75.                         }
  76.                     }
  77.                     break;
  78.                 case "left":
  79.                     if (!(currentPosition[1]-1 < 0)) {
  80.                         if (field[currentPosition[0]][currentPosition[1] - 1] == OPPONENTS) {
  81.                             touchedPlayers++;
  82.                             moves++;
  83.                             field[currentPosition[0]][currentPosition[1]] = EMPTY_SPOT;
  84.                             field[currentPosition[0]][currentPosition[1] - 1] = PLAYER;
  85.                             currentPosition[1] = currentPosition[1] - 1;
  86.  
  87.                         } else if (field[currentPosition[0]][currentPosition[1] - 1] == EMPTY_SPOT) {
  88.                             moves++;
  89.                             field[currentPosition[0]][currentPosition[1]] = EMPTY_SPOT;
  90.                             field[currentPosition[0]][currentPosition[1] - 1] = PLAYER;
  91.                             currentPosition[1] = currentPosition[1] - 1;
  92.                         }
  93.                     }
  94.                     break;
  95.             }
  96.  
  97.         }
  98.  
  99.         System.out.println("Game over!");
  100.         System.out.printf("Touched opponents: %d Moves made: %d%n", touchedPlayers, moves);
  101.     }
  102.  
  103.     private static int[] getCurrentPosition(char[][] field) {
  104.         int row = 0;
  105.         int col = 0;
  106.         for (int rows = 0; rows < field.length; rows++) {
  107.             for (int cols = 0; cols < field[rows].length; cols++) {
  108.                 if (field[rows][cols] == PLAYER) {
  109.                     row = rows;
  110.                     col = cols;
  111.                     return new int[] {row, col};
  112.                 }
  113.             }
  114.         }
  115.         return new int[] {row, col};
  116.     }
  117.  
  118.     private static void fillField(char[][] field, BufferedReader scanner) throws IOException {
  119.         for (int i = 0; i < field.length; i++) {
  120.             char[] newOne = scanner.readLine().replaceAll("\\s", "").toCharArray();
  121.             field[i] = newOne;
  122.         }
  123.     }
  124. }
  125.  
Advertisement
Add Comment
Please, Sign In to add comment