Browse Source

Improved random ship positioning

fleetstorm
Lorenz Hohmann 3 years ago
parent
commit
af28ea8e34
  1. 15
      src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java

15
src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java

@ -137,12 +137,21 @@ public class Matchfield {
do {
Random random = new Random();
int randomX = random.nextInt(this.size - length);
int randomY = random.nextInt(this.size - length);
origin = new Coordinate(randomX, randomY);
randomDirection = random.nextInt(2);
int randomX = 0, randomY = 0;
if (randomDirection == 0) {
randomX = random.nextInt(this.size - length);
randomY = random.nextInt(this.size);
} else if (randomDirection == 1) {
randomX = random.nextInt(this.size);
randomY = random.nextInt(this.size - length);
}
origin = new Coordinate(randomX, randomY);
if (this.isFreePosition(origin, length, randomDirection)) {
success = this.setShip(new Coordinate(randomX, randomY), length, randomDirection);
}

Loading…
Cancel
Save