Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.io.IOException;
- import java.util.*;
- import java.util.stream.Collectors;
- public class Main {
- public static int energy;
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- energy = Integer.parseInt(scanner.nextLine());
- int rows = Integer.parseInt(scanner.nextLine());
- char[][] field = new char[rows][];//правим си матрица от char
- //пазим индекси за позициите на Парис и Елена
- int parisRow = 0;
- int parisCol = 0;
- int helenaRow = 0;
- int helenaCol = 0;
- for (int i = 0; i < rows; i++) {
- // field[i]=scanner.nextLine().toCharArray();//четем редовете, но елементите са без space за това:
- //за да можем да намерим позицията на PAris i Helen минаваме по всеки ред
- String line = scanner.nextLine();
- field[i] = line.toCharArray();//чета редовете
- if (line.contains("P")) {//намираме индексите, на които се намират:
- parisRow = i; //и на тзи ред и на индекса на който се намира са ни координатите
- parisCol = line.indexOf("P");
- }
- if (line.contains("H")) {
- helenaRow = i;
- helenaCol = line.indexOf("H");
- }
- }
- boolean isAbducted = false;
- while (energy > 0 && !isAbducted) {
- String direction = scanner.next();
- int row = scanner.nextInt();
- int col = scanner.nextInt();
- field[row][col] = 'S';// това са индексите на спартанците от конзолата
- //за всички if ни трябва проверка дали може да се мести, т.е дали е в границите, за това метод:
- if (direction.equals("up") && CanMove(parisRow - 1, parisCol, field)) {
- field[parisRow][parisCol] = '-';
- parisRow--;
- isAbducted = moveParis(parisRow, parisCol, field);
- field[parisRow][parisCol] = 'P';
- } else if (direction.equals("down") && CanMove(parisRow + 1, parisCol, field)) {
- field[parisRow][parisCol] = '-';
- parisRow++;
- isAbducted = moveParis(parisRow, parisCol, field);
- field[parisRow][parisCol] = 'P';
- } else if (direction.equals("left") && CanMove(parisRow, parisCol - 1, field)) {
- field[parisRow][parisCol] = '-';
- parisCol--;
- isAbducted = moveParis(parisRow, parisCol, field);
- field[parisRow][parisCol] = 'P';
- } else if (direction.equals("right") && CanMove(parisRow, parisCol + 1, field)) {
- field[parisRow][parisCol] = '-';
- parisCol++;
- isAbducted = moveParis(parisRow, parisCol, field);
- field[parisRow][parisCol] = 'P';
- }
- energy--;
- }
- if (isAbducted) {
- field[parisRow][parisCol] = '-';
- System.out.println("Paris has successfully abducted Helen! Energy left: " + energy);
- } else {
- field[parisRow][parisCol] = 'X';
- System.out.println(String.format("Paris died at %d;%d.", parisRow, parisCol));
- }
- printField(field);
- }
- public static boolean moveParis(int newRow, int newCol, char[][] field) {
- boolean isAbducted = false;
- if (field[newRow][newCol] == 'S') {
- energy -= 2;
- } else if (field[newRow][newCol] == 'H') {
- isAbducted = true;
- }
- return isAbducted;
- }
- private static boolean CanMove(int newRow, int newCol, char[][] field) {
- return newRow >= 0 && newRow < field.length && newCol >= 0 && newCol < field[newRow].length;
- }
- private static void printField(char[][] field) {
- for (int r = 0; r < field.length; r++) {//първо до броя на редовете
- for (int c = 0; c < field[r].length; c++) {
- System.out.print(field[r][c]);
- }
- System.out.println();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement