Advertisement
Guest User

Untitled

a guest
Feb 21st, 2020
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.02 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class SpaceStationEstablishment {
  4. public static void main(String[] args) {
  5. Scanner scanner = new Scanner(System.in);
  6.  
  7. int n = Integer.parseInt(scanner.nextLine());
  8.  
  9. char[][] matrix = new char[n][n];
  10. int rowIndex = 0;
  11. int colIndex = 0;
  12. int counter = 0;
  13. int firstBlackholeRow = 0;
  14. int firstBlackholeCol = 0;
  15. int secondBlackholeRow = 0;
  16. int secondBlackholeCol = 0;
  17.  
  18.  
  19. for (int i = 0; i < n; i++) {
  20. String row = scanner.nextLine();
  21. for (int j = 0; j < row.length(); j++) {
  22. matrix[i][j] = row.charAt(j);
  23. if (matrix[i][j] == 'S') {
  24. rowIndex = i;
  25. colIndex = j;
  26. }
  27. if (matrix[i][j] == 'O') {
  28. counter++;
  29. if (counter == 1) {
  30. firstBlackholeRow = i;
  31. firstBlackholeCol = j;
  32. } else {
  33. secondBlackholeRow = i;
  34. secondBlackholeCol = j;
  35. }
  36. }
  37. }
  38. }
  39. boolean crossBorder = false;
  40. int starPower = 0;
  41. String command = scanner.nextLine();
  42. while (starPower < 50) {
  43. if (command.equals("left")) {
  44. if (!crossBorder(command, matrix, rowIndex, colIndex)) {
  45. if (Character.isDigit(matrix[rowIndex][colIndex - 1])) {
  46. starPower += Integer.parseInt(matrix[rowIndex][colIndex - 1] + "");
  47. if (starPower>=50){
  48. matrix[rowIndex][colIndex] = '-';
  49. matrix[rowIndex][colIndex - 1] = 'S';
  50. break;
  51. }
  52. matrix[rowIndex][colIndex] = '-';
  53. matrix[rowIndex][colIndex - 1] = 'S';
  54. colIndex = colIndex - 1;
  55. } else if (matrix[rowIndex][colIndex - 1] == 'O') {
  56. if (rowIndex == firstBlackholeRow && colIndex - 1 == firstBlackholeCol) {
  57. matrix[rowIndex][colIndex] = '-';
  58. matrix[secondBlackholeRow][secondBlackholeCol] = 'S';
  59. matrix[firstBlackholeRow][firstBlackholeCol] = '-';
  60. rowIndex = secondBlackholeRow;
  61. colIndex = secondBlackholeCol;
  62. } else {
  63. matrix[rowIndex][colIndex] = '-';
  64. matrix[firstBlackholeRow][firstBlackholeCol] = 'S';
  65. matrix[secondBlackholeRow][secondBlackholeCol] = '-';
  66. rowIndex = firstBlackholeRow;
  67. colIndex = firstBlackholeCol;
  68. }
  69. } else {
  70. matrix[rowIndex][colIndex - 1] = 'S';
  71. matrix[rowIndex][colIndex] = '-';
  72. colIndex = colIndex - 1;
  73. }
  74. } else {
  75. System.out.println("Bad news, the spaceship went to the void.");
  76. System.out.println("Star power collected: " + starPower);
  77. for (int i = 0; i < n; i++) {
  78. for (int j = 0; j < n; j++) {
  79. System.out.print(matrix[i][j]);
  80. }
  81. System.out.println();
  82. }
  83. return;
  84.  
  85. }
  86. } else if (command.equals("right")) {
  87. if (!crossBorder(command, matrix, rowIndex, colIndex)) {
  88. if (Character.isDigit(matrix[rowIndex][colIndex + 1])) {
  89. starPower += Integer.parseInt(matrix[rowIndex][colIndex + 1] + "");
  90. if (starPower>=50){
  91. matrix[rowIndex][colIndex] = '-';
  92. matrix[rowIndex][colIndex + 1] = 'S';
  93. break;
  94. }
  95. matrix[rowIndex][colIndex] = '-';
  96. matrix[rowIndex][colIndex + 1] = 'S';
  97. colIndex = colIndex + 1;
  98. } else if (matrix[rowIndex][colIndex + 1] == 'O') {
  99. if (rowIndex == firstBlackholeRow && colIndex + 1 == firstBlackholeCol) {
  100. matrix[rowIndex][colIndex] = '-';
  101. matrix[secondBlackholeRow][secondBlackholeCol] = 'S';
  102. matrix[firstBlackholeRow][firstBlackholeCol] = '-';
  103. rowIndex = secondBlackholeRow;
  104. colIndex = secondBlackholeCol;
  105. } else {
  106. matrix[rowIndex][colIndex] = '-';
  107. matrix[firstBlackholeRow][firstBlackholeCol] = 'S';
  108. matrix[secondBlackholeRow][secondBlackholeCol] = '-';
  109. rowIndex = firstBlackholeRow;
  110. colIndex = firstBlackholeCol;
  111. }
  112. } else {
  113. matrix[rowIndex][colIndex + 1] = 'S';
  114. matrix[rowIndex][colIndex] = '-';
  115. colIndex = colIndex + 1;
  116. }
  117. } else {
  118. System.out.println("Bad news, the spaceship went to the void.");
  119. System.out.println("Star power collected: " + starPower);
  120. for (int i = 0; i < n; i++) {
  121. for (int j = 0; j < n; j++) {
  122. System.out.print(matrix[i][j]);
  123. }
  124. System.out.println();
  125. }
  126. return;
  127.  
  128. }
  129. } else if (command.equals("up")) {
  130. if (!crossBorder(command, matrix, rowIndex, colIndex)) {
  131. if (Character.isDigit(matrix[rowIndex - 1][colIndex])) {
  132. starPower += Integer.parseInt(matrix[rowIndex - 1][colIndex] + "");
  133. if (starPower>=50){
  134. matrix[rowIndex][colIndex] = '-';
  135. matrix[rowIndex - 1][colIndex] = 'S';
  136. break;
  137. }
  138. matrix[rowIndex][colIndex] = '-';
  139. matrix[rowIndex - 1][colIndex] = 'S';
  140. rowIndex = rowIndex - 1;
  141. } else if (matrix[rowIndex - 1][colIndex] == 'O') {
  142. if (rowIndex - 1 == firstBlackholeRow && colIndex == firstBlackholeCol) {
  143. matrix[rowIndex][colIndex] = '-';
  144. matrix[secondBlackholeRow][secondBlackholeCol] = 'S';
  145. matrix[firstBlackholeRow][firstBlackholeCol] = '-';
  146. rowIndex = secondBlackholeRow;
  147. colIndex = secondBlackholeCol;
  148. } else {
  149. matrix[rowIndex][colIndex] = '-';
  150. matrix[firstBlackholeRow][firstBlackholeCol] = 'S';
  151. matrix[secondBlackholeRow][secondBlackholeCol] = '-';
  152. rowIndex = firstBlackholeRow;
  153. colIndex = firstBlackholeCol;
  154. }
  155. } else {
  156. matrix[rowIndex - 1][colIndex] = 'S';
  157. matrix[rowIndex][colIndex] = '-';
  158. rowIndex = rowIndex - 1;
  159. }
  160. } else {
  161. System.out.println("Bad news, the spaceship went to the void.");
  162. System.out.println("Star power collected: " + starPower);
  163. for (int i = 0; i < n; i++) {
  164. for (int j = 0; j < n; j++) {
  165. System.out.print(matrix[i][j]);
  166. }
  167. System.out.println();
  168. }
  169. return;
  170.  
  171. }
  172. } else if (command.equals("down")) {
  173. if (!crossBorder(command, matrix, rowIndex, colIndex)) {
  174. if (Character.isDigit(matrix[rowIndex + 1][colIndex])) {
  175. starPower += Integer.parseInt(matrix[rowIndex + 1][colIndex] + "");
  176. if (starPower>=50){
  177. matrix[rowIndex][colIndex] = '-';
  178. matrix[rowIndex + 1][colIndex] = 'S';
  179. break;
  180. }
  181. matrix[rowIndex][colIndex] = '-';
  182. matrix[rowIndex + 1][colIndex] = 'S';
  183. rowIndex = rowIndex + 1;
  184. } else if (matrix[rowIndex + 1][colIndex] == 'O') {
  185. if (rowIndex + 1 == firstBlackholeRow && colIndex == firstBlackholeCol) {
  186. matrix[rowIndex][colIndex] = '-';
  187. matrix[secondBlackholeRow][secondBlackholeCol] = 'S';
  188. matrix[firstBlackholeRow][firstBlackholeCol] = '-';
  189. rowIndex = secondBlackholeRow;
  190. colIndex = secondBlackholeCol;
  191. } else {
  192. matrix[rowIndex][colIndex] = '-';
  193. matrix[firstBlackholeRow][firstBlackholeCol] = 'S';
  194. matrix[secondBlackholeRow][secondBlackholeCol] = '-';
  195. rowIndex = firstBlackholeRow;
  196. colIndex = firstBlackholeCol;
  197. }
  198. } else {
  199. matrix[rowIndex + 1][colIndex] = 'S';
  200. matrix[rowIndex][colIndex] = '-';
  201. rowIndex = rowIndex + 1;
  202. }
  203. } else {
  204. System.out.println("Bad news, the spaceship went to the void.");
  205. System.out.println("Star power collected: " + starPower);
  206. for (int i = 0; i < n; i++) {
  207. for (int j = 0; j < n; j++) {
  208. System.out.print(matrix[i][j]);
  209. }
  210. System.out.println();
  211. }
  212. return;
  213. }
  214. }
  215. command = scanner.nextLine();
  216. }
  217. System.out.println("Good news! Stephen succeeded in collecting enough star power!");
  218. System.out.println("Star power collected: " + starPower);
  219. for (int i = 0; i < n; i++) {
  220. for (int j = 0; j < n; j++) {
  221. System.out.print(matrix[i][j]);
  222. }
  223. System.out.println();
  224. }
  225. }
  226.  
  227.  
  228. public static boolean crossBorder(String command, char[][] matrix, int rowIndex, int colIndex) {
  229. boolean outOfBorder = false;
  230. int newRowIndex = 0;
  231. int newColIndex = 0;
  232. if (command.equals("left")) {
  233. newColIndex = colIndex - 1;
  234. if (newColIndex < 0) {
  235. outOfBorder = true;
  236. }
  237. } else if (command.equals("right")) {
  238. newColIndex = colIndex + 1;
  239. if (newColIndex >= matrix.length) {
  240. outOfBorder = true;
  241. }
  242. } else if (command.equals("up")) {
  243. newRowIndex = rowIndex - 1;
  244. if (newRowIndex < 0) {
  245. outOfBorder = true;
  246. }
  247. } else if (command.equals("down")) {
  248. newRowIndex = rowIndex + 1;
  249. if (newRowIndex >= matrix.length) {
  250. outOfBorder = true;
  251. }
  252. } else {
  253. outOfBorder = false;
  254.  
  255. }
  256. matrix[rowIndex][colIndex] = '-';
  257. return outOfBorder;
  258.  
  259. }
  260.  
  261. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement