maze travler

i don't why i did get infinity loop
maze-code
moh123fmAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

evilrixSenior Software Engineer (Avast)Commented:
Rather than just a dump of a text file can you please attach your latest version of the source code file(s)? Once you've done that I'll take a look for you.
0
sarabandeCommented:
the code from the "dump"  

#include <iostream>
#include <fstream>
using namespace std;

#define rows 6
#define cols 6

int main()
{
    char maze[rows][cols];
    int i,j, startr,startc,exitr,exitc,cposition,cpositiony,cpositionx;
    char direction;
    ifstream in;
    ofstream out;

    in.open("maze.txt");
    out.open("maze1.txt");

    if(in.fail()) { cout << "error could not open inputfile:\n";}
    else
    {
        for ( i=0 ; i < rows; i++)
        {
            for ( j=0 ; j < cols;j++)
            {
                in >> maze [i][j];
                out << maze [i][j];

                cout << maze [i][j] << " ";
            }
            cout << endl;
        }// for

        for ( i=0 ; i < rows;i++)
            for ( j=0 ; j < cols;j++)
            {
                if (( maze[i][j] == 'S') || (maze[i][j] == 's')) { startr=i;startc= j;}
            }

            for ( i=0 ; i < rows;i++)
                for ( j=0 ; j < cols;j++)
                {
                    if (( maze[i][j] == 'E') ||(maze[i][j] == 'e')) { exitr=i;exitc= j;}
                }

                cposition= 's';
                cout << "\n";
    }
    cout<< "the staring location is :" << startr << " , " << startc << endl <<endl;
    cout<< "the ending location is :" << exitr << " , " << exitc << endl << endl;

    if (startr == 0) direction = 'd';
    else if (startr == rows-1) direction = 'u';
    else if (startc == 0) direction = 'r';
    else if (startc == cols-1) direction = 'l';
    cout << "Starting direction is: " << direction << endl;

    cpositionx=startr;
    cpositiony=startc;

    while(maze[cpositionx][cpositiony]!='E'){


        switch (direction) {
                case 'd':
                    for ( i=0 ; i < rows; i++)
                    {
                        for ( j=0 ; j < cols;j++)
                        {


                            if ((maze[cpositionx + 1][cpositiony - 1] =='#') && (maze[cpositionx + 1][cpositiony ]=='.'))
                            {
                                cpositionx = cpositionx+1;
                                maze[cpositionx][cpositiony]= '$';

                            }
                            else {

                                maze[cpositionx][cpositiony]= '$';

                                cpositionx = cpositionx+1;
                                cpositiony = cpositiony-1;
                                direction = 'l';

                            }//else
                        }}
                    break;

                case 'r':
                    for ( i=0 ; i < rows; i++)
                    {
                        for ( j=0 ; j < cols;j++)
                        {

                            if ((maze[cpositionx + 1][cpositiony +1] =='#')&&(maze[cpositionx -1][cpositiony ] =='#')&&(maze[cpositionx ][cpositiony+1]=='.'))

                            {
                                cpositionx = cpositionx;
                                cpositiony = cpositiony+1;

                                maze[cpositionx][cpositiony]= '$';
                                cout << "direction is: " << direction << endl;
                            }
                            else {

                                maze[cpositionx][cpositiony]= '$';
                                maze[cpositionx][cpositiony-1]= '$';
                                maze[cpositionx+1][cpositiony]= '$';

                                cpositionx = cpositionx-1;
                                direction = 'u';

                            }
                        }}
                    break;


                case 'l':
                    for ( i=0 ; i < rows; i++)
                    {
                        for ( j=0 ; j < cols;j++)
                        {

                            if ((maze[cpositionx - 1][cpositiony ] =='#')&&(maze[cpositionx +1][cpositiony ]=='#')&& (maze[cpositionx ][cpositiony+1 ]=='#')&&(maze[cpositionx ][cpositiony-1 ]=='.'))
                            {
                                cpositiony = cpositiony-1;
                                maze[cpositionx][cpositiony]= '$';

                                cout << "direction is: " << maze[cpositionx][cpositiony]<< endl;
                            }//if

                            else  {

                                maze[cpositionx][cpositiony]= '$';
                                maze[cpositionx][cpositiony-1]= '$';

                                cpositiony = cpositiony+1;
                                direction = 'r';
                                cout << " direction is: " << direction << endl;
                            }//else
                        }}
                    break;
                case 'u':
                    if ((maze[cpositionx + 1][cpositiony ] =='$')&&(maze[cpositionx -1][cpositiony-1 ] =='$')&& (maze[cpositionx-1][cpositiony+1 ] =='$')&& (maze[cpositionx -1][cpositiony ] =='.')&& (maze[cpositionx][cpositiony+1 ] =='$')&&(maze[cpositionx][cpositiony-1 ] =='$')){

                        cpositionx = cpositionx-1;
                        maze[cpositionx][cpositiony]= '$';


                        cout << "direction is: " << direction << endl;
                    }
                    else if (maze[cpositionx][cpositiony]== '$'&& maze[cpositionx-1][cpositiony]== '$')
                    {

                        maze[cpositionx][cpositiony]= '$';
                        maze[cpositionx-1][cpositiony]= '$';


                        cpositiony = cpositiony+1;

                        direction = 'r';
                        cout << "direction is: " << direction << endl;
                    }//else


                    else if (maze[cpositionx + 1][cpositiony ] =='$') {

                        maze[cpositionx][cpositiony]= '$';
                        maze[cpositionx][cpositiony-1]= '$';
                        maze[cpositionx+1][cpositiony+1]= '$';

                        cpositionx = cpositionx+1;

                        direction = 'd';
                        cout << "direction is: " << direction << endl;

                    }//else

                    break;

                case 'swh':
                    if((maze[cpositionx +1][cpositiony ] =='#')&& (maze[cpositionx ][cpositiony+1 ] =='#'))
                    {
                        cpositiony=cpositiony-1;
                    }
                    break;

        }//swh

        cout << maze [i][j] << " ";
    }//w
    in.close();
    out.close();
    return 0;
}

Open in new window


compiles.

the infinite loop is because while(maze[cpositionx][cpositiony]!='E') is always true. to prevent from this, you have to guarantee for each loop cycle that either cpositionx and/or cpositiony got a change, or, that the cell contents changes from 'E' to somewhat else if the position wouldn't change.

case 'swh':
i would look to this case block first, because 'swh' is not a valid code. the compiler probably uses 's' only and ignores the 'wh'.

if you output x and y position, the direction and the current cell contents of the maze at (x,y) at end of the while loop you pretty fast will see which cell would not be changed. then try to debug or find out which branch is not correct.

Sara

p.s. you were lucky that the "dump" contained the full source code at end
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sarabandeCommented:
@evilrix: now the 'purple one' was back not the 'dark one' :-)
0
evilrixSenior Software Engineer (Avast)Commented:
Dark in a purple clothing, Sara ;)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.