Advertisement
cankocanko

beeeeeeeeeeeee

Aug 19th, 2020
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.75 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. if (command.equals("End")){
  178. break;
  179. }
  180. }
  181.  
  182. if (gameOver && pollinatedFlowers >= 5) {
  183. System.out.println("The bee got lost!");
  184. System.out.println(String.format("Great job, the bee manage to pollinate %d flowers!", pollinatedFlowers));
  185. printMatrix(matrix);
  186. }
  187.  
  188. if (gameOver && pollinatedFlowers < 5) {
  189. System.out.println("The bee got lost!");
  190. System.out.println(String.format("The bee couldn't pollinate the flowers, she needed %d flowers more", 5 - pollinatedFlowers));
  191. printMatrix(matrix);
  192. }
  193. if (!gameOver && pollinatedFlowers >= 5) {
  194. System.out.println(String.format("Great job, the bee manage to pollinate %d flowers!", pollinatedFlowers));
  195. printMatrix(matrix);
  196. }
  197.  
  198. }
  199.  
  200. public static int moveUp(String[][] matrix, int playerRow) {
  201. if (playerRow > 0) {
  202. playerRow -= 1;
  203. }
  204. return playerRow;
  205. }
  206.  
  207. public static int moveDown(String[][] matrix, int playerRow) {
  208. if (playerRow < matrix.length - 1) {
  209. playerRow += 1;
  210. }
  211. return playerRow;
  212. }
  213.  
  214. public static int moveLeft(String[][] matrix, int playerCol) {
  215. if (playerCol > 0) {
  216. playerCol -= 1;
  217. }
  218. return playerCol;
  219. }
  220.  
  221. public static int moveRight(String[][] matrix, int playerCol) {
  222. if (playerCol < matrix.length - 1) {
  223. playerCol += 1;
  224. }
  225. return playerCol;
  226. }
  227.  
  228. public static void printMatrix(String[][] matrix) {
  229. for (int rows = 0; rows < matrix.length; rows++) {
  230. for (int cols = 0; cols < matrix[rows].length; cols++) {
  231. System.out.print(matrix[rows][cols]);
  232. }
  233. System.out.println();
  234. }
  235. }
  236.  
  237. }
  238.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement