# Solving a maze in java

• 04-11-2011, 11:54 PM
kuro
Solving a maze in java
Hi, I'm new to this forum and was wondering if anyone could help me out with a code i'm working on requiring 2D arrays and recursion (the later which I have difficulties understanding).

In a nut shell I have to make a program that generates either a random maze or reads a maze from a file. Then I have to have the program find a path from point a to point b and output the path using "+"s

The maze would look something like this:

BBBBBB
BOOOBB
BSBOBB
BOOOOB
BBBBXB

B = barrier
O= open path
X = exit
S = start

The main area of concern is my Solve method which would solve a given array.

Here is what I have so far.
Code:

public static int Solve (int row, int col)
{
int check = 1;
//check squares for O

if ((col - 1) >= 0)  //check for left border
{
if (array [row] [col - 1] == "O")
{
if (Solve (row, col + 1) == check)
{
array [row] [col] = "+";

return check;
}

}
}

if ((col + 1) < MAXCOL) //check for right border
{
if (array [row] [col + 1] == "O")
{
if (Solve (row, col + 1) == check)
{
array [row] [col] = "+";

return check;
}
}
}

if ((row - 1) < MAXROW) //check for down border
{
if (array [row - 1] [col] == "O")
{
if (Solve (row - 1, col) == check)
{
array [row] [col] = "+";

return check;
}
}
}

if ((row + 1) >= 0) //check for up border
{

if (array [row + 1] [col] == "O")
{
if (Solve (row + 1, col) == check)
{
array [row] [col] = "+";

return check;
}
}
}
c.println ("No Solution.");
return 0;
}
}

I'm getting out of bounds errors that I'm not sure how to fix :( Could anyone help me out? Also, would I have to search the array for "S" for it to start? My teacher says I would not but I don't understand why o.0

Any help would be greatly appreciated :)
• 04-12-2011, 12:27 AM
pbrockway2
Quote:

if (array [row] [col - 1] == "O")

Don't check for string equality with ==. For Strings (or any other objects) use the equals() method.

If you get runtime errors copy and post the entire stack trace. In general you can use System.out.println() just before the error occurs to figure out which index variable is causing the problem and what its value is at the time.

-----

I've just noticed that a large chunck of that code is commented out. When you post code it is a good idea not to include "junk" that will only obscure the problem. Also it would be good to post runnable code - ie something that others can actually use to see the problem for themselves. No-one wants to deal file i/o or anything else unrelated to the problem so it would be a good idea to hard code an actual maze that generates the out of bounds exceptions.

(What I am getting at is the value of a SSCCE)
• 04-12-2011, 12:27 AM
Junky
Do not compare Strings (or any other object) with ==, use the equals method instead.

Is that out of bounds negative or larger than the array length. This should help you in debugging your code which you need to do not us. Scatter a bunch of print statements to print out the values of row and col, especially when you + or - 1 from their values. This is the most likely spot when you exception occurs.
• 04-12-2011, 12:36 AM
kuro
Thanks for the replies so far. I'll fix up the == signs and the post to get rid of the unneeded stuff.

I'm not that good at programing. The error shows up like this:

java.lang.ArrayIndexOutOfBoundsException: 7

But I'm not really sure what that means :S
• 04-12-2011, 12:41 AM
Junky
Your array (if you are using the example above) is 6x6. Valid indicies are 0-5. Lets assume that row is 5 (the max value it can have) and somewhere in your code you call Solve (row + 1,...). This passes 6 to the next recursion which is already out of bounds. then somewhere in your code you try to access array[row+1][col]. This now makes it 7 and that is definately out of bounds. As I said you need to debug your code and make sure you do not call Solve with values out of bounds.