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
 
No comments:
Post a Comment