diff --git a/src/main/java/core/Ship.java b/src/main/java/core/Ship.java index f16d2d1..1924ee4 100644 --- a/src/main/java/core/Ship.java +++ b/src/main/java/core/Ship.java @@ -6,38 +6,25 @@ import java.util.function.Predicate; import ui.CoordToStringAttributeConverter; -/** - * Represents a ship object for the {@link Grid} class, Describes the different directions it can - * face, Describes the length of the ship Has the starting coordinate of the ship, Helps identify - * the name of different ships, - */ +/** Makes a ship object for the {@link Grid} class */ @Entity public class Ship { - public Long getId() { - return id; - } + /** List of coords for ship's position */ + @Transient private List coordList; + /** Identifier for Ship */ @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; - public enum Direction { - HORIZONTAL, - VERTICAL - } - - public Coord getStartcoordinate() { - return startcoordinate; - } - /** The starting coordinate of the ship */ // @OneToOne // @JoinColumn(name = "startcoordinate_id") @Convert(converter = CoordToStringAttributeConverter.class) private Coord startcoordinate; - /** Length of the Ship object */ + /** the size of the ships */ @Column private int size; /** the directions its facing like horizontal and vertical */ @@ -47,11 +34,34 @@ public Coord getStartcoordinate() { @Column(nullable = false) private String name; + /** Enum to represent ship placement direction on the grid */ + public enum Direction { + HORIZONTAL, + VERTICAL + } + + public Long getId() { + return id; + } + + public Coord getStartcoordinate() { + return startcoordinate; + } + + /** Length of the Ship object */ + /** The coordinates of the whole ship */ - // @Convert(converter = CoordToStringAttributeConverter.class) - @Transient private List coordList; - public Ship(Coord coordinate, int size, Direction direction, String name) { + /** + * Constructs a new ship with the following: + * + * @param coordinate + * @param size + * @param direction + * @param name + */ + public Ship( + final Coord coordinate, final int size, final Direction direction, final String name) { this.startcoordinate = coordinate; this.size = size; this.direction = direction; @@ -59,34 +69,53 @@ public Ship(Coord coordinate, int size, Direction direction, String name) { this.coordList = genCoordList(); } + /** Default constructor required by JPA */ public Ship() {} public int getSize() { return size; } + /** + * @return row index of ship starting coord + */ public int getStartRow() { return startcoordinate.row; } + /** + * @return starting column of ship + */ public int getStartCol() { return startcoordinate.col; } + /** + * @return the ships direction + */ public Direction getDirection() { return direction; } + /** + * @return the ships name + */ public String getName() { return name; } - public boolean containsCoord(Coord coord) { - return any(c -> coord.isEqual(c)); + /** + * checks if a ship occupies a given coord + * + * @param coord the coordinate to check + * @return true only if the ship contains the coord + */ + public boolean containsCoord(final Coord coord) { + return any(coord::isEqual); } private List genCoordList() { - List coordList = new ArrayList(); + final List coordList = new ArrayList<>(); if (direction == Direction.HORIZONTAL) { for (int i = 0; i < this.size; i++) { coordList.add(startcoordinate.shiftBy(0, i)); @@ -103,7 +132,13 @@ public List getCoordList() { return coordList; } - public boolean isOverlapping(Ship other) { + /** + * Checks if this ship overlaps with another + * + * @param other ship to check against + * @return true if overlap, false if otherwise + */ + public boolean isOverlapping(final Ship other) { return other.any(this::containsCoord); } @@ -112,16 +147,17 @@ public boolean isOverlapping(Ship other) { * * @return true if ship is on grid, false if not */ - public boolean isWithinBounds(Bounding bounding) { - for (Coord coord : this.coordList) { + public boolean isWithinBounds(final Bounding bounding) { + for (final Coord coord : this.coordList) { if (coord.row < 1 || coord.row > bounding.numRows()) return false; if (coord.col < 1 || coord.col > bounding.numCols()) return false; } return true; } - public boolean any(Predicate predicate) { - for (Coord coord : getCoordList()) { + /** Checks if coordinates in the ships coordinate match given predicate */ + public boolean any(final Predicate predicate) { + for (final Coord coord : getCoordList()) { if (predicate.test(coord)) { return true; } @@ -129,8 +165,14 @@ public boolean any(Predicate predicate) { return false; } - public boolean all(Predicate predicate) { - for (Coord coord : getCoordList()) { + /** + * Checks if all coords in ship coord list match predicate + * + * @param predicate the condition to test the coord against + * @return true if they satisfy, false if otherwise + */ + public boolean all(final Predicate predicate) { + for (final Coord coord : getCoordList()) { if (!predicate.test(coord)) { return false; }