Advertisement
Didart

Navy Battle

Jan 23rd, 2023
932
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.72 KB | None | 0 0
  1. package MultidimensionalArrays2;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class NavyBattle {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.  
  9.         int n = Integer.parseInt(scanner.nextLine());
  10.  
  11.         String[][] matrix = new String[n][n];
  12.  
  13.         fillMatrix(matrix, scanner);
  14.  
  15.         int submarineRow = -1;
  16.         int submarineCol = -1;
  17.         int countHits = 0;
  18.         int countCruiser = 0;
  19.  
  20.         for (int row = 0; row < n; row++) {
  21.             for (int col = 0; col < n; col++) {
  22.                 if (matrix[row][col].equals("S")) {
  23.                     submarineRow = row;
  24.                     submarineCol = col;
  25.                     break;
  26.                 }
  27.             }
  28.         }
  29.         String command = scanner.nextLine();
  30.         while (true) {
  31.             matrix[submarineRow][submarineCol] = "-";
  32.             if (command.equals("up")) {
  33.                 submarineRow--;
  34.             } else if (command.equals("down")) {
  35.                 submarineRow++;
  36.             } else if (command.equals("left")) {
  37.                 submarineCol--;
  38.             } else if (command.equals("right")) {
  39.                 submarineCol++;
  40.             }
  41.             String submarinePosition = matrix[submarineRow][submarineCol];
  42.  
  43.             if (submarinePosition.equals("-")) {
  44.                 matrix[submarineRow][submarineCol] = "S";
  45.             } else if (submarinePosition.equals("*")) {
  46.                 matrix[submarineRow][submarineCol] = "S";
  47.                 countHits++;
  48.  
  49.                 if (countHits == 3) {
  50.                     System.out.printf("Mission failed, U-9 disappeared! Last known coordinates [%d, %d]!%n",
  51.                             submarineRow, submarineCol);
  52.                     break;
  53.                 }
  54.             } else if (submarinePosition.equals("C")) {
  55.                 matrix[submarineRow][submarineCol] = "S";
  56.                 countCruiser++;
  57.                 if (countCruiser == 3) {
  58.                     System.out.println("Mission accomplished, U-9 has destroyed all battle cruisers of the enemy!");
  59.                     break;
  60.                 }
  61.             }
  62.             command = scanner.nextLine();
  63.         }
  64.         printMatrix(matrix);
  65.     }
  66.  
  67.     private static void fillMatrix(String[][] matrix, Scanner scanner) {
  68.         for (int row = 0; row < matrix.length; row++) {
  69.             matrix[row] = scanner.nextLine().split("");
  70.         }
  71.     }
  72.     private static void printMatrix(String[][] matrix) {
  73.         for (int row = 0; row < matrix.length; row++) {
  74.             for (int col = 0; col < matrix.length; col++) {
  75.                 System.out.print(matrix[row][col]);
  76.             }
  77.             System.out.println();
  78.         }
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement