borovaneca

NavyBattle

Mar 3rd, 2023
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.21 KB | None | 0 0
  1. package Advance.Exams.JavaAdvancedExam.December2022;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class NavyBattle {
  6.  
  7.     private static final char SUBMARINE = 'S';
  8.     private static final char BOMB = '*';
  9.     private static final char CRUISER = 'C';
  10.     private static final char EMPTY_SPOT = '-';
  11.     private static int submarineLives = 3;
  12.     private static int cruisers = 3;
  13.     public static void main(String[] args) {
  14.         Scanner scanner = new Scanner(System.in);
  15.  
  16.  
  17.         int size = Integer.parseInt(scanner.nextLine());
  18.         char[][] battlefield = new char[size][size];
  19.         fullFillBattlefield(scanner, battlefield);
  20.  
  21.  
  22.         int[] currentPosition = getCurrentPosition(battlefield);
  23.         while (submarineLives > 0 && cruisers > 0) {
  24.             String command = scanner.nextLine();
  25.             int row = currentPosition[0];
  26.             int col = currentPosition[1];
  27.  
  28.             switch (command) {
  29.                 case "right":
  30.                     if (battlefield[row][col + 1] == BOMB) {
  31.                         submarineLives--;
  32.  
  33.                     } else if (battlefield[row][col + 1] == CRUISER) {
  34.                         cruisers--;
  35.                     }
  36.                     currentPosition[1] = col + 1;
  37.                     battlefield[row][col] = EMPTY_SPOT;
  38.                     battlefield[row][col + 1] = SUBMARINE;
  39.                     break;
  40.                 case "down":
  41.                     if (battlefield[row + 1][col] == BOMB) {
  42.                         submarineLives--;
  43.  
  44.                     } else if (battlefield[row + 1][col] == CRUISER) {
  45.                         cruisers--;
  46.                     }
  47.                     currentPosition[0] = row + 1;
  48.                     battlefield[row][col] = EMPTY_SPOT;
  49.                     battlefield[row + 1][col] = SUBMARINE;
  50.                     break;
  51.                 case "left":
  52.                     if (battlefield[row][col - 1] == BOMB) {
  53.                         submarineLives--;
  54.  
  55.                     } else if (battlefield[row][col - 1] == CRUISER) {
  56.                         cruisers--;
  57.                     }
  58.                     currentPosition[1] = col - 1;
  59.                     battlefield[row][col] = EMPTY_SPOT;
  60.                     battlefield[row][col - 1] = SUBMARINE;
  61.                     break;
  62.                 case "up":
  63.                     if (battlefield[row - 1][col] == BOMB) {
  64.                         submarineLives--;
  65.  
  66.                     } else if (battlefield[row - 1][col] == CRUISER) {
  67.                         cruisers--;
  68.                     }
  69.                     currentPosition[0] = row - 1;
  70.                     battlefield[row][col] = EMPTY_SPOT;
  71.                     battlefield[row - 1][col] = SUBMARINE;
  72.                     break;
  73.             }
  74.         }
  75.  
  76.         if (submarineLives < 1) {
  77.             System.out.printf("Mission failed, U-9 disappeared! Last known coordinates [%d, %d]!\n", currentPosition[0], currentPosition[1]);
  78.         } else  {
  79.             System.out.println("Mission accomplished, U-9 has destroyed all battle cruisers of the enemy!");
  80.         }
  81.         printBattlefield(battlefield);
  82.     }
  83.  
  84.     private static void fullFillBattlefield(Scanner scanner, char[][] battlefield) {
  85.         for (int i = 0; i < battlefield.length; i++) {
  86.             String currentInput = scanner.nextLine();
  87.             for (int j = 0; j < currentInput.length(); j++) {
  88.                 battlefield[i][j] = currentInput.charAt(j);
  89.             }
  90.         }
  91.     }
  92.  
  93.     private static void printBattlefield(char[][] battlefield) {
  94.         for (int row = 0; row < battlefield.length; row++) {
  95.             for (int col = 0; col < battlefield.length; col++) {
  96.                 System.out.print(battlefield[row][col]);
  97.             }
  98.             System.out.println();
  99.         }
  100.     }
  101.  
  102.     private static int[] getCurrentPosition(char[][] battlefield) {
  103.         int[] position = new int[2];
  104.         for (int row = 0; row < battlefield.length; row++) {
  105.             for (int col = 0; col < battlefield[row].length; col++) {
  106.                 if (battlefield[row][col] == 'S') {
  107.                    return position = new int[] {row, col};
  108.                 }
  109.             }
  110.         }
  111.         return position;
  112.     }
  113. }
  114.  
Advertisement
Add Comment
Please, Sign In to add comment