# Recursive Maze Help

• November 26th, 2012, 09:59 PM
Deejay1992
Recursive Maze Help
Hi All,

I am having a problem with this maze project that I was assigned in my programming and data structures class. I have everything done, except for a few minor steps of the Generator method here it is:

// The Gen n Method: Recursively Generates The Maze

private void genFile(int startX, int startY, int endX, int endY) {
Random gen = new Random();

// *** BASES CASES ***

// Check if we can divide the space

boolean wallPossible = false;

// Check all vertical wall placements

for (int i = startX + 1; i < endX; i++) {
if ( ((endY+1 < n) || maze[i][endY+1] != '_') &&
((startY-1 >= 0) || maze[i][startY-1] != '_') ) {

// Do this when there is no wall opening mark on either side

wallPossible = true;
break;
}
}

if (!wallPossible) {

// Check all horizontal wall placements

for (int i=startY+1; i<endY; i++) {
if ( ((endX+1 < n) || maze[endX+1][i] != '_') &&
((startX-1 >= 0) || maze[startX-1][i] != '_') ) {

// Do this when there is no wall opening mark on either side

wallPossible = true;
break;

}
}
}

if (!wallPossible) return;

// *** RECURSIVE CASE ***
// Once here, it means we can divide the space

// Loop that keeps asking for direction and location for a wall until valid

int dir = 0;
int k = 0;

do {

// Pick a random direction

dir = gen.nextInt(2);

// Pick a random wall location

if (dir == 0) {

// Generate a random number between the boundaries
// that will represent the (vertical) wall location

k = gen.nextInt (endy - starty - 1) + starty + 1;

} else {

// Generate a random number between the boundaries
// that will represent the (horizontal) wall location

k = gen.nextInt(endY - startY - 1) + startY + 1;

}

// Check if valid

//for (int i = startX + 1; i < endX; i++) {
if (dir==0) {
if ( ((endY+1 < n) || maze[k][endY+1] != '_') &&
((startY-1 >= 0) || maze[k][startY-1] != '_') ) {

// Do this when there is no wall opening mark on either side

wallPossible = true;
break;
}
}

else {

// Check all horizontal wall placements

if ( ((endX+1 < n) || maze[endX+1][k] != '_') &&
((startX-1 >= 0) || maze[startX-1][k] != '_') ) {

// Do this when there is no wall opening mark on either side

wallPossible = true;
break;
}
}

} while (!wallPossible);

// Draw the wall

if (dir==0)
{
for (int y = startY; y <= endY; y++)
maze [k][y] = '*';

}

else {
for (int x = startX; x <= endX; x++)
maze[k][x] = '*';

}

// Pick a random passage location on the wall
// Make the passage

int r = gen.nextInt(endY-startY+1) + startY;

if (dir == 0){
maze [k][r] = '_';
}
else{
maze [r][k] = '_';
}

// Recursively split the two spaces

// Find the coordinates of the first space

// Find the coordinates of the second space

// Call genFile for both space
// genFile( , , , );
// genFile( , , , );

The last steps that are underlined and in bold print are the last steps that I need to finish this method please help.
• November 26th, 2012, 10:25 PM
curmudgeon
Re: Recursive Maze Help
Question triple posted! This one closed.