The 'S' is the beginning and the 'E' is the end.
The part I am having trouble with is that it goes into a stackoverflow and I don't know what is causing it. Also I need help when it comes to creating the arraylist containing the directions that it took to exit the maze.
Thanks in advance.
So what would be really helpful is if you provide us with the exception stacktrace, as that will show the lines it is looping around.
So your teacher wrote the enum and you need t use it? did I understand correctly?
My guess is that the problem is in your "getPathToExit" method, since that is a recursive function. You aren't hitting the base case, so it keeps calling itself over and over and over, and eventually you run out of memory.
I would suggest putting in a few "System.out.println()" statements in that method. print out what row and col are each time you enter, and what you return each time you come out.
Wendy Gibbons wrote:stack overflow means you have got into and endless loop...
@Aj: Just to be clear, it usually means that you've got into an endless recursive loop - ie, a call to a method that either
(a) is itself, or
(b) ends up calling itself in a ring
so I'd concentrate on line 107 if I were you.
Aj Prieto wrote:What it has to do is use an enum class, Direction, and use that to find its way out of a maze.
This has nothing to do with your problem, but strangely enough, I've implemented an enum (also called Direction) that does almost exactly what yours does (great minds obviously think alike ).
You might save yourself a lot of those switch statements by simply supplying the x and y "changes" for a single unit move in that direction as constructor values, viz:I'm also not sure that you need HERE, since it's (I assume) a condition, rather than a Direction - ie, some turn, or set of turns, that you've done has ended you up in the same Direction as you started from.
Aj Prieto wrote:I changed my code. I'm still getting stackoverflow.
Ooof. Bad move. Never change code unless:
(a) You KNOW that it's the right thing to do.
(b) It's to test a theory - and if it doesn't pan out: BACK OUT THE CHANGE.
Above all: don't add enhancements to code that doesn't yet work.
@Winston The HERE is to signify that it has reached the exit 'E'.
So I was right; it's a condition, NOT a Direction. It might work, but it's probably not the best solution.
I would have it print things like:
"calling getPathToExit with row = <whatever> and col = <whatever>"
" row or column out of bounds"
" map position equal to a space"
" map position equal to E"
" map position equal to 'S' or a space"
" entering for loop with <whatever> iterations"
" loop iteration <whatever>"
etc. each time above when I put "<whatever>, i would print out the value of the appropriate variable/calculation.
While I was writing that, I noticed something. What is the purpose of the or condition on line 38? if map[row][col] is a space, it will be handled on line 30. So it is un-needed at best, and wrong at worst.