desislava_topuzakova

02. Python

Feb 12th, 2022
1,067
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.37 KB | None | 0 0
  1. package ExamPreparation;
  2.  
  3. import java.util.Arrays;
  4. import java.util.Scanner;
  5.  
  6. public class Python_02 {
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.         int n = Integer.parseInt(scanner.nextLine()); //брой редове = брой колони
  10.         String [] commands = scanner.nextLine().split(",\\s+");
  11.  
  12.         char[][] matrix = new char[n][n];
  13.         //fill matrix
  14.         for (int row = 0; row < n; row++) {
  15.             //* * * * * -> *****
  16.             matrix[row] = scanner.nextLine().replaceAll(" ", "").toCharArray();
  17.         }
  18.  
  19.         //find python
  20.         //find all food
  21.         int countFood = 0; //общо количество храна в полето
  22.         int rowPython = 0;
  23.         int colPython = 0;
  24.         for (int row = 0; row < n; row++) {
  25.             for (int col = 0; col < n; col++) {
  26.                 char currentElement = matrix[row][col];
  27.                 if (currentElement == 's') {
  28.                     rowPython = row;
  29.                     colPython = col;
  30.                 } else if (currentElement == 'f') {
  31.                     countFood++;
  32.                 }
  33.             }
  34.         }
  35.  
  36.         //moves
  37.         int length = 1; //дължина на змията
  38.         boolean  isDead = false; //не съм умрял
  39.         for (String command : commands) {
  40.             matrix[rowPython][colPython] = '*';
  41.             //left/right/up/down
  42.             switch (command) {
  43.                 case "left":
  44.                     colPython--;
  45.                     break;
  46.                 case "right":
  47.                     colPython++;
  48.                     break;
  49.                 case "up":
  50.                     rowPython--;
  51.                     break;
  52.                 case "down":
  53.                     rowPython++;
  54.                     break;
  55.             }
  56.  
  57.             //след преместването дали сме излязли извън полето
  58.             if (rowPython < 0 || rowPython >= n) {
  59.                 if (rowPython < 0) {
  60.                     rowPython = n - 1;
  61.                 }
  62.  
  63.                 if (rowPython >= n) {
  64.                     rowPython = 0;
  65.                 }
  66.             }
  67.  
  68.             if (colPython < 0 || colPython >= n) {
  69.                 if (colPython < 0) {
  70.                     colPython = n - 1;
  71.                 }
  72.  
  73.                 if (colPython >= n) {
  74.                     colPython = 0;
  75.                 }
  76.             }
  77.  
  78.             if (countFood == 0) { //зяли цялата храна от полето
  79.                 break;
  80.             }
  81.  
  82.             //проверка къде сме отишли
  83.             if (matrix[rowPython][colPython] == 'f') {
  84.                 length++;
  85.                 countFood--; //изяждаме и намаляме брой на храните в полето
  86.             }
  87.  
  88.             if (matrix[rowPython][colPython] == 'e') {
  89.                 isDead = true;
  90.                 break;
  91.             }
  92.  
  93.             matrix[rowPython][colPython] = 's';
  94.         }
  95.  
  96.         if (countFood == 0) {
  97.             System.out.println("You win! Final python length is " + length);
  98.         } else if (isDead) {
  99.             System.out.println("You lose! Killed by an enemy!");
  100.         } else {
  101.             System.out.printf("You lose! There is still %d food to be eaten.", countFood);
  102.         }
  103.  
  104.     }
  105. }
  106.  
Advertisement
Add Comment
Please, Sign In to add comment