Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Advance.Exams.JavaAdvancedExam.February2023;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- public class BlindManBuff {
- private static final char PLAYER = 'B';
- private static final char OPPONENTS = 'P';
- private static final char OBSTACLE = 'O';
- private static final char EMPTY_SPOT = '-';
- public static void main(String[] args) throws IOException {
- BufferedReader scanner = new BufferedReader(new InputStreamReader(System.in));
- char[][] field = new char[Integer.parseInt(scanner.readLine().split("\\s")[0])][];
- fillField(field, scanner);
- int[] currentPosition = getCurrentPosition(field);
- int moves = 0;
- int touchedPlayers = 0;
- String command;
- while (touchedPlayers != 3 && !"Finish".equals(command = scanner.readLine())) {
- switch (command) {
- case "up":
- if (!(currentPosition[0]-1 < 0)) {
- if (field[currentPosition[0] - 1][currentPosition[1]] == OPPONENTS) {
- touchedPlayers++;
- moves++;
- field[currentPosition[0]][currentPosition[1]] = EMPTY_SPOT;
- field[currentPosition[0] -1][currentPosition[1]] = PLAYER;
- currentPosition[0] = currentPosition[0] - 1;
- } else if (field[currentPosition[0] - 1][currentPosition[1]] == EMPTY_SPOT) {
- moves++;
- field[currentPosition[0]][currentPosition[1]] = EMPTY_SPOT;
- field[currentPosition[0] - 1][currentPosition[1]] = PLAYER;
- currentPosition[0] = currentPosition[0] - 1;
- }
- }
- break;
- case "down":
- if (!(currentPosition[0]+1 > field.length - 1)) {
- if (field[currentPosition[0] + 1][currentPosition[1]] == OPPONENTS) {
- touchedPlayers++;
- moves++;
- field[currentPosition[0]][currentPosition[1]] = EMPTY_SPOT;
- field[currentPosition[0] + 1][currentPosition[1]] = PLAYER;
- currentPosition[0] = currentPosition[0] + 1;
- } else if (field[currentPosition[0] + 1][currentPosition[1]] == EMPTY_SPOT) {
- moves++;
- field[currentPosition[0]][currentPosition[1]] = EMPTY_SPOT;
- field[currentPosition[0] + 1][currentPosition[1]] = PLAYER;
- currentPosition[0] = currentPosition[0] + 1;
- }
- }
- break;
- case "right":
- if (!(currentPosition[1]+1 > field[0].length - 1)) {
- if (field[currentPosition[0]][currentPosition[1] + 1] == OPPONENTS) {
- touchedPlayers++;
- moves++;
- field[currentPosition[0]][currentPosition[1]] = EMPTY_SPOT;
- field[currentPosition[0]][currentPosition[1] + 1] = PLAYER;
- currentPosition[1] = currentPosition[1] + 1;
- } else if (field[currentPosition[0]][currentPosition[1] + 1] == EMPTY_SPOT) {
- moves++;
- field[currentPosition[0]][currentPosition[1]] = EMPTY_SPOT;
- field[currentPosition[0]][currentPosition[1] + 1] = PLAYER;
- currentPosition[1] = currentPosition[1] + 1;
- }
- }
- break;
- case "left":
- if (!(currentPosition[1]-1 < 0)) {
- if (field[currentPosition[0]][currentPosition[1] - 1] == OPPONENTS) {
- touchedPlayers++;
- moves++;
- field[currentPosition[0]][currentPosition[1]] = EMPTY_SPOT;
- field[currentPosition[0]][currentPosition[1] - 1] = PLAYER;
- currentPosition[1] = currentPosition[1] - 1;
- } else if (field[currentPosition[0]][currentPosition[1] - 1] == EMPTY_SPOT) {
- moves++;
- field[currentPosition[0]][currentPosition[1]] = EMPTY_SPOT;
- field[currentPosition[0]][currentPosition[1] - 1] = PLAYER;
- currentPosition[1] = currentPosition[1] - 1;
- }
- }
- break;
- }
- }
- System.out.println("Game over!");
- System.out.printf("Touched opponents: %d Moves made: %d%n", touchedPlayers, moves);
- }
- private static int[] getCurrentPosition(char[][] field) {
- int row = 0;
- int col = 0;
- for (int rows = 0; rows < field.length; rows++) {
- for (int cols = 0; cols < field[rows].length; cols++) {
- if (field[rows][cols] == PLAYER) {
- row = rows;
- col = cols;
- return new int[] {row, col};
- }
- }
- }
- return new int[] {row, col};
- }
- private static void fillField(char[][] field, BufferedReader scanner) throws IOException {
- for (int i = 0; i < field.length; i++) {
- char[] newOne = scanner.readLine().replaceAll("\\s", "").toCharArray();
- field[i] = newOne;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment