Advertisement
madifz

battleship_java

Jan 23rd, 2022
685
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package battleship;
  2.  
  3. import java.util.*;
  4.  
  5. class Main {
  6.     public static void main(String[] args) {
  7.  
  8.         Ship[] ships = {new Ship("Destroyer", 2),
  9.                 new Ship("Cruiser", 3),
  10.                 new Ship("Submarine", 3),
  11.                 new Ship("Battleship", 4),
  12.                 new Ship("Aircraft Carrier", 5)};
  13.         Battlefield myBattlefield = new Battlefield(ships);
  14.  
  15.         GamePlay.PlacementOfShips(myBattlefield);
  16.     }
  17. }
  18.  
  19. class GamePlay extends GameRules {
  20.     static Scanner scanner = new Scanner(System.in);
  21.  
  22.     public static void PlacementOfShips(Battlefield myBattlefield) {
  23.         myBattlefield.createBattlefield();
  24.         myBattlefield.printBattlefield();
  25.  
  26.         while (myBattlefield.getUnplacedShips() != 0) {
  27.  
  28.             System.out.printf("\nEnter the coordinates of the %s (%d cells):\n\n",
  29.                     myBattlefield.getShip().getNAME(), myBattlefield.getShip().getSIZE());
  30.  
  31.             while (true) {
  32.                 try {
  33.                     myBattlefield.getShip().setDeckCoordinates(scanner.nextLine().toUpperCase(Locale.ROOT).split(" "));
  34.  
  35.                     CheckShipPlacementRules(myBattlefield);
  36.  
  37.                     myBattlefield.placeShipOnBattlefield();
  38.                     myBattlefield.printBattlefield();
  39.                     break;
  40.                 } catch (Exception e) {
  41.                     System.out.println(e.getMessage().contains("Error") ? "\n" + e.getMessage()
  42.                             : "\n" + new Exception(String.format("Error! %s. Try again:" + "\n",
  43.                             e.getLocalizedMessage())).getMessage());
  44.                 }
  45.             }
  46.         }
  47.         scanner.close();
  48.     }
  49. }
  50.  
  51. class Battlefield {
  52.     private final char[][] BATTLEFIELD = new char[12][12];;
  53.     private final Ship[] SHIPS;
  54.     private int unplacedShips;
  55.  
  56.     public Battlefield(Ship[] ships) {
  57.         SHIPS = ships.clone();
  58.         this.unplacedShips = ships.length;
  59.     }
  60.  
  61.     public char[][] getBATTLEFIELD() {
  62.         return BATTLEFIELD;
  63.     }
  64.  
  65.     public Ship getShip() {
  66.         return this.SHIPS[this.unplacedShips - 1];
  67.     }
  68.  
  69.     public int getUnplacedShips() {
  70.         return this.unplacedShips;
  71.     }
  72.  
  73.     public void createBattlefield() {
  74.         for (int i = 1; i < BATTLEFIELD.length - 1; i++) {
  75.             for (int j = 1; j < BATTLEFIELD.length - 1; j++) {
  76.                 BATTLEFIELD[i][j] = '~';
  77.             }
  78.         }
  79.     }
  80.  
  81.     public void printBattlefield() {
  82.         System.out.println("\n  1 2 3 4 5 6 7 8 9 10");
  83.         for (int i = 1; i < BATTLEFIELD.length - 1; i++) {
  84.             System.out.print((char) ('@' + i) + " ");
  85.             for (int j = 1; j < BATTLEFIELD.length - 1; j++) {
  86.                 System.out.print(j == BATTLEFIELD.length - 2 ? BATTLEFIELD[i][j] + "\n" : BATTLEFIELD[i][j] + " ");
  87.             }
  88.         }
  89.     }
  90.  
  91.     public void placeShipOnBattlefield() {
  92.         for (int i = 0; i < getShip().getDeckCoordinates()[0].length; i++) {
  93.             BATTLEFIELD[getShip().getDeckCoordinates()[0][i]][getShip().getDeckCoordinates()[1][i]] = 'O';
  94.         }
  95.         this.unplacedShips--;
  96.     }
  97. }
  98.  
  99. class Ship {
  100.     private final String NAME;
  101.     private final int SIZE;
  102.     private int[][] deckCoordinates;
  103.  
  104.     public Ship(String name, int size) {
  105.         NAME = name;
  106.         SIZE = size;
  107.     }
  108.  
  109.     public String getNAME() {
  110.         return NAME;
  111.     }
  112.  
  113.     public int getSIZE() {
  114.         return SIZE;
  115.     }
  116.  
  117.     public void setDeckCoordinates(String[] coordinates) {
  118.  
  119.         int[] row = {coordinates[0].charAt(0) - '@', coordinates[1].charAt(0) - '@'};
  120.         int[] col = {Integer.parseInt(coordinates[0].substring(1)), Integer.parseInt(coordinates[1].substring(1))};
  121.  
  122.         this.deckCoordinates = new int[2][Math.abs((row[0] + col[0]) - (row[1] + col[1])) + 1];
  123.  
  124.         for (int i = 0; i < this.deckCoordinates[0].length; i++) {
  125.             this.deckCoordinates[0][i] = row[0] == row[1] ? Math.min(row[0], row[1]) : Math.min(row[0], row[1]) + i;
  126.             this.deckCoordinates[1][i] = row[0] == row[1] ? Math.min(col[0], col[1]) + i : Math.min(col[0], col[1]);
  127.         }
  128.     }
  129.  
  130.     public int[][] getDeckCoordinates() {
  131.         return this.deckCoordinates;
  132.     }
  133. }
  134.  
  135. class GameRules {
  136.     public static void CheckShipPlacementRules(Battlefield myBattlefield) throws Exception {
  137.         int[][] d = myBattlefield.getShip().getDeckCoordinates();
  138.         if (d[0][0] != d[0][d[0].length - 1] && d[1][0] != d[1][d[0].length - 1]
  139.                 || d[0][d[0].length - 1] > 10 || d[1][d[0].length - 1] > 10) {
  140.             throw new Exception("Error! Wrong ship location! Try again:\n");
  141.         } else if (myBattlefield.getShip().getSIZE() != d[0].length) {
  142.             throw new Exception(String.format("Error! Wrong length of the %s! Try again:\n", myBattlefield.getShip().getNAME()));
  143.         } else {
  144.             for (int i = 0; i < d[0].length; i++) {
  145.                 for (int j = d[0][i] - 1; j <= d[0][i] + 1; j++) {
  146.                     for (int l = d[1][i] - 1; l <= d[1][i] + 1; l++) {
  147.                         if (myBattlefield.getBATTLEFIELD()[j][l] == 'O') {
  148.                             throw new Exception("Error! You placed it too close to another one. Try again:\n");
  149.                         }
  150.                     }
  151.                 }
  152.             }
  153.         }
  154.     }
  155. }
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement