Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.Scanner;
- public class SpaceStationEstablishment {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- int dimension = Integer.parseInt(sc.nextLine());
- char[][] matrix = new char[dimension][dimension];
- int starPower = 0;
- int initRow = 0;
- int initCol = 0;
- int countBlackHoles = 0;
- int rowFirstBlackHole =0;
- int columnSecondBlackHole = 0;
- int rowSecondBlackHole = 0;
- int columnFirstBlackHole = 0;
- int shipRow;
- int shipCol;
- for(int row = 0; row < matrix.length; row++) {
- String[] input = sc.nextLine().split("");
- for (int col = 0; col < input.length; col++) {
- matrix[row][col] = input[col].charAt(0);
- if (matrix[row][col] == 'S') {
- initRow = row;
- initCol = col;
- }
- if (matrix[row][col] == 'O') {
- if (countBlackHoles > 0) {
- rowSecondBlackHole = row;
- columnSecondBlackHole = col;
- } else {
- rowFirstBlackHole = row;
- columnFirstBlackHole = col;
- countBlackHoles++;
- }
- }
- }
- }
- // }
- // }
- // printMatrix(matrix);
- char value;
- boolean outOfBounds = false;
- while (true) {
- String command = sc.nextLine();
- switch (command) {
- case "up":
- if (initRow -1 >= 0) {
- initRow -= 1;
- value = matrix[initRow][initCol];
- // matrix[initRow][initCol] = '-';
- matrix[initRow + 1][initCol] = '-';
- if (Character.isDigit(value)) {
- starPower += value - '0';
- matrix[initRow][initCol] = 'S';
- } else {
- if (initRow == rowFirstBlackHole && initCol == columnFirstBlackHole ) {
- initRow = rowSecondBlackHole;
- initCol = columnSecondBlackHole;
- } else if (initRow == rowSecondBlackHole && initCol == columnSecondBlackHole) {
- initRow = rowFirstBlackHole;
- initCol = columnFirstBlackHole;
- }
- }
- } else {
- outOfBounds = true;
- }
- break;
- case "down":
- if (initRow +1 < dimension) {
- initRow += 1;
- value = matrix[initRow][initCol];
- // matrix[initRow][initCol] = '-';
- matrix[initRow - 1][initCol] = '-';
- if (Character.isDigit(value)) {
- starPower += value - '0';
- matrix[initRow][initCol] = 'S';
- } else {
- if (initRow == rowFirstBlackHole && initCol == columnFirstBlackHole ) {
- initRow = rowSecondBlackHole;
- initCol = columnSecondBlackHole;
- } else if (initRow == rowSecondBlackHole && initCol == columnSecondBlackHole) {
- initRow = rowFirstBlackHole;
- initCol = columnFirstBlackHole;
- }
- }
- } else {
- outOfBounds = true;
- }
- break;
- case "left":
- if (initCol - 1 >=0) {
- initCol -= 1;
- value = matrix[initRow][initCol];
- // matrix[initRow][initCol] = '-';
- matrix[initRow][initCol+1] = '-';
- if (Character.isDigit(value)) {
- starPower += value - '0';
- matrix[initRow][initCol] = 'S';
- } else {
- if (initRow == rowFirstBlackHole && initCol == columnFirstBlackHole ) {
- initRow = rowSecondBlackHole;
- initCol = columnSecondBlackHole;
- } else if (initRow == rowSecondBlackHole && initCol == columnSecondBlackHole) {
- initRow = rowFirstBlackHole;
- initCol = columnFirstBlackHole;
- }
- }
- } else {
- outOfBounds = true;
- }
- break;
- case "right":
- if (initCol + 1 < matrix[initRow].length) {
- // matrix[initRow][initCol] = '-';
- initCol += 1;
- value = matrix[initRow][initCol];
- // matrix[initRow][initCol] = '-';
- matrix[initRow][initCol-1] = '-';
- if (Character.isDigit(value)) {
- starPower += value - '0';
- matrix[initRow][initCol] = 'S';
- } else {
- if (initRow == rowFirstBlackHole && initCol == columnFirstBlackHole ) {
- matrix[initRow][initCol] = '-';
- initRow = rowSecondBlackHole;
- initCol = columnSecondBlackHole;
- } else if (initRow == rowSecondBlackHole && initCol == columnSecondBlackHole) {
- initRow = rowFirstBlackHole;
- initCol = columnFirstBlackHole;
- }
- }
- } else {
- outOfBounds = true;
- }
- break;
- }
- if (outOfBounds || starPower >= 50) {
- // matrix[initRow][initCol] = '-';
- break;
- }
- }
- if (outOfBounds) {
- System.out.println("Bad news, the spaceship went to the void.");
- matrix[initRow][initCol] = '-';
- } else {
- System.out.println("Good news! Stephen succeeded in collecting enough star power!");
- }
- System.out.println("Star power collected: " + starPower);
- printMatrix(matrix);
- }
- private static void printMatrix(char[][] matrix) {
- for (int r = 0; r < matrix.length; r++) {
- for (int c = 0; c < matrix[r].length; c++) {
- System.out.print(matrix[r][c] + "");
- }
- System.out.println();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement