Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 73 additions & 31 deletions src/main/java/core/Ship.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Coord> 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 */
Expand All @@ -47,46 +34,88 @@ 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<Coord> 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;
this.name = 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<Coord> genCoordList() {
List<Coord> coordList = new ArrayList<Coord>();
final List<Coord> coordList = new ArrayList<>();
if (direction == Direction.HORIZONTAL) {
for (int i = 0; i < this.size; i++) {
coordList.add(startcoordinate.shiftBy(0, i));
Expand All @@ -103,7 +132,13 @@ public List<Coord> 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);
}

Expand All @@ -112,25 +147,32 @@ 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<Coord> predicate) {
for (Coord coord : getCoordList()) {
/** Checks if coordinates in the ships coordinate match given predicate */
public boolean any(final Predicate<Coord> predicate) {
for (final Coord coord : getCoordList()) {
if (predicate.test(coord)) {
return true;
}
}
return false;
}

public boolean all(Predicate<Coord> 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<Coord> predicate) {
for (final Coord coord : getCoordList()) {
if (!predicate.test(coord)) {
return false;
}
Expand Down
Loading