Advertisement
Guest User

Space Station Establishment

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