This really is a problem where the loop seems best, but you certainly can do much the same thing recursively.
The algorithm used here is essentially the same as the one I used before... step foreward, turn right, repeat. The function itself returns TRUE if it finds a path to the exit, and FALSE if it doesn't (most recursive functions where you need to find a path will either have returns like this, return the number of paths found, or some score for deciding which path has the highest/lowest score).
A change I'm going to use is that instead of having a CanStepForeward function, the recursive function will handle illegal positions. This is another thing you see a lot in recursive functions, (for example, a binary tree traversal often doesn't test for whether the children are NULL before trying to operate on them; it just operates on them and is clever enough to handle NULL).
I'm going to firther C-ize the code. The player is a struct of 3 ints. One for his x position, one for his y position, and one for his direction...
Code:
typedef struct
{
int x, y, dir;
} player;
int TraverseMaze(player ThePlayer)
{
if (GameOver(&ThePlayer)) return 1;
if (IllegalPosition(&ThePlayer)) return 0;
// At this point in the function, we know the player is at a legal
// position, that is, he isn't standing in a wall. Now we just do
// the turn right, test for path in front, turn left if illegal part...
TurnRight(&ThePlayer);
// player StepForeward(player ThePlayer);
// Basically, if the path 1 step ahead of use leads to the exit, then
// the path from our current position leads to the exit too.
if (TraverseMaze(StepForeward(ThePlayer))) return 1;
//else...
TurnLeft(&ThePlayer);
if (TraverseMaze(StepForeward(ThePlayer))) return 1;
// else...
TurnLeft(&ThePlayer);
if (TraverseMaze(StepForeward(ThePlayer))) return 1;
// now, if we turn left one more time, then we're going to be
// facing the direction we came from, but we can't do that,
// so we've exausted all the possibilities, so this path is false.
return 0;
}
Umm.. obviously I suggest cleaning up this code a lot if you want to use it (I think half my lines are return statements, isk). This code doesn't have anything in it to actually print the path, but adding that isn't too tough.
If I might suggest, a similar (better) problem would be to write a function which returns the number of paths from the starting location to the ending location.
Or, if you're up for a chellenge, you might want to try writing a function that can solve mazes like this...
Code:
###########
# B #
# ####### #
# # # #
# # ### # #
# # #E# # #
# # # #
# ### ### #
# #
###########
B = Begin Here
E = End Here