There are a number of things you might want to deal with first: the most significant being that the local declaration of
grid
inside your
Main
method masks the class level version you declare outside it.
Since the version inside the method will not be available to methods outside
Main
such as
MazeSolve
, that means whatever you do in Main is pretty much irrelevant... :laugh:
Get rid of the one inside, or pass it as a parameter to the MazeSolve method.
When it comes to solving a maze, there are a number of ways to do it:
http://en.wikipedia.org/wiki/Maze_solving_algorithm[
^] - some are better than others.
Recursion is one way to code it - and not a bad one, because it lets you "back up" and try another route if this doesn't work.
So, the FindTheWayOut method needs to be passed a cell reference (the start point), and a direction it can't go in (the way you entered, to prevent you looping back out the way you came in) and to return a value which describes the route, or "no route this way" if it can't find it.
Inside the method, you try each direction in turn by the routine for each of the possible directions you can leave the cell. If one of the calls succeeds, pass the route on back up. If it doesn't try the next direction. If no direction works, pass back "no route this way" and exit.
Try it on a smaller grid to start with (2x2 with just a way in and a way out is simple) and see what happens in the debugger before you work up to more complex mazes with actual walls!
If you still aren't sure, try it on paper first - that should give you a better idea.