I wrote a program to generate a maze and to solve it, however it has some bug in the solving part.
It forms a square maze like 5*5 or 16*16.
The maze starts at (0,0 in a 2D array, and ends at (size()-1, size()-1).
I use ‘1’ to indicate the path to end.
You can see from the picture below there are some unwanted ‘1’ although the program can find the exit.
Excuse me everyone, I really can’t debug this. Can anyone help me or guide me?
Much appreciated!
The screencapure is here. Im not allowed to post image directly
https://photos-1.dropbox.com/t/0/AADjdwSgmLdVKCZrI1C-gDvwZ9ORj0rGbv3UJ7AYqXWeuA/10/7014161/png/2048×1536/2/1355295600/0/2/bug.png/5sQR3E_jcow4lWIy9cFf2FYbmwl0C_sd2cfCyMPe0MU
My code is in here
https://www.dropbox.com/s/vldkcv4fy6bp1ff/Source.cpp
PROBLEM SOLOVED
Thanks everyone
my original code for solving the maze was
`else if (randomNum==1) {
if (y+1<myMaze.size() && !myMaze[x][y+1].left && !myMaze[x][y+1].visited)
{
y++;
myMaze[x][y].truePath=true;
myMaze[x][y].visited=true;
s1.push(myMaze[x][y]);
randomNum=rand()%4;
}
else
{
rightBusted=true;
randomNum=rand()%4;
}`
Then I just add these codes inside the if-statement to reset the bool variables to false, then the problem solved
downBusted=false;
rightBusted=false;
topBusted=false;
leftBusted=false;
You can debug this, man. Here’s how. You already have the tools you need to do it; all they need is a little tweaking.
Your best tool for debugging this is your displayMaze() routine. You just need to add two optional parameters to it to turn it into a powerful debugging tool:
Now if a caller omits curX & curY, the compiler fills in the defaults of -1. Now later on in that function, print a different character to indicate the “current maze position”. Here’s how I did it, which “seems to work” but I’m not guaranteeing it because I didn’t bother to actually understand your logic:
Now you have a formidable debugging tool built into your program. When main() calls it without the two extra parameters, no asterisk is printed. But when you call it from solveMaze(), you can specify the “current location” so it will flag that location with ‘*’. In solveMaze(), add a couple variables for keeping track of the “current location”…
…then, at the top of your loop, just call your formidable debugging tool so you get the current status of your overall solution as it progresses, step-by-step:
Now, wherever you change what you consider to be the “current location”, just update curX and curY accordingly, and your debug tool will keep you updated so you can see the solution as it unfolds graphically (well, pseudo-graphically). You might even add debug cout messages at key logic decision points, so you can correlate those decision points with the solution you see unfolding, so you can see if/when there’s a problem. And if you find a problem, you can scroll back up in your output to see where things went wrong.
Good luck!