Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Python {
- private static int rowOfThePlayer;
- private static int columnOfThePlayer;
- private static String theEnemy = "";
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int dimensions = Integer.parseInt(scanner.nextLine());
- String[] commands = scanner.nextLine().split(", ");
- char[][] theMatrix = new char[dimensions][dimensions];
- for (int i = 0; i < dimensions; i++) {
- String currentLine = scanner.nextLine().replaceAll("\\s+", "");
- if (currentLine.contains("s")) {
- rowOfThePlayer = i;
- columnOfThePlayer = currentLine.indexOf("s");
- }
- theMatrix[i] = currentLine.toCharArray();
- }
- int finalFoodCounter = 0;
- int pythonLength = countTheFoodQuantity(theMatrix) + 1;
- for (String currentCommand : commands) {
- switch (currentCommand) {
- case "up":
- moveUp(theMatrix);
- break;
- case "down":
- moveDown(theMatrix);
- break;
- case "left":
- moveLeft(theMatrix);
- break;
- case "right":
- moveRight(theMatrix);
- break;
- }
- if ("e".equals(theEnemy)) {
- System.out.println("You lose! Killed by an enemy!");
- return;
- }
- finalFoodCounter = countTheFoodQuantity(theMatrix);
- if (finalFoodCounter == 0) {
- System.out.printf("You win! Final python length is %d%n", pythonLength);
- return;
- }
- }
- System.out.printf("You lose! There is still %d food to be eaten.%n", finalFoodCounter);
- }
- private static void moveUp(char[][] theMatrix) {
- int previousRow = rowOfThePlayer;
- if (rowOfThePlayer - 1 < 0) {
- rowOfThePlayer = theMatrix.length;
- }
- if (theMatrix[rowOfThePlayer - 1][columnOfThePlayer] != 'e') {
- if (rowOfThePlayer == theMatrix.length) {
- previousRow = 0;
- }
- theMatrix[previousRow][columnOfThePlayer] = '*';
- rowOfThePlayer--;
- theMatrix[rowOfThePlayer][columnOfThePlayer] = 's';
- } else {
- theEnemy = "e";
- }
- }
- private static void moveDown(char[][] theMatrix) {
- int previousRow = rowOfThePlayer;
- if (rowOfThePlayer + 1 == theMatrix.length) {
- rowOfThePlayer = -1;
- }
- if (theMatrix[rowOfThePlayer + 1][columnOfThePlayer] != 'e') {
- if (rowOfThePlayer == -1) {
- previousRow = theMatrix.length - 1;
- }
- theMatrix[previousRow][columnOfThePlayer] = '*';
- rowOfThePlayer++;
- theMatrix[rowOfThePlayer][columnOfThePlayer] = 's';
- } else {
- theEnemy = "e";
- }
- }
- private static void moveLeft(char[][] theMatrix) {
- int previousColumn = columnOfThePlayer;
- if (columnOfThePlayer - 1 < 0) {
- columnOfThePlayer = theMatrix.length;
- }
- if (theMatrix[rowOfThePlayer][columnOfThePlayer - 1] != 'e') {
- if (columnOfThePlayer == theMatrix.length) {
- previousColumn = 0;
- }
- theMatrix[rowOfThePlayer][previousColumn] = '*';
- columnOfThePlayer--;
- theMatrix[rowOfThePlayer][columnOfThePlayer] = 's';
- } else {
- theEnemy = "e";
- }
- }
- private static void moveRight(char[][] theMatrix) {
- int previousColumn = columnOfThePlayer;
- if (columnOfThePlayer + 1 == theMatrix.length) {
- columnOfThePlayer = -1;
- }
- if (theMatrix[rowOfThePlayer][columnOfThePlayer + 1] != 'e') {
- if (columnOfThePlayer == -1) {
- previousColumn = theMatrix.length - 1;
- }
- theMatrix[rowOfThePlayer][previousColumn] = '*';
- columnOfThePlayer++;
- theMatrix[rowOfThePlayer][columnOfThePlayer] = 's';
- } else {
- theEnemy = "e";
- }
- }
- private static int countTheFoodQuantity(char[][] matrix) {
- int counter = 0;
- for (char[] rowsAndColumns : matrix) {
- for (char character : rowsAndColumns) {
- if (character == 'f') {
- counter++;
- }
- }
- }
- return counter;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement