@Override public List<Square> apply( Integer nrOfGoldItems2, Integer nrOfWalls2, Integer maxLatitude2, Integer maxLongitude2) { ArrayList<Square> available = new ArrayList<Square>(); int oneForTheBank = 1; int goldLeft = nrOfGoldItems2; int nrOfGoldSquares = 0; while (goldLeft > 0) { int gold = (int) Math.min((Math.random() * 9) + 1, goldLeft); goldLeft = goldLeft - gold; nrOfGoldSquares++; available.add(new GoldSquare(gold)); } for (int i = 0; i < (maxLatitude2 * maxLongitude2) - nrOfGoldSquares - nrOfWalls2 - oneForTheBank; i++) { available.add(Square.empty()); } for (int i = 0; i < nrOfWalls2; i++) { available.add(new WallSquare()); } Collections.shuffle(available); return available; }
@Override public Square[][] createField() { int tries = 0; int limit = 20; while (tries < limit) { Square[][] field = null; field = createUnvalidatedField(); if (isValid(field)) { return field; } tries++; System.out.println("Discarded\n" + Square.getField(field)); } throw new RuntimeException("Couldn't create field"); }
private Square[][] createUnvalidatedField() { int maxLatitude2 = maxLatitude; int maxLongitude2 = maxLongitude; int startLatitude2 = startLatitude; int startLongitude2 = startLongitude; List<Square> available = getAvailable(); Square[][] field = new Square[maxLatitude2 + 2][maxLongitude2 + 2]; for (int lat = 0; lat < maxLatitude2 + 2; lat++) { for (int lon = 0; lon < maxLongitude2 + 2; lon++) { if (lat == 0 || lon == 0 || lat == maxLatitude2 + 1 || lon == maxLongitude2 + 1) { field[lat][lon] = Square.wall(); } else { if (lat == startLatitude2 && lon == startLongitude2) { field[lat][lon] = new BankSquare(); } else { field[lat][lon] = available.remove(0); } } } } return field; }