good5dog5
good5dog5

Reputation: 149

solve maze by right hand principle

hello there
i am solving a text book exercise to solve a maze by right hand principle

i use switch case to deal it

switch ( face )
{
    case face_EAST:
    {
        if( map[x][y + 1] == '.' && map[x + 1][y] == '#' )
        {
            y = y + 1;
            gotoxy(x, y);
            face = face_EAST;
        }
        else if ( map[x - 1][y] == '.' && map[x][y + 1] == '#' )
        {
            x = x - 1;
            gotoxy( x, y );
            face = face_NORTH;
        }
        else if ( map[x + 1][y] == '.' && map[x + 1][y - 1] == '#' )
        {
            x = x + 1;
            gotoxy( x, y );
            face = face_SOUTH;
        }
        else
            face = face_SOUTH;

        break;
    }
    case face_SOUTH :
    {
        if( map[x + 1][y] == '.' && map[x][y - 1] == '#' )
        {
            x = x + 1;
            gotoxy( x, y );
            face = face_SOUTH;
        }
        else if( map[x][y - 1] == '.' && map[x - 1][y - 1] == '#' )
        {
            y = y - 1;
            gotoxy( x, y );
            face = face_WEST;
        }
        else if( map[x][y + 1] == '.' && map[x + 1][y] == '#' )
        {
            y = y + 1;
            gotoxy( x, y );
            face = face_EAST;
        }
        else
            face = face_WEST;
        break;
    }
    case face_WEST:
    {
        if( map[x][y - 1] == '.' && map[x - 1][y] == '#' )
        {
            y = y - 1;
            gotoxy( x, y );
            face = face_WEST;
        }
        else if( map[x - 1][y] == '.' && map[x - 1][y + 1] == '#' )
        {
            x = x - 1;
            gotoxy( x, y );
            face = face_NORTH;
        }
        else if( map[x + 1][y] == '.' && map[x][y - 1] == '#' )
        {
            x = x + 1;
            gotoxy( x, y );
            face = face_SOUTH;
        }
        else if( map[x][y + 1] == '.' && map[x + 1][y] == '#' )
        {
            y = y + 1;
            gotoxy( x, y );
            face = face_EAST;
        }
        else if( map[x - 1][y] == '.' && map[x - 1][y + 1] == '#' )
        {
            x = x - 1;
            gotoxy( x, y );
            face = face_NORTH;
        }
        else
            face = face_NORTH;

        break;
    }
    case face_NORTH:
    {
        if( map[x - 1][y] == '.' && map[x][y + 1] == '#' )
        {
            x = x - 1;
            gotoxy( x, y );
            face = face_NORTH;
        }
        else if( map[x][y - 1] == '.' && map[x - 1][y] == '#' )
        {
            y = y - 1;
            gotoxy( x, y );
            face = face_WEST;
        }
        else if( map[x][y + 1] == '.' && map[x + 1][y + 1] == '#' )
        {
            y = y + 1;
            gotoxy( x, y );
            face = face_EAST;
        }
        else
            face = face_EAST;

        break;
    }
}

i want to know if there any more easy way to make right hand principle work?
i have though to change the coordinate system, but can't implement it

Upvotes: 1

Views: 1254

Answers (1)

Crowman
Crowman

Reputation: 25926

As far as "any more easy way", this problem is a prime candidate for recursion. Folks often find recursion a little hard to grasp, at first, but it ends up being considerably easy to follow than what you have. There's a bunch of links in the "related" section to this question which may be helpful, and I wrote a program to do it a while ago, the look() function right at the bottom is the one that does the solving.

Upvotes: 1

Related Questions