Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2020
485
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.18 KB | None | 0 0
  1. package JavaAdvancedRetakeExamDec2019;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class PresentDelivery {
  6. static int kindKids = 0;
  7. static int newRowHero = 0;
  8. static int newColHero = 0;
  9. static int presentsCountToSpread;
  10. static int givenPresentsToNiceKid;
  11. static boolean notRanOut = false;
  12.  
  13.  
  14. public static void main(String[] args) {
  15.  
  16. Scanner scan = new Scanner(System.in);
  17.  
  18. presentsCountToSpread = Integer.parseInt(scan.nextLine());
  19.  
  20. int n = Integer.parseInt(scan.nextLine());
  21.  
  22. char[][] field = new char[n][n];
  23.  
  24. for (int row = 0; row < n; row++) {
  25. String line = scan.nextLine().replaceAll(" ", "");
  26. if (line.contains("S")) {
  27. newRowHero = row;
  28. newColHero = line.indexOf("S");
  29. }
  30. if (line.contains("V")) {
  31. for (int i = 0; i < line.length(); i++) {
  32. if (line.charAt(i) == 'V') {
  33. kindKids++;
  34. }
  35. }
  36. }
  37. field[row] = line.toCharArray();
  38. }
  39.  
  40. boolean wasOutOfField = false;
  41.  
  42. // int newRow = -1;
  43. // int newCol = -1;
  44.  
  45.  
  46. String command = scan.nextLine();
  47. while (!command.equals("Christmas morning") && presentsCountToSpread > 0) {
  48.  
  49. if (command.equals("up")) {
  50. // row - 1
  51. if (isOutOfBounds(newRowHero - 1, newColHero, field)) {
  52. wasOutOfField = true;
  53. field[newRowHero][newColHero] = '-';
  54. break;
  55. } else {
  56. moveHero(newRowHero, newColHero, newRowHero - 1, newColHero, field);
  57. }
  58. } else if (command.equals("down")) {
  59. // row + 1
  60. if (isOutOfBounds(newRowHero + 1, newColHero, field)) {
  61. wasOutOfField = true;
  62. field[newRowHero][newColHero] = '-';
  63. break;
  64. } else {
  65. moveHero(newRowHero, newColHero, newRowHero + 1, newColHero, field);
  66. }
  67. } else if (command.equals("left")) {
  68. // col - 1
  69. if (isOutOfBounds(newRowHero, newColHero - 1, field)) {
  70. wasOutOfField = true;
  71. field[newRowHero][newColHero] = '-';
  72. break;
  73. } else {
  74. moveHero(newRowHero, newColHero, newRowHero, newColHero - 1, field);
  75. }
  76. } else if (command.equals("right")) {
  77. // col + 1
  78. if (isOutOfBounds(newRowHero, newColHero + 1, field)) {
  79. wasOutOfField = true;
  80. field[newRowHero][newColHero] = '-';
  81. break;
  82. } else {
  83. moveHero(newRowHero, newColHero, newRowHero, newColHero + 1, field);
  84. }
  85. }
  86. if (presentsCountToSpread > 0) {
  87. command = scan.nextLine();
  88. } else {
  89. break;
  90. }
  91. }
  92.  
  93. int niceKidsWoPresents = kindKids - givenPresentsToNiceKid;
  94.  
  95.  
  96. if (presentsCountToSpread <= 0 && !notRanOut) {
  97. System.out.println("Santa ran out of presents!");
  98. }
  99.  
  100. if (wasOutOfField) {
  101. System.out.println("Santa ran out of presents!");
  102. }
  103.  
  104. printMatrix(field);
  105.  
  106. if (givenPresentsToNiceKid >= kindKids) {
  107. System.out.println(String.format("Good job, Santa! %d happy nice kid/s.", givenPresentsToNiceKid));
  108. } else {
  109. System.out.println(String.format("No presents for %s nice kid/s.", niceKidsWoPresents));
  110. }
  111. }
  112.  
  113. private static void moveHero(int currentRowHero, int currentColHero, int willBeAtRow, int willBeAtCol, char[][] field) {
  114. if (field[willBeAtRow][willBeAtCol] == 'V') {
  115. presentsCountToSpread--;
  116. givenPresentsToNiceKid++;
  117. field[willBeAtRow][willBeAtCol] = 'S';
  118. field[currentRowHero][currentColHero] = '-';
  119. newRowHero = willBeAtRow;
  120. newColHero = willBeAtCol;
  121.  
  122. } else if (field[willBeAtRow][willBeAtCol] == 'X') {
  123. field[willBeAtRow][willBeAtCol] = 'S';
  124. field[currentRowHero][currentColHero] = '-';
  125. newRowHero = willBeAtRow;
  126. newColHero = willBeAtCol;
  127.  
  128. } else if (field[willBeAtRow][willBeAtCol] == 'C') {
  129. checkForKidsAround(willBeAtRow, willBeAtCol, field);
  130. field[willBeAtRow][willBeAtCol] = 'S';
  131. field[currentRowHero][currentColHero] = '-';
  132. newRowHero = willBeAtRow;
  133. newColHero = willBeAtCol;
  134. } else {
  135. field[willBeAtRow][willBeAtCol] = 'S';
  136. field[currentRowHero][currentColHero] = '-';
  137. newRowHero = willBeAtRow;
  138. newColHero = willBeAtCol;
  139. }
  140. }
  141.  
  142. private static void checkForKidsAround(int rowHero, int colHero, char[][] field) {
  143. if (field[rowHero][colHero - 1] == 'V' && presentsCountToSpread > 0) {
  144. presentsCountToSpread--;
  145. givenPresentsToNiceKid++;
  146. field[rowHero][colHero - 1] = '-';
  147. }else if (field[rowHero][colHero - 1] == 'X' && presentsCountToSpread > 0) {
  148. presentsCountToSpread--;
  149. field[rowHero][colHero - 1] = '-';
  150. }
  151. if (field[rowHero][colHero + 1] == 'V' && presentsCountToSpread > 0){
  152. presentsCountToSpread--;
  153. givenPresentsToNiceKid++;
  154. field[rowHero][colHero + 1] = '-';
  155. } else if (field[rowHero][colHero + 1] == 'X' && presentsCountToSpread > 0) {
  156. presentsCountToSpread--;
  157. field[rowHero][colHero + 1] = '-';
  158. }
  159. if (field[rowHero - 1][colHero] == 'V' && presentsCountToSpread > 0) {
  160. presentsCountToSpread--;
  161. givenPresentsToNiceKid++;
  162. field[rowHero - 1][colHero] = '-';
  163. }else if (field[rowHero - 1][colHero] == 'X' && presentsCountToSpread > 0) {
  164. presentsCountToSpread--;
  165. field[rowHero - 1][colHero] = '-';
  166. }
  167. if (field[rowHero + 1][colHero] == 'V' && presentsCountToSpread > 0) {
  168. presentsCountToSpread--;
  169. givenPresentsToNiceKid++;
  170. field[rowHero + 1][colHero] = '-';
  171. }else if (field[rowHero + 1][colHero] == 'X' && presentsCountToSpread > 0) {
  172. presentsCountToSpread--;
  173. field[rowHero + 1][colHero] = '-';
  174. }
  175. if (kindKids == givenPresentsToNiceKid && presentsCountToSpread == 0) {
  176. notRanOut = true;
  177. }
  178. }
  179.  
  180. private static boolean isOutOfBounds(int row, int col, char[][] field) {
  181. return row < 0 || row >= field.length
  182. || col < 0 || col >= field[row].length;
  183. }
  184.  
  185. public static void printMatrix(char[][] matrix) {
  186. for (int row = 0; row < matrix.length; row++) {
  187. for (int col = 0; col < matrix[row].length; col++) {
  188. System.out.print(matrix[row][col] + " ");
  189. }
  190. System.out.println();
  191. }
  192. }
  193. }
  194.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement