From 60e44f2e1aad3f14c460e5244cd06de660e81d81 Mon Sep 17 00:00:00 2001 From: Luca Conte Date: Sun, 15 Dec 2024 15:10:20 +0100 Subject: [PATCH] add getVirtualOccupiedPoints --- src/Ship.java | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) 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) {