Atticool

Untitled

Jun 4th, 2023
698
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.01 KB | None | 0 0
  1. public class Board {
  2.     /*
  3.           1 2 3 4 5 6 7 8 9 10
  4.         A . . . . . . . . . .
  5.         B . . . . . . . . . .
  6.         C . . . . . . . . . .
  7.         D . . . . . . . . . .
  8.         E . . . . . . . . . .
  9.         F . . . . . . . . . .
  10.         G . . . . . . . . . .
  11.         H . . . . . . . . . .
  12.         I . . . . . . . . . .
  13.         J . . . . . . . . . .
  14.  
  15.         cells[x][y]
  16.         x is number, y is letter
  17.  
  18.        
  19.     */
  20.     Cell[][] cells = new Cell[10][10];
  21.  
  22.  
  23.     public Board() {
  24.             //place cells
  25.         for (int i = 0; i < 10; i++) {
  26.             for (int j = 0; j < 10; j++) {
  27.                 cells[i][j] = new Cell();
  28.             }
  29.         }
  30.     }
  31.  
  32.  
  33.     private static int convertLetterIndex(String letter) throws InvalidPositionException {
  34.         return switch (letter.toLowerCase()) {
  35.             case "a" -> 0;
  36.             case "b" -> 1;
  37.             case "c" -> 2;
  38.             case "d" -> 3;
  39.             case "e" -> 4;
  40.             case "f" -> 5;
  41.             case "g" -> 6;
  42.             case "h" -> 7;
  43.             case "i" -> 8;
  44.             case "j" -> 9;
  45.             default -> {
  46.                 throw new InvalidPositionException();
  47.             }
  48.         };
  49.     }
  50.  
  51.     private static int convertNumberIndex(String letter) throws InvalidPositionException {
  52.         return switch (letter.toLowerCase()) {
  53.             case "1" -> 0;
  54.             case "2" -> 1;
  55.             case "3" -> 2;
  56.             case "4" -> 3;
  57.             case "5" -> 4;
  58.             case "6" -> 5;
  59.             case "7" -> 6;
  60.             case "8" -> 7;
  61.             case "9" -> 8;
  62.             case "10" -> 9;
  63.             default -> {
  64.                 throw new InvalidPositionException();
  65.             }
  66.         };
  67.     }
  68.  
  69.     private static String indexConvert(int index) {
  70.         return switch (index) {
  71.             case 0 -> "A";
  72.             case 1 -> "B";
  73.             case 2 -> "C";
  74.             case 3 -> "D";
  75.             case 4 -> "E";
  76.             case 5 -> "F";
  77.             case 6 -> "G";
  78.             case 7 -> "H";
  79.             case 8 -> "I";
  80.             case 9 -> "J";
  81.             default -> null;
  82.         };
  83.     }
  84.  
  85.     private static String[] readPos(String position) {
  86.         String[] splitPosition = new String[2];
  87.         String stripPos = position.strip();
  88.         splitPosition[0] = String.valueOf(stripPos.charAt(0));
  89.         splitPosition[1] = stripPos.substring(1);
  90.         return splitPosition;
  91.     }
  92.  
  93.     public void placeShip(Ship target, String position, String direction) throws InvalidPlacementException, InvalidPositionException, InvalidShipTypeException{
  94.         try {
  95.             if (target == null) {
  96.                 throw new InvalidShipTypeException();
  97.             }
  98.  
  99.             String[] posArray = new String[2];
  100.             posArray = readPos(position);
  101.  
  102.             int y = convertNumberIndex(posArray[1]);
  103.             int x = convertLetterIndex(posArray[0]);
  104.  
  105.             switch (direction) {
  106.                 case "down":
  107.                     if (x > (10 - target.length())) {
  108.                         throw new InvalidPlacementException();
  109.                     }
  110.  
  111.                     for (int i = 0; i < target.length(); i++) {
  112.                         int j = i + x;
  113.                         if (cells[j][y].isOccupied()) {
  114.                             throw new InvalidPlacementException();
  115.                         };
  116.                         cells[j][y].placeSegment(target.getSegment(i + 1));
  117.                     }
  118.                     break;
  119.                 case "across":
  120.                     if (y > (10 - target.length())) {
  121.                         System.err.println(y);
  122.                         throw new InvalidPlacementException();
  123.                     }
  124.                     for (int i = 0; i < target.length(); i++) {
  125.                         int j = i + y;
  126.                         if (cells[x][j].isOccupied()) {
  127.                             throw new InvalidPlacementException();
  128.                         };
  129.                         cells[x][j].placeSegment(target.getSegment(i + 1));
  130.                     }
  131.                     break;
  132.                 default:
  133.                     throw new InvalidPlacementException();
  134.  
  135.             }
  136.         }
  137.         catch (StringIndexOutOfBoundsException e) {
  138.             throw new InvalidPositionException();
  139.         }
  140.     }
  141.     public void attack(String position) throws InvalidPositionException{
  142.         try {
  143.             String[] posArray = new String[2];
  144.             posArray = readPos(position);
  145.             int y = convertNumberIndex(posArray[1]);
  146.             int x = convertLetterIndex(posArray[0]);
  147.             cells[x][y].attack();
  148.         }
  149.         catch (StringIndexOutOfBoundsException e) {
  150.             throw new InvalidPositionException();
  151.         }
  152.     }
  153.  
  154.     public boolean hasBeenHit(String position) throws InvalidPositionException{
  155.         try {
  156.             String[] posArray = new String[2];
  157.             posArray = readPos(position);
  158.             int y = convertNumberIndex(posArray[1]);
  159.             int x = convertLetterIndex(posArray[0]);
  160.             return cells[x][y].hasBeenHit();
  161.         }
  162.         catch (StringIndexOutOfBoundsException e) {
  163.             throw new InvalidPositionException();
  164.         }
  165.     }
  166.  
  167.     public String displaySetup() {
  168.         StringBuilder sb = new StringBuilder("  1 2 3 4 5 6 7 8 9 10\n");
  169.         for (int i = 0; i < 10; i++) {
  170.             sb.append(indexConvert(i));
  171.             for (int j = 0; j < 10; j++) {
  172.                 sb.append(" "+this.cells[i][j].displaySetup());
  173.             }
  174.             sb.append("\n");
  175.         }
  176.         return sb.toString();
  177.     }
  178.  
  179.     @Override
  180.     public String toString() {
  181.         StringBuilder sb = new StringBuilder("  1 2 3 4 5 6 7 8 9 10\n");
  182.         for (int i = 0; i < 10; i++) {
  183.             sb.append(indexConvert(i));
  184.             for (int j = 0; j < 10; j++) {
  185.                 sb.append(" "+this.cells[i][j]);
  186.             }
  187.             sb.append("\n");
  188.         }
  189.         return sb.toString();
  190.     }
  191.  
  192.  
  193. }
Advertisement
Add Comment
Please, Sign In to add comment