Advertisement
meteor4o

JA-Exams-SpaceStationEstablishment

Oct 21st, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.11 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class SpaceStationEstablishment {
  6.     public static void main(String[] args) {
  7.         Scanner sc = new Scanner(System.in);
  8.         int dimension = Integer.parseInt(sc.nextLine());
  9.         char[][] matrix = new char[dimension][dimension];
  10.         int starPower = 0;
  11.         int initRow = 0;
  12.         int initCol = 0;
  13.         int countBlackHoles = 0;
  14.         int rowFirstBlackHole =0;
  15.         int columnSecondBlackHole = 0;
  16.         int rowSecondBlackHole = 0;
  17.         int columnFirstBlackHole = 0;
  18.         int shipRow;
  19.         int shipCol;
  20.  
  21.         for(int row = 0; row < matrix.length; row++) {
  22.             String[] input = sc.nextLine().split("");
  23.             for (int col = 0; col < input.length; col++) {
  24.                 matrix[row][col] = input[col].charAt(0);
  25.                 if (matrix[row][col] == 'S') {
  26.                     initRow = row;
  27.                     initCol = col;
  28.                 }
  29.                 if (matrix[row][col] == 'O') {
  30.  
  31.                     if (countBlackHoles > 0) {
  32.                         rowSecondBlackHole = row;
  33.                         columnSecondBlackHole = col;
  34.                     } else {
  35.                         rowFirstBlackHole = row;
  36.                         columnFirstBlackHole = col;
  37.                         countBlackHoles++;
  38.                     }
  39.  
  40.                     }
  41.                 }
  42.                 }
  43. //            }
  44. //        }
  45.  //       printMatrix(matrix);
  46.  
  47.         char value;
  48.         boolean outOfBounds = false;
  49.         while (true) {
  50.             String command = sc.nextLine();
  51.             switch (command) {
  52.                 case "up":
  53.                     if (initRow -1 >= 0) {
  54.                         initRow -= 1;
  55.                         value = matrix[initRow][initCol];
  56. //                        matrix[initRow][initCol] = '-';
  57.                         matrix[initRow + 1][initCol] = '-';
  58.                         if (Character.isDigit(value)) {
  59.                             starPower += value - '0';
  60.                             matrix[initRow][initCol] = 'S';
  61.                         } else {
  62.                             if (initRow == rowFirstBlackHole && initCol == columnFirstBlackHole ) {
  63.                                 initRow = rowSecondBlackHole;
  64.                                 initCol = columnSecondBlackHole;
  65.                             } else if (initRow == rowSecondBlackHole && initCol == columnSecondBlackHole) {
  66.                                 initRow = rowFirstBlackHole;
  67.                                 initCol = columnFirstBlackHole;
  68.                             }
  69.                         }
  70.  
  71.                     } else {
  72.                         outOfBounds = true;
  73.                     }
  74.                     break;
  75.  
  76.                 case "down":
  77.                     if (initRow +1 < dimension) {
  78.                         initRow += 1;
  79.                         value = matrix[initRow][initCol];
  80. //                        matrix[initRow][initCol] = '-';
  81.                         matrix[initRow - 1][initCol] = '-';
  82.                         if (Character.isDigit(value)) {
  83.                             starPower += value - '0';
  84.                             matrix[initRow][initCol] = 'S';
  85.                         } else {
  86.                             if (initRow == rowFirstBlackHole && initCol == columnFirstBlackHole ) {
  87.                                 initRow = rowSecondBlackHole;
  88.                                 initCol = columnSecondBlackHole;
  89.                             } else if (initRow == rowSecondBlackHole && initCol == columnSecondBlackHole) {
  90.                                 initRow = rowFirstBlackHole;
  91.                                 initCol = columnFirstBlackHole;
  92.                             }
  93.                         }
  94.  
  95.                     } else {
  96.                         outOfBounds = true;
  97.                     }
  98.                     break;
  99.  
  100.                 case "left":
  101.                     if (initCol - 1 >=0) {
  102.                         initCol -= 1;
  103.                         value = matrix[initRow][initCol];
  104. //                        matrix[initRow][initCol] = '-';
  105.                         matrix[initRow][initCol+1] = '-';
  106.                         if (Character.isDigit(value)) {
  107.                             starPower += value - '0';
  108.                             matrix[initRow][initCol] = 'S';
  109.                         } else {
  110.                             if (initRow == rowFirstBlackHole && initCol == columnFirstBlackHole ) {
  111.                                 initRow = rowSecondBlackHole;
  112.                                 initCol = columnSecondBlackHole;
  113.                             } else if (initRow == rowSecondBlackHole && initCol == columnSecondBlackHole) {
  114.                                 initRow = rowFirstBlackHole;
  115.                                 initCol = columnFirstBlackHole;
  116.                             }
  117.                         }
  118.  
  119.                     } else {
  120.                         outOfBounds = true;
  121.                     }
  122.  
  123.                     break;
  124.  
  125.                 case "right":
  126.                     if (initCol + 1 < matrix[initRow].length) {
  127. //                        matrix[initRow][initCol] = '-';
  128.                         initCol += 1;
  129.                         value = matrix[initRow][initCol];
  130. //                        matrix[initRow][initCol] = '-';
  131.                         matrix[initRow][initCol-1] = '-';
  132.                         if (Character.isDigit(value)) {
  133.                             starPower += value - '0';
  134.                             matrix[initRow][initCol] = 'S';
  135.                         } else {
  136.                             if (initRow == rowFirstBlackHole && initCol == columnFirstBlackHole ) {
  137.                                 matrix[initRow][initCol] = '-';
  138.                                 initRow = rowSecondBlackHole;
  139.                                 initCol = columnSecondBlackHole;
  140.                             } else if (initRow == rowSecondBlackHole && initCol == columnSecondBlackHole) {
  141.                                 initRow = rowFirstBlackHole;
  142.                                 initCol = columnFirstBlackHole;
  143.                             }
  144.                         }
  145.  
  146.                     } else {
  147.                         outOfBounds = true;
  148.                     }
  149.  
  150.                     break;
  151.             }
  152.             if (outOfBounds || starPower >= 50) {
  153.  //               matrix[initRow][initCol] = '-';
  154.                 break;
  155.             }
  156.  
  157.         }
  158.         if (outOfBounds) {
  159.             System.out.println("Bad news, the spaceship went to the void.");
  160.             matrix[initRow][initCol] = '-';
  161.         } else {
  162.             System.out.println("Good news! Stephen succeeded in collecting enough star power!");
  163.         }
  164.         System.out.println("Star power collected: " + starPower);
  165.  
  166.         printMatrix(matrix);
  167.     }
  168.     private static void printMatrix(char[][] matrix) {
  169.         for (int r = 0; r < matrix.length; r++) {
  170.             for (int c = 0; c < matrix[r].length; c++) {
  171.                 System.out.print(matrix[r][c] + "");
  172.             }
  173.             System.out.println();
  174.  
  175.         }
  176.     }
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement