I am trying to implement the Aldous-Broder algorithm to create a maze generator. The problem is that when I am trying to randomly choose a cell that is next to the current cell, it keeps getting stuck in the corner and does not go over the cells where x=0 and/or y=0. This is the loop where it keeps getting stuck
while(remainingCells>0){
int[][] cells = new int[4][2];
cells[0][0] = x;
cells[0][1] = y-1;
cells[1][0] = x+1;
cells[1][1] = y;
cells[2][0] = x;
cells[2][1] = y+1;
cells[3][0] = x-1;
cells[3][1] = y;
while(true){
Double helper = Math.random();
if(helper<0.25){
nextCell = 0;
}
else if(helper>=0.25 && helper<0.5){
nextCell = 1;
}
else if(helper>=0.5 && helper<0.75){
nextCell = 2;
}
else{
nextCell = 3;
}
int currX = cells[nextCell][0];
int currY = cells[nextCell][1];
if(currX>0 && currY>0 && currX<3 && currY<3){
break;
}
}
if(nextCell == 0){
if(maze[x][y-1] == 1){
start[times][0] = x;
start[times][1] = y;
y--;
end[times][0] = x;
end[times][1] = y;
times++;
maze[x][y] = 0;
remainingCells--;
}
else{
y--;
}
}
else if(nextCell == 1){
if(maze[x+1][y] == 1){
start[times][0] = x;
start[times][1] = y;
x++;
end[times][0] = x;
end[times][1] = y;
times++;
maze[x][y] = 0;
remainingCells--;
}
else{
x++;
}
}
else if(nextCell == 2){
if(maze[x][y+1] == 1){
start[times][0] = x;
start[times][1] = y;
y++;
end[times][0] = x;
end[times][1] = y;
times++;
maze[x][y] = 0;
remainingCells--;
}
else{
y++;
}
}
else if(nextCell == 3){
if(maze[x-1][y] == 1){
start[times][0] = x;
start[times][1] = y;
x--;
end[times][0] = x;
end[times][1] = y;
times++;
maze[x][y] = 0;
remainingCells--;
}
else{
x--;
}
}
}
x and y are always 1 or 2 and I don't why that is. Perhaps it is because the code generates pseudo-random numbers? or have I just messed up in my implementation? Any help would be appreciated! Thanks!
Aucun commentaire:
Enregistrer un commentaire