diff --git a/src/Ship.java b/src/Ship.java index 5dfac8c..7dd7459 100644 --- a/src/Ship.java +++ b/src/Ship.java @@ -97,12 +97,7 @@ public class Ship { } // Liste an Punkten die das Schiff einnehmen wuerde - List shipPoints = new ArrayList<>(); - for (int i = 0; i < this.size; i++) { - int x = horizontal ? pos.getX() + i : pos.getX(); //falls horizontal dann pos.x + i ansonsten pos.x - int y = horizontal ? pos.getY() : pos.getY() + i; - shipPoints.add(new Point(x, y)); - } + List shipPoints = this.getVirtualOccupiedPoints(pos, horizontal); // ueberlappen mit anderen Schiffen pruefen for (Ship otherShip : shipsList) { @@ -126,6 +121,22 @@ public class Ship { return true; } + /** + * Returns the Points on the ship if it were to be placed at positino `pos` in orientation defined by `horizontal` + */ + public List getVirtualOccupiedPoints(Point pos, boolean horizontal) { + List points = new ArrayList<>(); + if (pos == null) { + return points; + } + for (int i = 0; i < this.size; i++) { + int x = horizontal ? pos.getX() + i : pos.getX(); + int y = horizontal ? pos.getY() : pos.getY() + i; + points.add(new Point(x, y)); + } + return points; + } + public List getOccupiedPoints() { List points = new ArrayList<>(); if (this.position == null) {