Hello. Just need some advice on what I'm doing wrong with my code down below. I'm suppose to right up a program that reads in maze code that looks like this " 2 3swensw 2 1" for example. the first two digits = row & columns of the array while a "nested for loop" reads in the " nswe" values for each cell of the array. (n being north, s being south, e being east, and w being south.) I have completed that part of the program just fine but my problem lies with in writing a function that finds the way through the maze while outputting the path and the exit point. I've got the program to do the very first point past the starting point, which the starting point is the last two digits of that line up above.

I've been working on hours on this trying the while loop and do while loop but each time when i run the program it seems to be stuck in an endless loop.

So i started over and tried to get the program to run without outputting the pathway of each maze and the exit point. So the code down below is without the outFile << lines to make it more simple and less text.

To me this code looks like it should work fine, but for some reason the readRow and the readColumn aren't adding up to the point where they reach mazeSizeRow and mazeSizeColumn.

Note: the readRow--; and the readcolumn--; are needed because the start point of maze 1 is (1,1) which actually would be (0,0) to the array of the maze.

void TraverseMaze(char mazeArray[][Columns], int startRow, int startColumn, int mazeSizeRow, int mazeSizeColumn)
{
    int readRow,
        readColumn;
        
    readRow = startRow;
    readColumn = startColumn;
    
    readRow--;
    readColumn--;
    
    do
    {
        if(mazeArray[readRow][readColumn]=='N')
        {
            readRow--;
            
            outFile << readRow << "," << readColumn << "   ";
        }
        else if(mazeArray[readRow][readColumn]=='S')
        {
            readRow++;
            
            outFile << readRow << "," << readColumn << "   ";
        }
        else if(mazeArray[readRow][readColumn]=='E')
        {
            readColumn++;
            
            outFile << readRow << "," << readColumn << "   ";
        }
        else if(mazeArray[readRow][readColumn]=='W')
        {
            readColumn--;
            
            outFile << readRow << "," << readColumn << "   ";
        }
    }while(readRow <= mazeSizeRow || readColumn <= mazeSizeColumn);
}

Recommended Answers

All 4 Replies

Because if mazeArray[readRow][readColumn] is north you go up one row.
Then if the new mazeArray[readRow][readColumn] is south, you go down one row.
now the new mazeArray[readRow][readColumn] is north (we know this because you were just there) you go up one row.
Then....

You need to keep track of which directions you've already gone so you don't get stuck like this.

Because if mazeArray[readRow][readColumn] is north you go up one row.
Then if the new mazeArray[readRow][readColumn] is south, you go down one row.
now the new mazeArray[readRow][readColumn] is north (we know this because you were just there) you go up one row.
Then....

You need to keep track of which directions you've already gone so you don't get stuck like this.

Well thats what I was thinking when I was righting this out. Well at least so I thought.

I thought since I had the readRow--; under the if ....=='N' that it would keep track of readRow now being the new integer so that when it ran the new readRow and readColumn through the set of if else statements that it would now be for example (1,2) instead of (2,2).

Here, since someone gave me a warning for "unorganized post" Ill made it easier to understand for such people.

Basically the program reads in several mazes in a 2D array and assigns each cell of the array a N S W or E ( North South West or East).

After that the program is suppose to (while getting the starting point from the input) "find" its way around the maze while printing out the pathway and exit point.

The have successfully written the first part of the program, my problem lies within the second part.

I've been successful with the second part to the point where I can get it to print out the first point after the starting point. But when I put it into a loop (do while loop) it seems to go on as an endless loop and I can't reason out why it's doing that.

Just thought a second pair of eyes could help me out and make it where I can wrap my mind around this part and why it wasn't working the first place. It keeps going in an endless loop.

So here is the code for the function to find its way through the maze.

Heres an example of the a maze:

....1 2 3 4
1 N S E W
2 S S W E
3 S W E N

and i know in the array it really looks like this when you have to access each cell of the array:

....0 1 2 3
0 N S E W
1 S S W E
2 S W E N

and the starting point for this example say be it (0,1)

void TraverseMaze(char mazeArray[][Columns], int startRow, int startColumn, int mazeSizeRow, int mazeSizeColumn)
{
    int readRow,
        readColumn;
        
    readRow = startRow;
    readColumn = startColumn;
    
    readRow--;
    readColumn--;
    
    do
    {
        if(mazeArray[readRow][readColumn]=='N')
        {
            readRow--;
            
            outFile << readRow << "," << readColumn << "   ";
        }
        else if(mazeArray[readRow][readColumn]=='S')
        {
            readRow++;
            
            outFile << readRow << "," << readColumn << "   ";
        }
        else if(mazeArray[readRow][readColumn]=='E')
        {
            readColumn++;
            
            outFile << readRow << "," << readColumn << "   ";
        }
        else if(mazeArray[readRow][readColumn]=='W')
        {
            readColumn--;
            
            outFile << readRow << "," << readColumn << "   ";
        }
    }while(readRow <= mazeSizeRow || readColumn <= mazeSizeColumn);
}

Here, since someone gave me a warning for "unorganized post" Ill made it easier to understand for such people.

If you bothered to read the warning it has nothing to do with the content of your post. Read it again if you are confused.

As for you endless loop problem my comments above still stand. There is no "remembering" in the program once the loop starts again. What it knew in the first loop is forgotten in the second loop.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.