Advertisement
Guest User

Space Station Establishment EXAM

a guest
Oct 20th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.57 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class SpaceStationEstablishment_EXAM {
  4. public static int stefanRow = 0;
  5. public static int stefanCol = 0;
  6. public static int firstBlackRow = 0;
  7. public static int firstBlackCol = 0;
  8. public static int secondBlackRow = 0;
  9. public static int secondBlackCol = 0;
  10. public static int stars = 0;
  11.  
  12. public static void main(String[] args) {
  13.  
  14. Scanner scanner = new Scanner(System.in);
  15.  
  16. int rows = Integer.parseInt(scanner.nextLine());
  17. char[][] matrix = new char[rows][];
  18. int blackHolesCount = 2;
  19.  
  20. for (int i = 0; i < rows; i++) {
  21. String line = scanner.nextLine();
  22. matrix[i] = line.toCharArray();
  23. if (line.contains("S")) {
  24. stefanRow = i;
  25. stefanCol = line.indexOf('S');
  26. }
  27. if (line.contains("O")) {
  28. if (blackHolesCount == 2) {
  29. firstBlackRow = i;
  30. firstBlackCol = line.indexOf("O");
  31. blackHolesCount--;
  32.  
  33. } else {
  34. secondBlackRow = i;
  35. secondBlackCol = line.indexOf("O");
  36.  
  37. }
  38. }
  39. }
  40.  
  41. boolean outOfField = false;
  42.  
  43. while (!outOfField && stars < 50) {
  44.  
  45. String command = scanner.nextLine();
  46.  
  47. switch (command) {
  48. case "up":
  49. if (stefanRow - 1 < 0) {
  50. outOfField = true;
  51. matrix[stefanRow][stefanCol] = '-';
  52. } else if (matrix[stefanRow - 1][stefanCol] == 'O') {
  53. if (stefanRow - 1 == firstBlackRow && stefanCol == firstBlackCol) {
  54. moveFirstTosecond(matrix, firstBlackRow, firstBlackCol, secondBlackRow, secondBlackCol);
  55. } else if ((stefanRow - 1 == secondBlackRow && stefanCol == secondBlackCol))
  56. moveSecondTofirst(matrix, secondBlackRow, secondBlackCol, firstBlackRow, firstBlackCol);
  57.  
  58. } else if (isDigit(stefanRow - 1, stefanCol, matrix)) {
  59. stars += Integer.parseInt(String.valueOf(matrix[stefanRow - 1][stefanCol]));
  60. matrix[stefanRow][stefanCol] = '-';
  61. stefanRow--;
  62. matrix[stefanRow][stefanCol] = 'S';
  63. } else if (stefanRow - 1 < 0) {
  64. outOfField = true;
  65. matrix[stefanRow][stefanCol] = '-';
  66. } else {
  67. matrix[stefanRow][stefanCol] = '-';
  68. stefanRow--;
  69. matrix[stefanRow][stefanCol] = 'S';
  70. }
  71.  
  72. break;
  73. case "down":
  74. if (stefanRow + 1 > matrix.length - 1) {
  75. outOfField = true;
  76. matrix[stefanRow][stefanCol] = '-';
  77.  
  78. } else if (matrix[stefanRow + 1][stefanCol] == 'O') {
  79. if (stefanRow + 1 == firstBlackRow && stefanCol == firstBlackCol) {
  80. moveFirstTosecond(matrix, firstBlackRow, firstBlackCol, secondBlackRow, secondBlackCol);
  81. } else if ((stefanRow + 1 == secondBlackRow && stefanCol == secondBlackCol))
  82. moveSecondTofirst(matrix, secondBlackRow, secondBlackCol, firstBlackRow, firstBlackCol);
  83.  
  84. } else if (isDigit(stefanRow + 1, stefanCol, matrix)) {
  85. stars += Integer.parseInt(String.valueOf(matrix[stefanRow + 1][stefanCol]));
  86. matrix[stefanRow][stefanCol] = '-';
  87. stefanRow++;
  88. matrix[stefanRow][stefanCol] = 'S';
  89. } else {
  90. matrix[stefanRow][stefanCol] = '-';
  91. stefanRow++;
  92. matrix[stefanRow][stefanCol] = 'S';
  93. }
  94.  
  95. break;
  96. case "left":
  97. if (stefanCol - 1 < 0) {
  98. outOfField = true;
  99. matrix[stefanRow][stefanCol] = '-';
  100.  
  101. } else if (matrix[stefanRow][stefanCol - 1] == 'O') {
  102. if (stefanRow == firstBlackRow && stefanCol - 1 == firstBlackCol) {
  103. moveFirstTosecond(matrix, firstBlackRow, firstBlackCol, secondBlackRow, secondBlackCol);
  104. } else if ((stefanRow == secondBlackRow && stefanCol - 1 == secondBlackCol))
  105. moveSecondTofirst(matrix, secondBlackRow, secondBlackCol, firstBlackRow, firstBlackCol);
  106.  
  107. } else if (isDigit(stefanRow, stefanCol - 1, matrix)) {
  108. stars += Integer.parseInt(String.valueOf(matrix[stefanRow][stefanCol-1]));
  109. matrix[stefanRow][stefanCol] = '-';
  110. stefanCol--;
  111. matrix[stefanRow][stefanCol] = 'S';
  112. } else {
  113. matrix[stefanRow][stefanCol] = '-';
  114. stefanCol--;
  115. matrix[stefanRow][stefanCol] = 'S';
  116. }
  117.  
  118. break;
  119. case "right":
  120. if (stefanCol + 1 > matrix.length - 1) {
  121. outOfField = true;
  122. matrix[stefanRow][stefanCol] = '-';
  123.  
  124. } else if (matrix[stefanRow][stefanCol + 1] == 'O') {
  125. if (stefanRow == firstBlackRow && stefanCol + 1 == firstBlackCol) {
  126. moveFirstTosecond(matrix, firstBlackRow, firstBlackCol, secondBlackRow, secondBlackCol);
  127. } else if ((stefanRow == secondBlackRow && stefanCol + 1 == secondBlackCol))
  128. moveSecondTofirst(matrix, secondBlackRow, secondBlackCol, firstBlackRow, firstBlackCol);
  129.  
  130. } else if (isDigit(stefanRow, stefanCol + 1, matrix)) {
  131. stars += Integer.parseInt(String.valueOf(matrix[stefanRow ][stefanCol+1]));
  132. matrix[stefanRow][stefanCol] = '-';
  133. stefanCol++;
  134. matrix[stefanRow][stefanCol] = 'S';
  135. } else {
  136. matrix[stefanRow][stefanCol] = '-';
  137. stefanCol++;
  138. matrix[stefanRow][stefanCol] = 'S';
  139. }
  140. break;
  141. }
  142.  
  143.  
  144. }
  145. if (outOfField) {
  146.  
  147. System.out.println("Bad news, the spaceship went to the void.");
  148. System.out.printf("Star power collected: %d\n", stars);
  149.  
  150. } else if (stars >= 50) {
  151. System.out.println("Good news! Stephen succeeded in collecting enough star power!");
  152. System.out.printf("Star power collected: %d\n", stars);
  153.  
  154. }
  155.  
  156. printMatrix(matrix);
  157. }
  158.  
  159. private static void moveSecondTofirst(char[][] matrix, int secondBlackRow, int secondBlackCol, int firstBlackRow, int firstBlackCol) {
  160. matrix[stefanRow][stefanCol] = '-';
  161. matrix[secondBlackRow][secondBlackCol] = '-';
  162. matrix[firstBlackRow][firstBlackCol] = 'S';
  163. stefanRow = firstBlackRow;
  164. stefanCol = firstBlackCol;
  165. }
  166.  
  167. private static void moveFirstTosecond(char[][] matrix, int firstBlackRow, int firstBlackCol, int secondBlackRow, int secondBlackCol) {
  168. matrix[stefanRow][stefanCol] = '-';
  169. matrix[firstBlackRow][firstBlackCol] = '-';
  170. matrix[secondBlackRow][secondBlackCol] = 'S';
  171. stefanRow = secondBlackRow;
  172. stefanCol = secondBlackCol;
  173. }
  174.  
  175. private static boolean isValid(int row, int col, char[][] matrix) {
  176. return row >= 0 && row < matrix.length && col >= 0 && col < matrix[row].length;
  177. }
  178.  
  179. private static boolean isDigit(int stefanRow, int stefanCol, char[][] matrix) {
  180. int value = 0;
  181. for (int i = 0; i < matrix.length; i++) {
  182. for (int j = 0; j < matrix[i].length; j++) {
  183. if (Character.isDigit(matrix[stefanRow][stefanCol])) {
  184. return true;
  185. }
  186. }
  187. }
  188. return false;
  189. }
  190.  
  191.  
  192. private static void printMatrix(char[][] matrix) {
  193. for (int i = 0; i < matrix.length; i++) {
  194. for (int j = 0; j < matrix[i].length; j++) {
  195. System.out.print(matrix[i][j]);
  196. }
  197. System.out.println();
  198. }
  199. }
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement