diff --git a/src/Ship.java b/src/Ship.java index 22a205d..5dfac8c 100644 --- a/src/Ship.java +++ b/src/Ship.java @@ -65,8 +65,17 @@ public class Ship { this.position = null; } - public boolean setPosition(Point pos, List shipsList, int boardSize) { - // ueberpruefe boundaries + public boolean setPosition(Point pos, boolean horizontal, List shipsList, int boardSize) { + if (!this.checkValidPlacement(pos, horizontal, shipsList, boardSize)) return false; + + // kein ueberlappen also setze das Schiff + this.position = pos; + this.horizontal = horizontal; + return true; + } + + public boolean checkValidPlacement(Point pos, boolean horizontal, List shipsList, int boardSize) { + // ueberpruefe boundaries if (pos.getX() < 0 || pos.getY() < 0 || pos.getX() >= boardSize || pos.getY() >= boardSize) { return false; } @@ -75,7 +84,7 @@ public class Ship { int endX = pos.getX(); int endY = pos.getY(); - if (this.horizontal) { // rechts links + if (horizontal) { // rechts links endX = pos.getX() + this.size - 1; if (endX >= boardSize) { return false; @@ -90,8 +99,8 @@ 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 = this.horizontal ? pos.getX() + i : pos.getX(); //falls horizontal dann pos.x + i ansonsten pos.x - int y = this.horizontal ? pos.getY() : pos.getY() + 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)); } @@ -114,9 +123,6 @@ public class Ship { } } } - - // kein ueberlappen also setze das Schiff - this.position = pos; return true; }