Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.59 KB | None | 0 0
  1. package com.spand0x;
  2.  
  3. import java.util.Arrays;
  4. import java.util.Scanner;
  5.  
  6. public class HelensAbduction {
  7.     private static int parisRow = -1;
  8.     private static int parisCol = -1;
  9.     private static int energy = 0;
  10.  
  11.     public static void main(String[] args) {
  12.         Scanner scanner = new Scanner(System.in);
  13.         energy = Integer.parseInt(scanner.nextLine());
  14.         int size = Integer.parseInt(scanner.nextLine());
  15.         char[][] field = new char[size][size];
  16.         int helenRow = -1;
  17.         int helenCol = -1;
  18.         for (int i = 0; i < size; i++) {
  19.             char[] input = scanner.nextLine().toCharArray();
  20.             for (int j = 0; j < size; j++) {
  21.                 field[i][j] = input[j];
  22.                 if (input[j] == 'P') {
  23.                     parisRow = i;
  24.                     parisCol = j;
  25.                 } else if (input[j] == 'H') {
  26.                     helenRow = i;
  27.                     helenCol = j;
  28.                 }
  29.             }
  30.         }
  31.         boolean foundHelen = false;
  32.         while (energy > 0) {
  33.             String[] input = scanner.nextLine().split("\\s+");
  34.             String command = input[0];
  35.             int rowSpawn = Integer.parseInt(input[1]);
  36.             int colSpawn = Integer.parseInt(input[2]);
  37.             field[rowSpawn][colSpawn] = 'S';
  38.  
  39.  
  40.             switch (command) {
  41.                 case "up":
  42.                     if (parisRow == 0) {
  43.                         energy--;
  44.                     } else {
  45.                         field[parisRow][parisCol] = '-';
  46.                         parisRow -= 1;
  47.                         move(field);
  48.                     }
  49.                     break;
  50.                 case "down":
  51.                     if (parisRow == size - 1) {
  52.                         energy--;
  53.                     } else {
  54.                         field[parisRow][parisCol] = '-';
  55.                         parisRow += 1;
  56.                         move(field);
  57.                     }
  58.                     break;
  59.                 case "left":
  60.                     if (parisCol == 0) {
  61.                         energy--;
  62.                     } else {
  63.                         field[parisRow][parisCol] = '-';
  64.                         parisCol -= 1;
  65.                         move(field);
  66.                     }
  67.                     break;
  68.                 case "right":
  69.                     if (parisCol == size - 1) {
  70.                         energy--;
  71.                     } else {
  72.                         field[parisRow][parisCol] = '-';
  73.                         parisCol += 1;
  74.                         move(field);
  75.                     }
  76.                     break;
  77.             }
  78.             if (parisRow == helenRow && parisCol == helenCol) { //find where
  79.                 foundHelen = true;
  80.                 field[parisRow][parisCol] = '-';
  81.                 break;
  82.             }
  83.         }
  84.         if(foundHelen){
  85.             System.out.println("Paris has successfully abducted Helen! Energy left: " + energy);
  86.         }else {
  87.             System.out.printf("Paris died at %d;%d.%n",parisRow,parisCol);
  88.         }
  89.         for (int i = 0; i < size; i++) {
  90.             for (int j = 0; j < size; j++) {
  91.                 System.out.print(field[i][j]);
  92.             }
  93.             System.out.println();
  94.         }
  95.  
  96.     }
  97.  
  98.     private static char[][] move(char[][] field) {
  99.         if (field[parisRow][parisCol] == 'S') {
  100.             energy -= 2;
  101.         }
  102.         energy--;
  103.         if (energy <= 0) {
  104.             field[parisRow][parisCol] = 'X';
  105.         }else {
  106.             field[parisRow][parisCol] = 'P';
  107.         }
  108.         return field;
  109.     }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement