Advertisement
cankocanko

BeeBeeBee

Aug 19th, 2020
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.33 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. }
  20. String command = sc.nextLine();
  21.  
  22. int pollinatedFlowers = 0;
  23.  
  24.  
  25. boolean gameOver = false;
  26.  
  27. while (!"end".equals(command) && !gameOver) {
  28.  
  29. int nextField = 0;
  30.  
  31.  
  32. for (int rows = 0; rows < matrix.length; rows++) {
  33. for (int cols = 0; cols < matrix[rows].length; cols++) {
  34. if (matrix[rows][cols].equals("B")) {
  35. playerRow = rows;
  36. playerCol = cols;
  37. }
  38. }
  39. }
  40.  
  41. switch (command) {
  42. case "up":
  43.  
  44. nextField = moveUp(matrix, playerRow);
  45.  
  46. if (playerRow == nextField) {
  47. matrix[playerRow][playerCol] = ".";
  48. gameOver = true;
  49. continue;
  50. }
  51.  
  52. matrix[playerRow][playerCol] = ".";
  53. playerRow = nextField;
  54. if (matrix[playerRow][playerCol].equals(".")) {
  55. matrix[playerRow][playerCol] = "B";
  56. }
  57.  
  58. if (matrix[playerRow][playerCol].equals("f")) {
  59. matrix[playerRow][playerCol] = "B";
  60. pollinatedFlowers += 1;
  61. }
  62.  
  63. if (matrix[playerRow][playerCol].equals("O")) {
  64. matrix[playerRow][playerCol] = ".";
  65. playerRow -= 1;
  66. matrix[playerRow][playerCol] = "B";
  67. }
  68.  
  69. break;
  70. case "down":
  71.  
  72. nextField = moveDown(matrix, playerRow);
  73.  
  74. if (playerRow == nextField) {
  75. matrix[playerRow][playerCol] = ".";
  76. gameOver = true;
  77. continue;
  78. }
  79.  
  80. matrix[playerRow][playerCol] = ".";
  81. playerRow = nextField;
  82. if (matrix[playerRow][playerCol].equals(".")) {
  83. matrix[playerRow][playerCol] = "B";
  84. }
  85.  
  86. if (matrix[playerRow][playerCol].equals("f")) {
  87. matrix[playerRow][playerCol] = "B";
  88. pollinatedFlowers += 1;
  89. }
  90.  
  91. if (matrix[playerRow][playerCol].equals("O")) {
  92. matrix[playerRow][playerCol] = ".";
  93. playerRow += 1;
  94. matrix[playerRow][playerCol] = "B";
  95. }
  96.  
  97. break;
  98. case "left":
  99.  
  100. nextField = moveLeft(matrix, playerCol);
  101.  
  102. if (playerCol == nextField) {
  103. matrix[playerRow][playerCol] = ".";
  104. gameOver = true;
  105. continue;
  106. }
  107.  
  108. matrix[playerRow][playerCol] = ".";
  109. playerCol = nextField;
  110. if (matrix[playerRow][playerCol].equals(".")) {
  111. matrix[playerRow][playerCol] = "B";
  112. }
  113.  
  114. if (matrix[playerRow][playerCol].equals("f")) {
  115. matrix[playerRow][playerCol] = "B";
  116. pollinatedFlowers += 1;
  117. }
  118.  
  119. if (matrix[playerRow][playerCol].equals("O")) {
  120. matrix[playerRow][playerCol] = ".";
  121. playerCol -= 1;
  122. matrix[playerRow][playerCol] = "B";
  123. }
  124.  
  125. break;
  126. case "right":
  127.  
  128. nextField = moveRight(matrix, playerCol);
  129.  
  130. if (playerCol == nextField) {
  131. matrix[playerRow][playerCol] = ".";
  132. gameOver = true;
  133. continue;
  134. }
  135.  
  136. matrix[playerRow][playerCol] = ".";
  137. playerCol = nextField;
  138. if (matrix[playerRow][playerCol].equals(".")) {
  139. matrix[playerRow][playerCol] = "B";
  140. }
  141.  
  142. if (matrix[playerRow][playerCol].equals("f")) {
  143. matrix[playerRow][playerCol] = "B";
  144. pollinatedFlowers += 1;
  145. }
  146.  
  147. if (matrix[playerRow][playerCol].equals("O")) {
  148. matrix[playerRow][playerCol] = ".";
  149. playerCol += 1;
  150. matrix[playerRow][playerCol] = "B";
  151. }
  152.  
  153. break;
  154. }
  155.  
  156. command = sc.nextLine();
  157. }
  158.  
  159. if (gameOver) {
  160. System.out.println(String.format("The bee couldn't pollinate the flowers, she needed %s flowers more"
  161. , 5 - pollinatedFlowers));
  162. }
  163. if (!gameOver && pollinatedFlowers >= 5) {
  164. System.out.println(String.format("Great job, the bee manage to pollinate %d flowers!", pollinatedFlowers));
  165. }
  166.  
  167. }
  168.  
  169. public static int moveUp(String[][] matrix, int playerRow) {
  170. if (playerRow > 0) {
  171. playerRow -= 1;
  172. }
  173. return playerRow;
  174. }
  175.  
  176. public static int moveDown(String[][] matrix, int playerRow) {
  177. if (playerRow < matrix.length - 1) {
  178. playerRow += 1;
  179. }
  180. return playerRow;
  181. }
  182.  
  183. public static int moveLeft(String[][] matrix, int playerCol) {
  184. if (playerCol > 0) {
  185. playerCol -= 1;
  186. }
  187. return playerCol;
  188. }
  189.  
  190. public static int moveRight(String[][] matrix, int playerCol) {
  191. if (playerCol < matrix.length - 1) {
  192. playerCol += 1;
  193. }
  194. return playerCol;
  195. }
  196. }
  197.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement