I currently in processing making a recursive division maze generating algorithm and I think I'm almost there. I currently have a 2-dimensional array, 20 cells wide, 15 cells tall. The array contains cell objects, which holds rows, columns and a boolean variable to indicate whether or not it's a wall.
When ever I uncomment
generateMaze(height, maxWidth-randomColumn, 1, randomColumn+1);
I get a stackoverflow. Without it, it will only traverse left and up, I need to make it traverse right and down as well. I've been staring at this for long time to figure out why but just cannot see to do it.
EDIT: I can generate something now, but the maze is often blocked, that is, there are paths that are blocked. So If I set up an random starting location, it might be surrounded by walls.
private void generateMaze(int minColumn, int maxColumn, int minRow, int maxRow){
int width = maxColumn - minColumn;
int height = maxRow-minRow;
if (width > 2 && height > 2){
if ("VERTICAL".equals(getOrientation(height, width))){
splitVertical(minColumn, maxColumn, minRow, maxRow);
}
else{
splitHorizontal(minColumn, maxColumn, minRow, maxRow);
}
}
}
private void splitVertical (int minColumn, int maxColumn, int minRow, int maxRow){
int randomColumn = getRandomNumber(minColumn, maxColumn);
for (int i= minRow; i < maxRow; i++){
maze[i][randomColumn] = new Cell (i+1, randomColumn+1, true);
}
maze[(getRandomNumber(minRow, maxRow))][randomColumn].setWall(false);
generateMaze(minColumn, randomColumn, minRow, maxRow);
generateMaze(randomColumn, maxColumn, minRow, maxRow);
}
private void splitHorizontal (int minColumn, int maxColumn, int minRow, int maxRow){
int randomRow = getRandomNumber(minRow, maxRow);
for (int i = minColumn; i < maxColumn; i++){
maze[randomRow][i] = new Cell (randomRow+1, i+1, true);
}
generateMaze(minColumn, maxColumn, minRow, randomRow);
generateMaze(minColumn, maxColumn, randomRow, maxRow);
}
private String getOrientation(int height, int width) {
Random rand = new Random();
if (height > width){
return "HORIZONTAL";
} else if (width > height){
return "VERTICAL";
} else {
int randomNumber = rand.nextInt(2);
if (randomNumber == 0){
return "HORIZONTAL";
} else{
return "VERTICAL";
}
}
}
private int getRandomNumber(int x, int y){
int minimum = x;
int maximum = y;
int randomNumber = 0;
Random rand = new Random();
randomNumber = rand.nextInt((maximum-minimum)+1)+ minimum;
return randomNumber;
}
}