Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.10 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class ATheGardenMatrix {
  4. public static void main(String[] args) {
  5. Scanner scanner = new Scanner(System.in);
  6.  
  7. int lettuceCount = 0;
  8. int potatoesCount = 0;
  9. int carrotsCount = 0;
  10. int harmedVeggies = 0;
  11. int rows = Integer.parseInt(scanner.nextLine());
  12.  
  13. String[][] matrix = new String[rows][];
  14. for (int row = 0; row < matrix.length; row++) {
  15. String[] inputTokens = scanner.nextLine().split(" ");
  16. matrix[row] = inputTokens;
  17.  
  18. }
  19. String input = scanner.nextLine();
  20.  
  21. while (!input.equals("End of Harvest")) {
  22. String[] tokens = input.split("\\s+");
  23. String type = tokens[0];
  24. int row = Integer.parseInt(tokens[1]);
  25. int col = Integer.parseInt(tokens[2]);
  26.  
  27.  
  28.  
  29. switch (type) {
  30. case "Harvest":
  31. if (isInBounds(matrix,row,col)) {
  32. String toHarvest = matrix[row][col];
  33. // if (Character.isAlphabetic(toHarvest)) {
  34. if (toHarvest.equals("L")) {
  35. lettuceCount++;
  36. matrix = changeHarvest(matrix, row, col);
  37. } else if (toHarvest.equals("P")) {
  38. potatoesCount++;
  39. matrix = changeHarvest(matrix, row, col);
  40. } else if (toHarvest.equals("C")) {
  41. carrotsCount++;
  42. matrix = changeHarvest(matrix, row, col);
  43. }
  44. }
  45. // }
  46. break;
  47. case "Mole":
  48. if (isInBounds(matrix,row,col)) {
  49. String direction = tokens[3];
  50. String toMole;
  51. if (direction.equalsIgnoreCase("Up")) {
  52. for (int i = row; i >= 0; i -= 2) {
  53. matrix = changeMole(matrix, i, col);
  54. if (Character.isAlphabetic(matrix[row][i].charAt(0))) {
  55. harmedVeggies++;
  56. }
  57. }
  58. } else if (direction.equalsIgnoreCase("Down")) {
  59. for (int i = row; i < matrix.length; i += 2) {
  60. matrix = changeMole(matrix, i, col);
  61. if (Character.isAlphabetic(matrix[row][i].charAt(0))) {
  62. harmedVeggies++;
  63. }
  64.  
  65. }
  66. } else if (direction.equalsIgnoreCase("Left")) {
  67. for (int i = col; i >= 0; i -= 2) {
  68. matrix = changeMole(matrix, row, i);
  69. if (Character.isAlphabetic(matrix[row][i].charAt(0))) {
  70. harmedVeggies++;
  71. }
  72. }
  73. } else if (direction.equalsIgnoreCase("Right")) {
  74. for (int i = col; i <= matrix.length; i += 2) {
  75. if (Character.isAlphabetic(matrix[row][i].charAt(0))) {
  76. harmedVeggies++;
  77. }
  78. matrix = changeMole(matrix, row, i);
  79.  
  80.  
  81. }
  82. }
  83. }
  84. break;
  85. }
  86. // printMatrix(matrix);
  87. input = scanner.nextLine();
  88. }
  89. printMatrix(matrix);
  90. System.out.println("Carrots: " + carrotsCount);
  91. System.out.println("Potatoes: " + potatoesCount);
  92. System.out.println("Lettuce: " + lettuceCount);
  93. System.out.println("Harmed vegetables: " + harmedVeggies);
  94.  
  95.  
  96.  
  97. }
  98.  
  99. private static boolean isInBounds(String[][] matrix, int row, int col){
  100. boolean isBounds = false;
  101. if (row >= 0 && row < matrix.length){
  102. isBounds = true;
  103. }
  104. if (col >= 0 && col < matrix[row].length){
  105. isBounds = true;
  106. }
  107. return isBounds;
  108. }
  109. private static String[][] changeHarvest(String[][] matrix, int row, int col){
  110. matrix[row][col] = " ";
  111.  
  112. return matrix;
  113. }
  114.  
  115. private static String[][] changeMole(String[][] matrix, int row, int col) {
  116. matrix[row][col] = " ";
  117.  
  118. return matrix;
  119. }
  120.  
  121.  
  122. private static void printMatrix(String[][] matrix) {
  123. for (int row = 0; row < matrix.length; row++) {
  124. for (int col = 0; col < matrix[row].length; col++) {
  125. System.out.print(matrix[row][col] + " ");
  126. }
  127. System.out.println();
  128. }
  129. }
  130.  
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement