Advertisement
cankocanko

NewBeeeeee

Aug 19th, 2020
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.66 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Main {
  6.  
  7. static int playerRow = 0;
  8. static int playerCol = 0;
  9.  
  10. public static void main(String[] args) {
  11. Scanner sc = new Scanner(System.in);
  12.  
  13. int size = Integer.parseInt(sc.nextLine());
  14.  
  15. String[][] matrix = new String[size][size];
  16.  
  17. for (int rows = 0; rows < matrix.length; rows++) {
  18. String[] arr = sc.nextLine().split(""); // split by what ?
  19. matrix[rows] = arr;
  20. }
  21. String command = sc.nextLine();
  22.  
  23. int pollinatedFlowers = 0;
  24.  
  25.  
  26. boolean gameOver = false;
  27.  
  28. while (!"End".equals(command) && !gameOver) {
  29.  
  30. int nextField = 0;
  31.  
  32.  
  33. for (int rows = 0; rows < matrix.length; rows++) {
  34. for (int cols = 0; cols < matrix[rows].length; cols++) {
  35. if (matrix[rows][cols].equals("B")) {
  36. playerRow = rows;
  37. playerCol = cols;
  38. }
  39. }
  40. }
  41.  
  42. switch (command) {
  43. case "up":
  44.  
  45. nextField = moveUp(matrix, playerRow);
  46.  
  47. if (playerRow == nextField) {
  48. matrix[playerRow][playerCol] = ".";
  49. gameOver = true;
  50. continue;
  51. }
  52.  
  53. matrix[playerRow][playerCol] = ".";
  54. playerRow = nextField;
  55. if (matrix[playerRow][playerCol].equals(".")) {
  56. matrix[playerRow][playerCol] = "B";
  57. }
  58.  
  59. if (matrix[playerRow][playerCol].equals("f")) {
  60. matrix[playerRow][playerCol] = "B";
  61. pollinatedFlowers += 1;
  62. }
  63.  
  64. if (matrix[playerRow][playerCol].equals("O")) {
  65. matrix[playerRow][playerCol] = ".";
  66. playerRow -= 1;
  67.  
  68. if (matrix[playerRow][playerCol].equals("f")) {
  69. pollinatedFlowers += 1;
  70. }
  71. matrix[playerRow][playerCol] = "B";
  72. }
  73.  
  74. break;
  75. case "down":
  76.  
  77. nextField = moveDown(matrix, playerRow);
  78.  
  79. if (playerRow == nextField) {
  80. matrix[playerRow][playerCol] = ".";
  81. gameOver = true;
  82. continue;
  83. }
  84.  
  85. matrix[playerRow][playerCol] = ".";
  86. playerRow = nextField;
  87. if (matrix[playerRow][playerCol].equals(".")) {
  88. matrix[playerRow][playerCol] = "B";
  89. }
  90.  
  91. if (matrix[playerRow][playerCol].equals("f")) {
  92. matrix[playerRow][playerCol] = "B";
  93. pollinatedFlowers += 1;
  94. }
  95.  
  96. if (matrix[playerRow][playerCol].equals("O")) {
  97. matrix[playerRow][playerCol] = ".";
  98. playerRow += 1;
  99.  
  100. if (matrix[playerRow][playerCol].equals("f")) {
  101. pollinatedFlowers += 1;
  102. }
  103.  
  104. matrix[playerRow][playerCol] = "B";
  105. }
  106.  
  107. break;
  108. case "left":
  109.  
  110. nextField = moveLeft(matrix, playerCol);
  111.  
  112. if (playerCol == nextField) {
  113. matrix[playerRow][playerCol] = ".";
  114. gameOver = true;
  115. continue;
  116. }
  117.  
  118. matrix[playerRow][playerCol] = ".";
  119. playerCol = nextField;
  120. if (matrix[playerRow][playerCol].equals(".")) {
  121. matrix[playerRow][playerCol] = "B";
  122. }
  123.  
  124. if (matrix[playerRow][playerCol].equals("f")) {
  125. matrix[playerRow][playerCol] = "B";
  126. pollinatedFlowers += 1;
  127. }
  128.  
  129. if (matrix[playerRow][playerCol].equals("O")) {
  130. matrix[playerRow][playerCol] = ".";
  131. playerCol -= 1;
  132.  
  133. if (matrix[playerRow][playerCol].equals("f")) {
  134. pollinatedFlowers += 1;
  135. }
  136.  
  137. matrix[playerRow][playerCol] = "B";
  138. }
  139.  
  140. break;
  141. case "right":
  142.  
  143. nextField = moveRight(matrix, playerCol);
  144.  
  145. if (playerCol == nextField) {
  146. matrix[playerRow][playerCol] = ".";
  147. gameOver = true;
  148. continue;
  149. }
  150.  
  151. matrix[playerRow][playerCol] = ".";
  152. playerCol = nextField;
  153. if (matrix[playerRow][playerCol].equals(".")) {
  154. matrix[playerRow][playerCol] = "B";
  155. }
  156.  
  157. if (matrix[playerRow][playerCol].equals("f")) {
  158. matrix[playerRow][playerCol] = "B";
  159. pollinatedFlowers += 1;
  160. }
  161.  
  162. if (matrix[playerRow][playerCol].equals("O")) {
  163. matrix[playerRow][playerCol] = ".";
  164. playerCol += 1;
  165.  
  166. if (matrix[playerRow][playerCol].equals("f")) {
  167. pollinatedFlowers += 1;
  168. }
  169.  
  170. matrix[playerRow][playerCol] = "B";
  171. }
  172.  
  173. break;
  174. }
  175.  
  176. command = sc.nextLine();
  177. }
  178.  
  179. if (gameOver && pollinatedFlowers >= 5) {
  180. System.out.println("The bee got lost!");
  181. System.out.println(String.format("Great job, the bee manage to pollinate %d flowers!", pollinatedFlowers));
  182. printMatrix(matrix);
  183. }
  184.  
  185. if (gameOver && pollinatedFlowers < 5) {
  186. System.out.println("The bee got lost!");
  187. System.out.println(String.format("The bee couldn't pollinate the flowers, she needed %d flowers more", 5 - pollinatedFlowers));
  188. printMatrix(matrix);
  189. }
  190. if (!gameOver && pollinatedFlowers >= 5) {
  191. System.out.println(String.format("Great job, the bee manage to pollinate %d flowers!", pollinatedFlowers));
  192. printMatrix(matrix);
  193. }
  194.  
  195. }
  196.  
  197. public static int moveUp(String[][] matrix, int playerRow) {
  198. if (playerRow > 0) {
  199. playerRow -= 1;
  200. }
  201. return playerRow;
  202. }
  203.  
  204. public static int moveDown(String[][] matrix, int playerRow) {
  205. if (playerRow < matrix.length - 1) {
  206. playerRow += 1;
  207. }
  208. return playerRow;
  209. }
  210.  
  211. public static int moveLeft(String[][] matrix, int playerCol) {
  212. if (playerCol > 0) {
  213. playerCol -= 1;
  214. }
  215. return playerCol;
  216. }
  217.  
  218. public static int moveRight(String[][] matrix, int playerCol) {
  219. if (playerCol < matrix.length - 1) {
  220. playerCol += 1;
  221. }
  222. return playerCol;
  223. }
  224.  
  225. public static void printMatrix(String[][] matrix) {
  226. for (int rows = 0; rows < matrix.length; rows++) {
  227. for (int cols = 0; cols < matrix[rows].length; cols++) {
  228. System.out.print(matrix[rows][cols]);
  229. }
  230. System.out.println();
  231. }
  232. }
  233.  
  234. }
  235.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement