add checkValidShipPlacement and set horizontal in setPosition
This commit is contained in:
parent
f565708461
commit
b5f1151a4f
|
@ -65,8 +65,17 @@ public class Ship {
|
||||||
this.position = null;
|
this.position = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean setPosition(Point pos, List<Ship> shipsList, int boardSize) {
|
public boolean setPosition(Point pos, boolean horizontal, List<Ship> shipsList, int boardSize) {
|
||||||
// ueberpruefe boundaries
|
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<Ship> shipsList, int boardSize) {
|
||||||
|
// ueberpruefe boundaries
|
||||||
if (pos.getX() < 0 || pos.getY() < 0 || pos.getX() >= boardSize || pos.getY() >= boardSize) {
|
if (pos.getX() < 0 || pos.getY() < 0 || pos.getX() >= boardSize || pos.getY() >= boardSize) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -75,7 +84,7 @@ public class Ship {
|
||||||
int endX = pos.getX();
|
int endX = pos.getX();
|
||||||
int endY = pos.getY();
|
int endY = pos.getY();
|
||||||
|
|
||||||
if (this.horizontal) { // rechts links
|
if (horizontal) { // rechts links
|
||||||
endX = pos.getX() + this.size - 1;
|
endX = pos.getX() + this.size - 1;
|
||||||
if (endX >= boardSize) {
|
if (endX >= boardSize) {
|
||||||
return false;
|
return false;
|
||||||
|
@ -90,8 +99,8 @@ public class Ship {
|
||||||
// Liste an Punkten die das Schiff einnehmen wuerde
|
// Liste an Punkten die das Schiff einnehmen wuerde
|
||||||
List<Point> shipPoints = new ArrayList<>();
|
List<Point> shipPoints = new ArrayList<>();
|
||||||
for (int i = 0; i < this.size; i++) {
|
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 x = 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 y = horizontal ? pos.getY() : pos.getY() + i;
|
||||||
shipPoints.add(new Point(x, y));
|
shipPoints.add(new Point(x, y));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -114,9 +123,6 @@ public class Ship {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// kein ueberlappen also setze das Schiff
|
|
||||||
this.position = pos;
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue