Advertisement
Andreeva-Magdalena97

Hole

Oct 23rd, 2019
447
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.25 KB | None | 0 0
  1. package EX2;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Scanner;
  6.  
  7. public class SpaceStation {
  8.  
  9. public static void main(String[] args) {
  10. Scanner s = new Scanner(System.in);
  11. int n = Integer.parseInt(s.nextLine());
  12. String[][] arr = new String[n][n];
  13. List<String> list = new ArrayList<>();
  14. list.add("up");
  15. list.add("down");
  16. list.add("left");
  17. list.add("right");
  18. int row = 0;
  19. int col = 0;
  20. int holeRow = -1;
  21. int holeCol = -1;
  22. int holeRowTwo = -1;
  23. int holeColTwo = -1;
  24. for (int i = 0; i < n; i++) {
  25. String input = s.nextLine();
  26. for (int j = 0; j < n; j++) {
  27. arr[i][j] = String.valueOf(input.charAt(j));
  28. }
  29. if (input.contains("S")) {
  30. row = i;
  31. col = input.indexOf("S");
  32. }
  33. if (input.contains("O")) {
  34. if (holeRow > -1) {
  35. holeRowTwo = i;
  36. holeColTwo = input.lastIndexOf("O");
  37.  
  38. } else {
  39. holeRow = i;
  40. holeCol = input.indexOf("O");
  41. }
  42.  
  43. }
  44. }
  45. System.out.println(row + " " + col);
  46. int points = 0;
  47. while (true) {
  48. String command = s.nextLine();
  49. switch (command) {
  50. case "up":
  51. if (row - 1 < 0) {
  52. arr[row][col] = "-";
  53. System.out.println("Bad news, the spaceship went to the void.");
  54. break;
  55. } else {
  56. if (arr[row - 1][col].charAt(0) == 'O') {
  57. arr[row][col] = "-";
  58. if (arr[row - 1][col] == arr[holeRow][holeCol]) {
  59. arr[holeRow][holeCol] = "-";
  60. arr[holeRowTwo][holeColTwo] = "S";
  61. row = holeRowTwo;
  62. col = holeColTwo;
  63. } else {
  64. arr[holeRowTwo][holeColTwo] = "-";
  65. arr[holeRow][holeCol] = "S";
  66. row = holeRow;
  67. col = holeCol;
  68. }
  69. } else {
  70. if (Character.isDigit(arr[row - 1][col].charAt(0))) {
  71. points += Integer.parseInt(arr[row - 1][col]);
  72. }
  73. arr[row][col] = "-";
  74. arr[--row][col] = "S";
  75. }
  76. }
  77. break;
  78. case "down":
  79. if (row + 1 >= arr.length) {
  80. arr[row][col] = "-";
  81. System.out.println("Bad news, the spaceship went to the void.");
  82. break;
  83. }else {
  84. if (arr[row + 1][col].charAt(0) == 'O') {
  85. arr[row][col] = "-";
  86. if (arr[row + 1][col] == arr[holeRow][holeCol]) {
  87. arr[holeRow][holeCol] = "-";
  88. arr[holeRowTwo][holeColTwo] = "S";
  89. row = holeRowTwo;
  90. col = holeColTwo;
  91. } else {
  92. arr[holeRowTwo][holeColTwo] = "-";
  93. arr[holeRow][holeCol] = "S";
  94. row = holeRow;
  95. col = holeCol;
  96. }
  97. } else {
  98. if (Character.isDigit(arr[row + 1][col].charAt(0))) {
  99. points += Integer.parseInt(arr[row + 1][col]);
  100. }
  101. arr[row][col] = "-";
  102. arr[++row][col] = "S";
  103. }
  104. }
  105. break;
  106. case "left":
  107. if (col - 1 < 0) {
  108. arr[row][col] = "-";
  109. System.out.println("Bad news, the spaceship went to the void.");
  110. break;
  111. } else {
  112. if (arr[row][col-1].charAt(0) == 'O') {
  113. arr[row][col] = "-";
  114. if (arr[row][col-1] == arr[holeRow][holeCol]) {
  115. arr[holeRow][holeCol] = "-";
  116. arr[holeRowTwo][holeColTwo] = "S";
  117. row = holeRowTwo;
  118. col = holeColTwo;
  119. } else {
  120. arr[holeRowTwo][holeColTwo] = "-";
  121. arr[holeRow][holeCol] = "S";
  122. row = holeRow;
  123. col = holeCol;
  124. }
  125. } else {
  126. if (Character.isDigit(arr[row][col-1].charAt(0))) {
  127. points += Integer.parseInt(arr[row][col-1]);
  128. }
  129. arr[row][col] = "-";
  130. arr[row][--col] = "S";
  131. }
  132. }
  133. break;
  134. case "right":
  135. if (col + 1 >= arr.length) {
  136. arr[row][col] = "-";
  137. System.out.println("Bad news, the spaceship went to the void.");
  138. break;
  139. } else {
  140. if (arr[row ][col+1].charAt(0) == 'O') {
  141. arr[row][col] = "-";
  142. if (arr[row][col+1] == arr[holeRow][holeCol]) {
  143. arr[holeRow][holeCol] = "-";
  144. arr[holeRowTwo][holeColTwo] = "S";
  145. row = holeRowTwo;
  146. col = holeColTwo;
  147. } else {
  148. arr[holeRowTwo][holeColTwo] = "-";
  149. arr[holeRow][holeCol] = "S";
  150. row = holeRow;
  151. col = holeCol;
  152. }
  153. } else {
  154. if (Character.isDigit(arr[row][col+1].charAt(0))) {
  155. points += Integer.parseInt(arr[row][col+1]);
  156. }
  157. arr[row][col] = "-";
  158. arr[row][++col] = "S";
  159. }
  160. }
  161. break;
  162. }
  163.  
  164. if (!list.contains(command)) {
  165. break;
  166. }
  167. }
  168. //Print
  169. for (int k = 0; k < n; k++) {
  170. for (int j = 0; j < n; j++) {
  171. System.out.print(arr[k][j]);
  172. }
  173. System.out.println();
  174. }
  175. System.out.println(points);
  176. }
  177.  
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement