I've been trying to write the same method for a couple of hours now now and I don't seem to be getting any closer to figuring it out. What it has to do is use an enum class, Direction, and use that to find its way out of a maze.
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.
Da mihi sis bubulae frustum assae, solana tuberosa in modo Gallico fricta ac quassum lactatum coagulatum crassum.
stack overflow means you have got into and endless loop, and it is going round and round in circles till it runs out of memory or stack.
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?
when you get an exception it lists the method and line it was at when the error occured, but also the method and line that called that method, so on until it reaches the main method, or the start of the thread.
The exception stacktrace is what looks like garbage dumped to your screen when the program crashes. It actually contains useful information (usually only the top several lines are needed).
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.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
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.
Bats fly at night, 'cause they aren't we. And if we tried, we'd hit a tree -- Ogden Nash (or should've been).
Articles by Winston can be found here
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.
Having it print things like "HAWAII" or "HELLO" isn't really what I mean.
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.