Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.Scanner;
- public class Main {
- static int playerRow = 0;
- static int playerCol = 0;
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- int size = Integer.parseInt(sc.nextLine());
- String[][] matrix = new String[size][size];
- for (int rows = 0; rows < matrix.length; rows++) {
- String[] arr = sc.nextLine().split(""); // split by what ?
- matrix[rows] = arr;
- }
- String command = sc.nextLine();
- int pollinatedFlowers = 0;
- boolean gameOver = false;
- while (!"End".equals(command) && !gameOver) {
- int nextField = 0;
- for (int rows = 0; rows < matrix.length; rows++) {
- for (int cols = 0; cols < matrix[rows].length; cols++) {
- if (matrix[rows][cols].equals("B")) {
- playerRow = rows;
- playerCol = cols;
- }
- }
- }
- switch (command) {
- case "up":
- nextField = moveUp(matrix, playerRow);
- if (playerRow == nextField) {
- matrix[playerRow][playerCol] = ".";
- gameOver = true;
- continue;
- }
- matrix[playerRow][playerCol] = ".";
- playerRow = nextField;
- if (matrix[playerRow][playerCol].equals(".")) {
- matrix[playerRow][playerCol] = "B";
- }
- if (matrix[playerRow][playerCol].equals("f")) {
- matrix[playerRow][playerCol] = "B";
- pollinatedFlowers += 1;
- }
- if (matrix[playerRow][playerCol].equals("O")) {
- matrix[playerRow][playerCol] = ".";
- playerRow -= 1;
- if (matrix[playerRow][playerCol].equals("f")) {
- pollinatedFlowers += 1;
- }
- matrix[playerRow][playerCol] = "B";
- }
- break;
- case "down":
- nextField = moveDown(matrix, playerRow);
- if (playerRow == nextField) {
- matrix[playerRow][playerCol] = ".";
- gameOver = true;
- continue;
- }
- matrix[playerRow][playerCol] = ".";
- playerRow = nextField;
- if (matrix[playerRow][playerCol].equals(".")) {
- matrix[playerRow][playerCol] = "B";
- }
- if (matrix[playerRow][playerCol].equals("f")) {
- matrix[playerRow][playerCol] = "B";
- pollinatedFlowers += 1;
- }
- if (matrix[playerRow][playerCol].equals("O")) {
- matrix[playerRow][playerCol] = ".";
- playerRow += 1;
- if (matrix[playerRow][playerCol].equals("f")) {
- pollinatedFlowers += 1;
- }
- matrix[playerRow][playerCol] = "B";
- }
- break;
- case "left":
- nextField = moveLeft(matrix, playerCol);
- if (playerCol == nextField) {
- matrix[playerRow][playerCol] = ".";
- gameOver = true;
- continue;
- }
- matrix[playerRow][playerCol] = ".";
- playerCol = nextField;
- if (matrix[playerRow][playerCol].equals(".")) {
- matrix[playerRow][playerCol] = "B";
- }
- if (matrix[playerRow][playerCol].equals("f")) {
- matrix[playerRow][playerCol] = "B";
- pollinatedFlowers += 1;
- }
- if (matrix[playerRow][playerCol].equals("O")) {
- matrix[playerRow][playerCol] = ".";
- playerCol -= 1;
- if (matrix[playerRow][playerCol].equals("f")) {
- pollinatedFlowers += 1;
- }
- matrix[playerRow][playerCol] = "B";
- }
- break;
- case "right":
- nextField = moveRight(matrix, playerCol);
- if (playerCol == nextField) {
- matrix[playerRow][playerCol] = ".";
- gameOver = true;
- continue;
- }
- matrix[playerRow][playerCol] = ".";
- playerCol = nextField;
- if (matrix[playerRow][playerCol].equals(".")) {
- matrix[playerRow][playerCol] = "B";
- }
- if (matrix[playerRow][playerCol].equals("f")) {
- matrix[playerRow][playerCol] = "B";
- pollinatedFlowers += 1;
- }
- if (matrix[playerRow][playerCol].equals("O")) {
- matrix[playerRow][playerCol] = ".";
- playerCol += 1;
- if (matrix[playerRow][playerCol].equals("f")) {
- pollinatedFlowers += 1;
- }
- matrix[playerRow][playerCol] = "B";
- }
- break;
- }
- command = sc.nextLine();
- }
- if (gameOver && pollinatedFlowers >= 5) {
- System.out.println("The bee got lost!");
- System.out.println(String.format("Great job, the bee manage to pollinate %d flowers!", pollinatedFlowers));
- printMatrix(matrix);
- }
- if (gameOver && pollinatedFlowers < 5) {
- System.out.println("The bee got lost!");
- System.out.println(String.format("The bee couldn't pollinate the flowers, she needed %d flowers more", 5 - pollinatedFlowers));
- printMatrix(matrix);
- }
- if (!gameOver && pollinatedFlowers >= 5) {
- System.out.println(String.format("Great job, the bee manage to pollinate %d flowers!", pollinatedFlowers));
- printMatrix(matrix);
- }
- }
- public static int moveUp(String[][] matrix, int playerRow) {
- if (playerRow > 0) {
- playerRow -= 1;
- }
- return playerRow;
- }
- public static int moveDown(String[][] matrix, int playerRow) {
- if (playerRow < matrix.length - 1) {
- playerRow += 1;
- }
- return playerRow;
- }
- public static int moveLeft(String[][] matrix, int playerCol) {
- if (playerCol > 0) {
- playerCol -= 1;
- }
- return playerCol;
- }
- public static int moveRight(String[][] matrix, int playerCol) {
- if (playerCol < matrix.length - 1) {
- playerCol += 1;
- }
- return playerCol;
- }
- public static void printMatrix(String[][] matrix) {
- for (int rows = 0; rows < matrix.length; rows++) {
- for (int cols = 0; cols < matrix[rows].length; cols++) {
- System.out.print(matrix[rows][cols]);
- }
- System.out.println();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement