Results 1 to 5 of 5
  1. #1
    kuro is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Post 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.
    Java 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 :)
    Last edited by kuro; 04-11-2011 at 11:33 PM.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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)

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,759
    Rep Power
    7

    Default

    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.

  4. #4
    kuro is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Default

    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

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,759
    Rep Power
    7

    Default

    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.

Similar Threads

  1. Solving a Maze
    By bdario1 in forum New To Java
    Replies: 4
    Last Post: 04-14-2010, 12:02 AM
  2. Pls help me in solving this java question
    By dunnoGUy in forum New To Java
    Replies: 1
    Last Post: 02-28-2010, 07:10 AM
  3. Help me with create a 2D Maze in Java
    By hnam31190 in forum New To Java
    Replies: 4
    Last Post: 11-02-2008, 01:37 AM
  4. Solving this equations problem in Java
    By matt_well in forum New To Java
    Replies: 17
    Last Post: 08-30-2008, 09:05 PM
  5. Java Maze Problem
    By mousey182 in forum New To Java
    Replies: 2
    Last Post: 03-28-2008, 06:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •