Advertisement
svephoto

Python [Java]

Jul 4th, 2021 (edited)
844
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.66 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Python {
  4.     private static int rowOfThePlayer;
  5.     private static int columnOfThePlayer;
  6.     private static String theEnemy = "";
  7.  
  8.     public static void main(String[] args) {
  9.         Scanner scanner = new Scanner(System.in);
  10.  
  11.         int dimensions = Integer.parseInt(scanner.nextLine());
  12.  
  13.         String[] commands = scanner.nextLine().split(", ");
  14.  
  15.         char[][] theMatrix = new char[dimensions][dimensions];
  16.  
  17.         for (int i = 0; i < dimensions; i++) {
  18.             String currentLine = scanner.nextLine().replaceAll("\\s+", "");
  19.  
  20.             if (currentLine.contains("s")) {
  21.                 rowOfThePlayer = i;
  22.                 columnOfThePlayer = currentLine.indexOf("s");
  23.             }
  24.  
  25.             theMatrix[i] = currentLine.toCharArray();
  26.         }
  27.  
  28.         int finalFoodCounter = 0;
  29.         int pythonLength = countTheFoodQuantity(theMatrix) + 1;
  30.  
  31.         for (String currentCommand : commands) {
  32.             switch (currentCommand) {
  33.                 case "up":
  34.                     moveUp(theMatrix);
  35.                     break;
  36.                 case "down":
  37.                     moveDown(theMatrix);
  38.                     break;
  39.                 case "left":
  40.                     moveLeft(theMatrix);
  41.                     break;
  42.                 case "right":
  43.                     moveRight(theMatrix);
  44.                     break;
  45.             }
  46.  
  47.             if ("e".equals(theEnemy)) {
  48.                 System.out.println("You lose! Killed by an enemy!");
  49.                 return;
  50.             }
  51.  
  52.             finalFoodCounter = countTheFoodQuantity(theMatrix);
  53.  
  54.             if (finalFoodCounter == 0) {
  55.                 System.out.printf("You win! Final python length is %d%n", pythonLength);
  56.                 return;
  57.             }
  58.         }
  59.  
  60.         System.out.printf("You lose! There is still %d food to be eaten.%n", finalFoodCounter);
  61.     }
  62.  
  63.     private static void moveUp(char[][] theMatrix) {
  64.         int previousRow = rowOfThePlayer;
  65.  
  66.         if (rowOfThePlayer - 1 < 0) {
  67.             rowOfThePlayer = theMatrix.length;
  68.         }
  69.  
  70.         if (theMatrix[rowOfThePlayer - 1][columnOfThePlayer] != 'e') {
  71.             if (rowOfThePlayer == theMatrix.length) {
  72.                 previousRow = 0;
  73.             }
  74.  
  75.             theMatrix[previousRow][columnOfThePlayer] = '*';
  76.             rowOfThePlayer--;
  77.  
  78.             theMatrix[rowOfThePlayer][columnOfThePlayer] = 's';
  79.         } else {
  80.             theEnemy = "e";
  81.         }
  82.     }
  83.  
  84.     private static void moveDown(char[][] theMatrix) {
  85.         int previousRow = rowOfThePlayer;
  86.  
  87.         if (rowOfThePlayer + 1 == theMatrix.length) {
  88.             rowOfThePlayer = -1;
  89.         }
  90.  
  91.         if (theMatrix[rowOfThePlayer + 1][columnOfThePlayer] != 'e') {
  92.             if (rowOfThePlayer == -1) {
  93.                 previousRow = theMatrix.length - 1;
  94.             }
  95.  
  96.             theMatrix[previousRow][columnOfThePlayer] = '*';
  97.             rowOfThePlayer++;
  98.  
  99.             theMatrix[rowOfThePlayer][columnOfThePlayer] = 's';
  100.         } else {
  101.             theEnemy = "e";
  102.         }
  103.     }
  104.  
  105.     private static void moveLeft(char[][] theMatrix) {
  106.         int previousColumn = columnOfThePlayer;
  107.  
  108.         if (columnOfThePlayer - 1 < 0) {
  109.             columnOfThePlayer = theMatrix.length;
  110.         }
  111.  
  112.         if (theMatrix[rowOfThePlayer][columnOfThePlayer - 1] != 'e') {
  113.             if (columnOfThePlayer == theMatrix.length) {
  114.                 previousColumn = 0;
  115.             }
  116.  
  117.             theMatrix[rowOfThePlayer][previousColumn] = '*';
  118.             columnOfThePlayer--;
  119.  
  120.             theMatrix[rowOfThePlayer][columnOfThePlayer] = 's';
  121.         } else {
  122.             theEnemy = "e";
  123.         }
  124.     }
  125.  
  126.     private static void moveRight(char[][] theMatrix) {
  127.         int previousColumn = columnOfThePlayer;
  128.  
  129.         if (columnOfThePlayer + 1 == theMatrix.length) {
  130.             columnOfThePlayer = -1;
  131.         }
  132.  
  133.         if (theMatrix[rowOfThePlayer][columnOfThePlayer + 1] != 'e') {
  134.             if (columnOfThePlayer == -1) {
  135.                 previousColumn = theMatrix.length - 1;
  136.             }
  137.  
  138.             theMatrix[rowOfThePlayer][previousColumn] = '*';
  139.             columnOfThePlayer++;
  140.  
  141.             theMatrix[rowOfThePlayer][columnOfThePlayer] = 's';
  142.         } else {
  143.             theEnemy = "e";
  144.         }
  145.     }
  146.  
  147.     private static int countTheFoodQuantity(char[][] matrix) {
  148.         int counter = 0;
  149.  
  150.         for (char[] rowsAndColumns : matrix) {
  151.             for (char character : rowsAndColumns) {
  152.                 if (character == 'f') {
  153.                     counter++;
  154.                 }
  155.             }
  156.         }
  157.  
  158.         return counter;
  159.     }
  160. }
  161.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement