Advertisement
Guest User

Untitled

a guest
Feb 10th, 2025
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.16 KB | None | 0 0
  1. package conroller;
  2.  
  3. import model.ConstantsModel;
  4. import model.ship.ShipModel;
  5. import model.navigation.CoordinateModel;
  6.  
  7. import java.util.Scanner;
  8.  
  9. public class CoordinateController {
  10.     private static ShipModel ship;
  11.  
  12.     public static ShipModel placeShip(ShipModel ship) {
  13.         CoordinateController.ship = ship;
  14.  
  15.         int[] fullShipCoords = askShipCoords();
  16.         CoordinateModel[] shipCoordinates = new CoordinateModel[fullShipCoords.length];
  17.         for (int i = 0; i < shipCoordinates.length - 1; i++) {
  18.             shipCoordinates[i] = new CoordinateModel(i, i + 1);
  19.         }
  20.         ship.putShip(shipCoordinates);
  21.  
  22.         return ship;
  23.     }
  24.  
  25.     private static int[] askShipCoords() {
  26.         askCoords(ship.getName(), ship.getSize());
  27.         String[] userInputCoords = new Scanner(System.in).nextLine().toUpperCase().split(" ");
  28.  
  29.         while (!userInputCoords[0].matches(ConstantsModel.COORDS_REGEX)
  30.                 && !userInputCoords[1].matches(ConstantsModel.COORDS_REGEX)) {
  31.             System.out.println(ConstantsModel.TELL_ERROR);
  32.             askCoords(ship.getName(), ship.getSize());
  33.             userInputCoords = new Scanner(System.in).nextLine().toUpperCase().split(" ");
  34.         }
  35.  
  36.         return parseCoordsToArrayIndexes(userInputCoords);
  37.     }
  38.  
  39.     // Coordinates are input by user as a string like 'D3 D6'. Tokenization allows easier validation and preparation
  40.     // for parsing them to array indexes
  41.     private static void askCoords(String shipName, int shipSize) {
  42.         System.out.println(ConstantsModel.ASK_BOARD_COORDS + shipName);
  43.         System.out.println(ConstantsModel.TELL_SIZE + shipSize);
  44.     }
  45.  
  46.     /**
  47.      * To represent the alphanumeric coords in an array-index format, they require parsing
  48.      *
  49.      * @param userInputCoords
  50.      * @return array-index parsed alphanumeric coords
  51.      */
  52.     private static int[] parseCoordsToArrayIndexes(String[] userInputCoords) {
  53.         int[] parsedCoords = new int[4];
  54.         for (int i = 0; i < userInputCoords.length; i++) {
  55.             // These two lines were made by ChatGPT
  56.             parsedCoords[i * 2] = userInputCoords[i].toUpperCase().charAt(0) - 65;
  57.             parsedCoords[i * 2 + 1] = Integer.parseInt(userInputCoords[i].substring(1)) - 1;
  58.         }
  59.         return extrapolateBodyCoords(parsedCoords);
  60.     }
  61.  
  62.     /**
  63.      * User input consists only of the coordinates of a ships front and tail, the ship's body part's locations
  64.      * have to be extrapolated from these.
  65.      *
  66.      * @param parsedCoords index 0 and 1 represent the ships front, index 2 and 3 the tail.
  67.      * @return an array containing the parsedCoords and the extrapolated coords.
  68.      */
  69.     private static int[] extrapolateBodyCoords(int[] parsedCoords) {
  70.         int[] allShipPartsCoords = new int[ship.getSize() * 2];
  71.  
  72.         int xCoord = parsedCoords[0];
  73.         int yCoord = parsedCoords[1];
  74.  
  75.         /* In case the even indexes of parsedCoords are equal, this value of the equal indexes remains unchanged
  76.          * for all coordinates and only the odd indexes are extrapolated.
  77.          * In this case, the 3rd index determines the upper limit for the extrapolation.
  78.          */
  79.         if (parsedCoords[0] == parsedCoords[2]) {
  80.             for (int i = 0; i < parsedCoords[3]; i++) {
  81.                 if (i % 2 == 0) {
  82.                     allShipPartsCoords[i] = xCoord;
  83.                 } else {
  84.                     allShipPartsCoords[i] = yCoord;
  85.                     yCoord++;
  86.                 }
  87.             }
  88.         }
  89.  
  90.         /* In case the odd indexes of parsedCoords are equal, this value of the odd indexes remains unchanged
  91.          * for all coordinates and only the even indexes are extrapolated.
  92.          * In this case, the 2nd index determines the upper limit for the extrapolation.
  93.          */
  94.         if (parsedCoords[1] == parsedCoords[3]) {
  95.             for (int i = 0; i < parsedCoords[2]; i++) {
  96.                 if (i % 2 == 0) {
  97.                     allShipPartsCoords[i] = xCoord;
  98.                     xCoord++;
  99.                 }
  100.                 allShipPartsCoords[i] = yCoord;
  101.             }
  102.         }
  103.         return allShipPartsCoords;
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement