I need to create maze,
first, I tried randomly insert 'O' or 'B' into a 2-D array then randomly put the exit and start. but the problem is that 9 of the 10 maze I created doesn't have a path(start doesn't lead to exit, 'O' is the open path);
so what I'm trying to do is to create a path that will have at least 1 path from the start to the exit.
start point cannot be the border and exit point can only be the border.
B B B B B B
B B B B B B
B B B B S B
B B B B B B
B B X B B B
Basically I want to create a path to the exit and some path tat doesn't lead to the exit.
Example:
B B B B B B
B B O O O B
B B B B S B
B B O O O B (doesn't have to be this shape, this is just an example what I want to create)
B B X B B B
here is my code:
public static void randomly(int sx, int sy){ //sx and sy is the location of the startpoint
int x[] ={-1,0,0,1};
int y[] ={0,-1,1,0};
int count;
boolean check = false;
int afterX, afterY;
for(int i = 0 ; i < 4; i++){
afterX = sx + x[i];
afterY = sy + y[i];
if(maze[afterX][afterY] == 'X'){
return;
}
}
do{
count = 0;
int number = (int)(Math.random()* 4);
afterX = sx + x[number];
afterY = sy + y[number];
if(afterX > 0 && afterY > 0 && afterX < row-1 && afterY<column-1){
maze[afterX][afterY] = 'O';
}
}
}while((afterX == 0 || afterY == 0 || afterX == row-1 || afterY == column-1) && check);
randomly(afterX,afterY);
}
The problem is that I get this:
B B B B B B
B B O O B B
B O O O S B
B O O O O B
B B X B B B
I don't always get this, but most of the time
The problem is that the 'O' usually stick together, but what I want is the example I gave above