Advertisement
nbzhk

P02Python

Oct 12th, 2023 (edited)
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.72 KB | None | 0 0
  1. package Exams.Exam26June2021;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class P02Python {
  6.     private static int row;
  7.     private static int col;
  8.     private static int food;
  9.     private static int length;
  10.  
  11.     public static void main(String[] args) {
  12.         Scanner scanner = new Scanner(System.in);
  13.  
  14.         length = 1;
  15.  
  16.         int n = Integer.parseInt(scanner.nextLine());
  17.  
  18.         String[] commands = scanner.nextLine().split(", ");
  19.  
  20.         char[][] screen = fillScreen(n, scanner);
  21.  
  22.         boolean steppedOnEnemy = false;
  23.  
  24.         for (String command : commands) {
  25.             switch (command) {
  26.                 case "up":
  27.                     if (row - 1 < 0) {
  28.                         row = n - 1;
  29.                     } else {
  30.                         row--;
  31.                     }
  32.                     break;
  33.                 case "down":
  34.                     if (row + 1 > n - 1) {
  35.                         row = 0;
  36.                     } else {
  37.                         row++;
  38.                     }
  39.                     break;
  40.                 case "left":
  41.                     if (col - 1 < 0) {
  42.                         col = n - 1;
  43.                     } else {
  44.                         col--;
  45.                     }
  46.                     break;
  47.                 case "right":
  48.                     if (col + 1 > n - 1) {
  49.                         col = 0;
  50.                     } else {
  51.                         col++;
  52.                     }
  53.                     break;
  54.             }
  55.  
  56.             if (screen[row][col] == 'e') {
  57.                 steppedOnEnemy = true;
  58.             } else if (screen[row][col] == 'f') {
  59.                 length++;
  60.                 food--;
  61.             }
  62.  
  63.             if (food == 0 || steppedOnEnemy) {
  64.                 break;
  65.             }
  66.         }
  67.        
  68.         if (food == 0) {
  69.             System.out.printf("You win! Final python length is %d%n", length);
  70.         } else if (steppedOnEnemy) {
  71.             System.out.println("You lose! Killed by an enemy!");
  72.         } else {
  73.             System.out.printf("You lose! There is still %d food to be eaten.%n", food);
  74.  
  75.         }
  76.     }
  77.  
  78.  
  79.  
  80.     private static char[][] fillScreen(int n, Scanner scanner) {
  81.         char[][] screen = new char[n][n];
  82.         for (int r = 0; r < screen.length; r++) {
  83.             String input = scanner.nextLine().replaceAll(" ", "");
  84.             for (int c = 0; c < screen[r].length; c++) {
  85.                 if (input.charAt(c) == 's') {
  86.                     row = r;
  87.                     col = c;
  88.                 } else if (input.charAt(c) == 'f') {
  89.                     food++;
  90.                 }
  91.                 screen[r][c] = input.charAt(c);
  92.             }
  93.         }
  94.  
  95.         return screen;
  96.     }
  97. }
  98.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement