Reputation: 870
The code:
public void generateMaze(boolean Array[][], int val)
{
Stack<Integer> StackX = new Stack<Integer>();
Stack<Integer> StackY = new Stack<Integer>();
int x = val / 2; // Start in the middle
int y = val / 2; // Start in the middle
StackX.push(x);
StackY.push(y);
while(!StackX.isEmpty())
{
Array[x][y] = true; // is Visited
x = StackX.peek();
y = StackY.peek();
if(Array[x][y+1] == false)
{
StackX.push(x);
StackY.push(y+1);
y = y + 1;
}
else if(Array[x][y-1] == false)
{
StackX.push(x);
StackY.push(y-1);
y = y - 1;
}
else if(Array[x+1][y] == false)
{
StackX.push(x+1);
StackY.push(y);
x = x+1;
}
else if(Array[x-1][y] == false)
{
StackX.push(x-1);
StackY.push(y);
x = x-1;
}
else
{
StackX.pop();
StackY.pop();
}
}
}
Whenever I print my maze, it seems that every spot in the maze returns as a true value, and therefore is marked with a star. Is there anything that I am doing incorrectly, any help would be appreciated.
Upvotes: 3
Views: 112
Reputation: 21773
The problem is that you use the boolean array to indicate two distinct things:
1) that there's a wall there (initially set to true in the array)
2) that you've visited the tile (set to true during the maze solver)
Instead, you'll need to keep two arrays, and check both - don't walk onto a wall or onto a checked tile, but when you walk onto a new tile only set it as checked, not as a wall. Then you'll be able to print -wall -reached by solver -unreached by solver as different characters.
(I am assuming that generateMaze() is misnamed and is actually passed a generated maze made somewhere else. If it's passed an empty maze, well, the problem is then that you need to MAKE a maze first ;) )
Upvotes: 4