|
|
@ -1,5 +1,7 @@ |
|
|
|
package de.tims.fleetstorm.matchfield; |
|
|
|
|
|
|
|
import java.util.Random; |
|
|
|
|
|
|
|
public class Matchfield { |
|
|
|
|
|
|
|
private Coordinate[][] matchfield; |
|
|
@ -127,4 +129,27 @@ public class Matchfield { |
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
public boolean setShipOnRandomPosition(int length) { |
|
|
|
boolean success = false; |
|
|
|
Coordinate origin = null; |
|
|
|
int randomDirection; |
|
|
|
|
|
|
|
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); |
|
|
|
|
|
|
|
if (this.isFreePosition(origin, length, randomDirection)) { |
|
|
|
success = this.setShip(new Coordinate(randomX, randomY), length, randomDirection); |
|
|
|
} |
|
|
|
|
|
|
|
} while (!success); |
|
|
|
|
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
} |