I am making a dungeon map generator and am currently trying to make it place empty rooms. It first checks if the space is empty and then places a room. The problem is that most of the times it compiles and I get a map/2d array printed, but sometimes it compiles but doesn't print the map. I don't get an error.
public class Mapa {
public static void main(String[] args) {
// Create a 2D array map filled with floors
char map1[][] = new char[100][100];
for (int i = 0; i < map1.length; i++) {
for (int j = 0; j < map1[i].length; j++) {
map1[i][j] = tiles.wall();
}
}
char mapFinal[][] = roomPlacement(map1);
for (int i = 0; i < mapFinal.length; i++) {
for (int j = 0; j < mapFinal[i].length; j++) {
System.out.print(mapFinal[i][j] + " ");
}
System.out.println();
}
}
public static char[][] roomPlacement(char map1[][]) {
// this method places rooms into the 2D array map
char mapReplacement[][] = map1;
int roomNumber = (int) (10 + (Math.random() * 10));
int roomLength = (int) (10 + (Math.random() * 10));
int roomWidth = (int) (10 + (Math.random() * 10));
int startPositionX = (int) (Math.random() * (mapReplacement.length - roomLength));
int startPositionY = (int) (Math.random() * (mapReplacement.length - roomWidth));
int counter = 0;
while (roomNumber > counter ) {
if (position(startPositionX, startPositionY, roomLength, roomWidth, mapReplacement)) {
if (roomSpace(mapReplacement, startPositionX, startPositionY, roomLength, roomWidth) == true) {
for (int i = startPositionX; i < (startPositionX + roomLength); i++) {
for (int j = startPositionY; j < (startPositionY + roomWidth); j++) {
mapReplacement[i][j] = tiles.floor();
}
}
}
if (roomSpace(mapReplacement, startPositionX, startPositionY, roomLength, roomWidth) == false) {
startPositionX = (int) (Math.random() * (mapReplacement.length - roomLength));
startPositionY = (int) (Math.random() * (mapReplacement.length - roomWidth));
}
counter++;
}
roomLength = (int) (10 + (Math.random() * 10));
roomWidth = (int) (10 + (Math.random() * 10));
}
return mapReplacement;
}
public static boolean roomSpace(char map1[][], int startPositionX, int startPositionY, int roomLength,
int roomWidth) {
// this method checks if there is enough free space
boolean isFreeSpace = false;
char mapCheck[][] = map1;
int counter = 0;
for (int i = startPositionX; i < (startPositionX + roomLength); i++) {
for (int j = startPositionY; j < (startPositionY + roomWidth); j++) {
if (mapCheck[i][j] == tiles.wall()) {
counter++;
}
}
}
if (counter == (roomLength * roomWidth)) {
isFreeSpace = true;
}
if (isFreeSpace == true) {
System.out.println("true");
} else if (isFreeSpace == false) {
System.out.println("false");
}
return isFreeSpace;
}
public static boolean position(int startPositionX, int startPositionY, int roomLength, int roomWidth,
char mapReplacement[][]) {
boolean position = false;
// checks if the room is out of bounds
if (startPositionX > 0 && startPositionX < mapReplacement.length && startPositionX + roomLength > 0
&& startPositionX + roomLength < mapReplacement.length && startPositionY > 0
&& startPositionY < mapReplacement.length && startPositionY + roomWidth > 0
&& startPositionY + roomWidth < mapReplacement.length) {
position = true;
}
return position;
}
}
Sorry for the long code but wanted to let you see the whole code to better understand it