Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class ATheGardenMatrix {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int lettuceCount = 0;
- int potatoesCount = 0;
- int carrotsCount = 0;
- int harmedVeggies = 0;
- int rows = Integer.parseInt(scanner.nextLine());
- String[][] matrix = new String[rows][];
- for (int row = 0; row < matrix.length; row++) {
- String[] inputTokens = scanner.nextLine().split(" ");
- matrix[row] = inputTokens;
- }
- String input = scanner.nextLine();
- while (!input.equals("End of Harvest")) {
- String[] tokens = input.split("\\s+");
- String type = tokens[0];
- int row = Integer.parseInt(tokens[1]);
- int col = Integer.parseInt(tokens[2]);
- switch (type) {
- case "Harvest":
- if (isInBounds(matrix,row,col)) {
- String toHarvest = matrix[row][col];
- // if (Character.isAlphabetic(toHarvest)) {
- if (toHarvest.equals("L")) {
- lettuceCount++;
- matrix = changeHarvest(matrix, row, col);
- } else if (toHarvest.equals("P")) {
- potatoesCount++;
- matrix = changeHarvest(matrix, row, col);
- } else if (toHarvest.equals("C")) {
- carrotsCount++;
- matrix = changeHarvest(matrix, row, col);
- }
- }
- // }
- break;
- case "Mole":
- if (isInBounds(matrix,row,col)) {
- String direction = tokens[3];
- String toMole;
- if (direction.equalsIgnoreCase("Up")) {
- for (int i = row; i >= 0; i -= 2) {
- matrix = changeMole(matrix, i, col);
- if (Character.isAlphabetic(matrix[row][i].charAt(0))) {
- harmedVeggies++;
- }
- }
- } else if (direction.equalsIgnoreCase("Down")) {
- for (int i = row; i < matrix.length; i += 2) {
- matrix = changeMole(matrix, i, col);
- if (Character.isAlphabetic(matrix[row][i].charAt(0))) {
- harmedVeggies++;
- }
- }
- } else if (direction.equalsIgnoreCase("Left")) {
- for (int i = col; i >= 0; i -= 2) {
- matrix = changeMole(matrix, row, i);
- if (Character.isAlphabetic(matrix[row][i].charAt(0))) {
- harmedVeggies++;
- }
- }
- } else if (direction.equalsIgnoreCase("Right")) {
- for (int i = col; i <= matrix.length; i += 2) {
- if (Character.isAlphabetic(matrix[row][i].charAt(0))) {
- harmedVeggies++;
- }
- matrix = changeMole(matrix, row, i);
- }
- }
- }
- break;
- }
- // printMatrix(matrix);
- input = scanner.nextLine();
- }
- printMatrix(matrix);
- System.out.println("Carrots: " + carrotsCount);
- System.out.println("Potatoes: " + potatoesCount);
- System.out.println("Lettuce: " + lettuceCount);
- System.out.println("Harmed vegetables: " + harmedVeggies);
- }
- private static boolean isInBounds(String[][] matrix, int row, int col){
- boolean isBounds = false;
- if (row >= 0 && row < matrix.length){
- isBounds = true;
- }
- if (col >= 0 && col < matrix[row].length){
- isBounds = true;
- }
- return isBounds;
- }
- private static String[][] changeHarvest(String[][] matrix, int row, int col){
- matrix[row][col] = " ";
- return matrix;
- }
- private static String[][] changeMole(String[][] matrix, int row, int col) {
- matrix[row][col] = " ";
- return matrix;
- }
- private static void printMatrix(String[][] matrix) {
- for (int row = 0; row < matrix.length; row++) {
- for (int col = 0; col < matrix[row].length; col++) {
- System.out.print(matrix[row][col] + " ");
- }
- System.out.println();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement