Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.97 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.*;
  4.  
  5. public class Main {
  6. public static void main(String[] args) {
  7. Scanner scanner = new Scanner(System.in);
  8. int size=Integer.parseInt(scanner.nextLine());
  9. char[][] field=new char[size][size];
  10. int stephenRow=0;
  11. int stephenCol=0;
  12. int firstRow=0;
  13. int firstCol=0;
  14. int secondRow=0;
  15. int secondCol=0;
  16. int count=2;
  17. int star=0;
  18. for (int i = 0; i < field.length; i++) {
  19. String line=scanner.nextLine();
  20. field[i]=line.toCharArray();
  21. if(line.contains("S")) {
  22. stephenRow=i;
  23. stephenCol=line.indexOf('S');
  24. }
  25. if(line.contains("O")) {
  26. if(count==2) {
  27. firstRow = i;
  28. firstCol = line.indexOf('O');
  29. count--;
  30. } else {
  31. secondRow = i;
  32. secondCol = line.indexOf('O');
  33. }
  34. }
  35. }
  36. boolean holeExist=true;
  37. while (star<50) {
  38. String move=scanner.nextLine();
  39. if("up".equals(move)) {
  40. if(!canMove(stephenRow-1,stephenCol,field)) {
  41. System.out.println("Bad news, the spaceship went to the void.");
  42. field[stephenRow][stephenCol]='-';
  43. break;
  44. }
  45. field[stephenRow][stephenCol]='-';
  46. char symbol=field[stephenRow-1][stephenCol];
  47. if(Character.isDigit(symbol)) {
  48. star+=symbol-'0';
  49. }
  50. field[stephenRow-1][stephenCol]='S';
  51. if(symbol=='O') {
  52. holeExist=false;
  53. if(field[stephenRow-1][stephenCol]==field[firstRow][firstCol]) {
  54. field[secondRow][secondCol]='S';
  55. field[firstRow][firstCol]='-';
  56. stephenRow=secondRow;
  57. stephenCol=secondCol;
  58. } else {
  59. field[firstRow][firstCol]='S';
  60. field[secondRow][secondCol]='-';
  61. stephenRow=firstRow;
  62. stephenCol=firstCol;
  63. }
  64. }
  65. if(holeExist) {
  66. stephenRow--;
  67. }
  68. } else if("down".equals(move)) {
  69. if(!canMove(stephenRow+1,stephenCol,field)) {
  70. System.out.println("Bad news, the spaceship went to the void.");
  71. field[stephenRow][stephenCol]='-';
  72. break;
  73. }
  74. field[stephenRow][stephenCol]='-';
  75. char symbol=field[stephenRow+1][stephenCol];
  76. if(Character.isDigit(symbol)) {
  77. star+=symbol-'0';
  78. }
  79. field[stephenRow+1][stephenCol]='S';
  80. if(symbol=='O') {
  81. holeExist=false;
  82. if(field[stephenRow+1][stephenCol]==field[firstRow][firstCol]) {
  83. field[secondRow][secondCol]='S';
  84. field[firstRow][firstCol]='-';
  85. stephenRow=secondRow;
  86. stephenCol=secondCol;
  87. } else {
  88. field[firstRow][firstCol]='S';
  89. field[secondRow][secondCol]='-';
  90. stephenRow=firstRow;
  91. stephenCol=firstCol;
  92. }
  93. }
  94. if(holeExist) {
  95. stephenRow++;
  96. }
  97. } else if("left".equals(move)) {
  98. if(!canMove(stephenRow,stephenCol-1,field)) {
  99. System.out.println("Bad news, the spaceship went to the void.");
  100. field[stephenRow][stephenCol]='-';
  101. break;
  102. }
  103. field[stephenRow][stephenCol]='-';
  104. char symbol=field[stephenRow][stephenCol-1];
  105. if(Character.isDigit(symbol)) {
  106. star+=symbol-'0';
  107. }
  108. field[stephenRow][stephenCol-1]='S';
  109. if(symbol=='O') {
  110. holeExist=false;
  111. if(field[stephenRow][stephenCol-1]==field[firstRow][firstCol]) {
  112. field[secondRow][secondCol]='S';
  113. field[firstRow][firstCol]='-';
  114. stephenRow=secondRow;
  115. stephenCol=secondCol;
  116. } else {
  117. field[firstRow][firstCol]='S';
  118. field[secondRow][secondCol]='-';
  119. stephenRow=firstRow;
  120. stephenCol=firstCol;
  121. }
  122. }
  123. if(holeExist) {
  124. stephenCol--;
  125. }
  126. } else if("right".equals(move)) {
  127. if(!canMove(stephenRow,stephenCol+1,field)) {
  128. System.out.println("Bad news, the spaceship went to the void.");
  129. field[stephenRow][stephenCol]='-';
  130. break;
  131. }
  132. field[stephenRow][stephenCol]='-';
  133. char symbol=field[stephenRow][stephenCol+1];
  134. if(Character.isDigit(symbol)) {
  135. star+=symbol-'0';
  136. }
  137. field[stephenRow][stephenCol+1]='S';
  138. if(symbol=='O') {
  139. holeExist=false;
  140. if(field[stephenRow][stephenCol+1]==field[firstRow][firstCol]) {
  141. field[secondRow][secondCol]='S';
  142. field[firstRow][firstCol]='-';
  143. stephenRow=secondRow;
  144. stephenCol=secondCol;
  145. } else {
  146. field[firstRow][firstCol]='S';
  147. field[secondRow][secondCol]='-';
  148. stephenRow=firstRow;
  149. stephenCol=firstCol;
  150. }
  151. }
  152. if(holeExist) {
  153. stephenCol++;
  154. }
  155. }
  156. }
  157. if(star>=50) {
  158. System.out.println("Good news! Stephen succeeded in collecting enough star power!");
  159. }
  160. System.out.printf("Star power collected: %d%n",star);
  161. printField(field);
  162. }
  163. private static boolean canMove(int stephenRow, int stephenCol, char[][] field) {
  164. return stephenRow>=0 && stephenRow<field.length &&
  165. stephenCol>=0 && stephenCol<field.length;
  166. }
  167. private static void printField(char[][] field) {
  168. for (int r = 0; r < field.length; r++) {
  169. for (int c = 0; c < field.length; c++) {
  170. System.out.print(field[r][c]);
  171. }
  172. System.out.println();
  173. }
  174. }
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement