Advertisement
IrinaIgnatova

Matrix-Helen's Abduction

Oct 25th, 2019
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.46 KB | None | 0 0
  1. package com.company;
  2.  
  3.  
  4. import java.io.IOException;
  5.  
  6. import java.util.*;
  7. import java.util.stream.Collectors;
  8.  
  9.  
  10. public class Main {
  11.     public static int energy;
  12.  
  13.     public static void main(String[] args) {
  14.         Scanner scanner = new Scanner(System.in);
  15.  
  16.          energy = Integer.parseInt(scanner.nextLine());
  17.         int rows = Integer.parseInt(scanner.nextLine());
  18.  
  19.         char[][] field = new char[rows][];//правим си матрица от char
  20.         //пазим индекси за позициите на Парис и Елена
  21.         int parisRow = 0;
  22.         int parisCol = 0;
  23.         int helenaRow = 0;
  24.         int helenaCol = 0;
  25.  
  26.         for (int i = 0; i < rows; i++) {
  27.             // field[i]=scanner.nextLine().toCharArray();//четем редовете, но елементите са без space за това:
  28.             //за да можем да намерим позицията на PAris i Helen минаваме по всеки ред
  29.             String line = scanner.nextLine();
  30.             field[i] = line.toCharArray();//чета редовете
  31.             if (line.contains("P")) {//намираме индексите, на които се намират:
  32.                 parisRow = i; //и на тзи ред и на индекса на който се намира са ни координатите
  33.                 parisCol = line.indexOf("P");
  34.             }
  35.             if (line.contains("H")) {
  36.                 helenaRow = i;
  37.                 helenaCol = line.indexOf("H");
  38.             }
  39.         }
  40.  
  41.  
  42.         boolean isAbducted = false;
  43.  
  44.         while (energy > 0 && !isAbducted) {
  45.  
  46.             String direction = scanner.next();
  47.             int row = scanner.nextInt();
  48.             int col = scanner.nextInt();
  49.  
  50.             field[row][col] = 'S';// това са индексите на спартанците от конзолата
  51.  
  52.  
  53.             //за всички if ни трябва проверка дали може да се мести, т.е дали е в границите, за това метод:
  54.  
  55.             if (direction.equals("up") && CanMove(parisRow - 1, parisCol, field)) {
  56.  
  57.                 field[parisRow][parisCol] = '-';
  58.                 parisRow--;
  59.                 isAbducted = moveParis(parisRow, parisCol, field);
  60.  
  61.                 field[parisRow][parisCol] = 'P';
  62.  
  63.             } else if (direction.equals("down") && CanMove(parisRow + 1, parisCol, field)) {
  64.                 field[parisRow][parisCol] = '-';
  65.                 parisRow++;
  66.                 isAbducted = moveParis(parisRow, parisCol, field);
  67.  
  68.                 field[parisRow][parisCol] = 'P';
  69.  
  70.  
  71.             } else if (direction.equals("left") && CanMove(parisRow, parisCol - 1, field)) {
  72.                 field[parisRow][parisCol] = '-';
  73.                 parisCol--;
  74.                 isAbducted = moveParis(parisRow, parisCol, field);
  75.  
  76.                 field[parisRow][parisCol] = 'P';
  77.  
  78.  
  79.             } else if (direction.equals("right") && CanMove(parisRow, parisCol + 1, field)) {
  80.                 field[parisRow][parisCol] = '-';
  81.                 parisCol++;
  82.                 isAbducted = moveParis(parisRow, parisCol, field);
  83.  
  84.                 field[parisRow][parisCol] = 'P';
  85.  
  86.             }
  87.  
  88.             energy--;
  89.         }
  90.  
  91.         if (isAbducted) {
  92.             field[parisRow][parisCol] = '-';
  93.             System.out.println("Paris has successfully abducted Helen! Energy left: " + energy);
  94.         } else {
  95.             field[parisRow][parisCol] = 'X';
  96.             System.out.println(String.format("Paris died at %d;%d.", parisRow, parisCol));
  97.         }
  98.  
  99.         printField(field);
  100.  
  101.     }
  102.  
  103.     public static boolean moveParis(int newRow, int newCol, char[][] field) {
  104.         boolean isAbducted = false;
  105.         if (field[newRow][newCol] == 'S') {
  106.             energy -= 2;
  107.         } else if (field[newRow][newCol] == 'H') {
  108.             isAbducted = true;
  109.         }
  110.         return isAbducted;
  111.     }
  112.  
  113.     private static boolean CanMove(int newRow, int newCol, char[][] field) {
  114.         return newRow >= 0 && newRow < field.length && newCol >= 0 && newCol < field[newRow].length;
  115.     }
  116.  
  117.     private static void printField(char[][] field) {
  118.         for (int r = 0; r < field.length; r++) {//първо до броя на редовете
  119.             for (int c = 0; c < field[r].length; c++) {
  120.                 System.out.print(field[r][c]);
  121.             }
  122.             System.out.println();
  123.         }
  124.     }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement