Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.*;
- public class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int size=Integer.parseInt(scanner.nextLine());
- char[][] field=new char[size][size];
- int stephenRow=0;
- int stephenCol=0;
- int firstRow=0;
- int firstCol=0;
- int secondRow=0;
- int secondCol=0;
- int count=2;
- int star=0;
- for (int i = 0; i < field.length; i++) {
- String line=scanner.nextLine();
- field[i]=line.toCharArray();
- if(line.contains("S")) {
- stephenRow=i;
- stephenCol=line.indexOf('S');
- }
- if(line.contains("O")) {
- if(count==2) {
- firstRow = i;
- firstCol = line.indexOf('O');
- count--;
- } else {
- secondRow = i;
- secondCol = line.indexOf('O');
- }
- }
- }
- boolean holeExist=true;
- while (star<50) {
- String move=scanner.nextLine();
- if("up".equals(move)) {
- if(!canMove(stephenRow-1,stephenCol,field)) {
- System.out.println("Bad news, the spaceship went to the void.");
- field[stephenRow][stephenCol]='-';
- break;
- }
- field[stephenRow][stephenCol]='-';
- char symbol=field[stephenRow-1][stephenCol];
- if(Character.isDigit(symbol)) {
- star+=symbol-'0';
- }
- field[stephenRow-1][stephenCol]='S';
- if(symbol=='O') {
- holeExist=false;
- if(field[stephenRow-1][stephenCol]==field[firstRow][firstCol]) {
- field[secondRow][secondCol]='S';
- field[firstRow][firstCol]='-';
- stephenRow=secondRow;
- stephenCol=secondCol;
- } else {
- field[firstRow][firstCol]='S';
- field[secondRow][secondCol]='-';
- stephenRow=firstRow;
- stephenCol=firstCol;
- }
- }
- if(holeExist) {
- stephenRow--;
- }
- } else if("down".equals(move)) {
- if(!canMove(stephenRow+1,stephenCol,field)) {
- System.out.println("Bad news, the spaceship went to the void.");
- field[stephenRow][stephenCol]='-';
- break;
- }
- field[stephenRow][stephenCol]='-';
- char symbol=field[stephenRow+1][stephenCol];
- if(Character.isDigit(symbol)) {
- star+=symbol-'0';
- }
- field[stephenRow+1][stephenCol]='S';
- if(symbol=='O') {
- holeExist=false;
- if(field[stephenRow+1][stephenCol]==field[firstRow][firstCol]) {
- field[secondRow][secondCol]='S';
- field[firstRow][firstCol]='-';
- stephenRow=secondRow;
- stephenCol=secondCol;
- } else {
- field[firstRow][firstCol]='S';
- field[secondRow][secondCol]='-';
- stephenRow=firstRow;
- stephenCol=firstCol;
- }
- }
- if(holeExist) {
- stephenRow++;
- }
- } else if("left".equals(move)) {
- if(!canMove(stephenRow,stephenCol-1,field)) {
- System.out.println("Bad news, the spaceship went to the void.");
- field[stephenRow][stephenCol]='-';
- break;
- }
- field[stephenRow][stephenCol]='-';
- char symbol=field[stephenRow][stephenCol-1];
- if(Character.isDigit(symbol)) {
- star+=symbol-'0';
- }
- field[stephenRow][stephenCol-1]='S';
- if(symbol=='O') {
- holeExist=false;
- if(field[stephenRow][stephenCol-1]==field[firstRow][firstCol]) {
- field[secondRow][secondCol]='S';
- field[firstRow][firstCol]='-';
- stephenRow=secondRow;
- stephenCol=secondCol;
- } else {
- field[firstRow][firstCol]='S';
- field[secondRow][secondCol]='-';
- stephenRow=firstRow;
- stephenCol=firstCol;
- }
- }
- if(holeExist) {
- stephenCol--;
- }
- } else if("right".equals(move)) {
- if(!canMove(stephenRow,stephenCol+1,field)) {
- System.out.println("Bad news, the spaceship went to the void.");
- field[stephenRow][stephenCol]='-';
- break;
- }
- field[stephenRow][stephenCol]='-';
- char symbol=field[stephenRow][stephenCol+1];
- if(Character.isDigit(symbol)) {
- star+=symbol-'0';
- }
- field[stephenRow][stephenCol+1]='S';
- if(symbol=='O') {
- holeExist=false;
- if(field[stephenRow][stephenCol+1]==field[firstRow][firstCol]) {
- field[secondRow][secondCol]='S';
- field[firstRow][firstCol]='-';
- stephenRow=secondRow;
- stephenCol=secondCol;
- } else {
- field[firstRow][firstCol]='S';
- field[secondRow][secondCol]='-';
- stephenRow=firstRow;
- stephenCol=firstCol;
- }
- }
- if(holeExist) {
- stephenCol++;
- }
- }
- }
- if(star>=50) {
- System.out.println("Good news! Stephen succeeded in collecting enough star power!");
- }
- System.out.printf("Star power collected: %d%n",star);
- printField(field);
- }
- private static boolean canMove(int stephenRow, int stephenCol, char[][] field) {
- return stephenRow>=0 && stephenRow<field.length &&
- stephenCol>=0 && stephenCol<field.length;
- }
- private static void printField(char[][] field) {
- for (int r = 0; r < field.length; r++) {
- for (int c = 0; c < field.length; c++) {
- System.out.print(field[r][c]);
- }
- System.out.println();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement