0

I am trying to use stacks to find my way through a maze.

Before I worry about stacks, I am trying to get it so that it comes to a dead-end first.

However, when I run my code I am getting java.lang.ArrayIndexOutofBoundsException: -1. Which does not make sense since I am constantly updating the rows and columns.

Here is part of my code that I am stuck on:

    Maze myMaze = new Maze (rows, columns);
    MazeDisplay myDisplay = new MazeDisplay (myMaze);

    myMaze.buildMaze(10);
    myMaze.setSolveAnimationDelay(75);

    boolean [][] mazeArray = new boolean [rows][columns];

    for (int row=0; row<mazeArray.length; row++)
        for (int col=0; col<mazeArray[row].length; col++)
            mazeArray[row][col] = false;
            mazeArray [myMaze.getCurrentRow()][myMaze.getCurrentCol()] = true;


    for (int i = 0; i < 9999999 ; i++) //Temporary  for now 
    {
        int arrayRows = myMaze.getCurrentRow();
        int arrayCols = myMaze.getCurrentCol();

        if (myMaze.isOpen(Maze.Direction.RIGHT) && mazeArray [arrayRows][1 + arrayCols] == false)
        {
            myMaze.move(Maze.Direction.RIGHT);
            //mazeStack.push(Maze.Direction.RIGHT);
            mazeArray[arrayRows][arrayCols] = true;
        }
        else if (myMaze.isOpen(Maze.Direction.UP) && mazeArray [1 + arrayRows][arrayCols] == false)
        {
            myMaze.move(Maze.Direction.UP);
            //mazeStack.push(Maze.Direction.UP);
            mazeArray[arrayRows][arrayCols] = true;
        }
        else if (myMaze.isOpen(Maze.Direction.LEFT) && mazeArray [arrayRows][arrayCols - 1] == false) // <---Getting an error here.
        {
            myMaze.move(Maze.Direction.LEFT);
            //mazeStack.push(Maze.Direction.LEFT);
            mazeArray[arrayRows][arrayCols] = true;
        }
        else if (myMaze.isOpen(Maze.Direction.DOWN) && mazeArray [arrayRows - 1][arrayCols] == false) // <---Getting an error here.
        {
            myMaze.move(Maze.Direction.DOWN);
            //mazeStack.push(Maze.Direction.DOWN);
            mazeArray[arrayRows][arrayCols] = true;
        }

Is there a way to get around this? What I want is to access the ArrayList's position down or left of the current position.

Here are some of the methods of the Maze class:

//-------- isOpen - returns true if there is no wall in the direction that is passed in
public boolean isOpen(Direction direction)
{
    boolean result = false;
    if (direction == Direction.UP && mazeArray[currentArrayRow-1][currentArrayCol]==0)
        result = true;
    else if (direction == Direction.DOWN && mazeArray[currentArrayRow+1][currentArrayCol]==0)
        result = true;
    else if (direction == Direction.LEFT && mazeArray[currentArrayRow][currentArrayCol-1]==0)
        result = true;
    else if (direction == Direction.RIGHT && mazeArray[currentArrayRow][currentArrayCol+1]==0)
        result = true;

    return result;
}

//-------- getCurrentRow - returns the current (real) row
public int getCurrentRow()
{
    return currentArrayRow/2;
}

//-------- getCurrentCol - returns the current (real) col
public int getCurrentCol()
{
    return currentArrayCol/2;
}

// -------- move - receives a Direction and moves there if OK.  Calls the other
//                 arrayMove to do this
public boolean move(Direction direction)
{
    boolean success = false;

    if (direction == Direction.UP)
        success = arrayMove(currentArrayRow-2, currentArrayCol);
    else if (direction == Direction.DOWN)
        success = arrayMove(currentArrayRow+2, currentArrayCol);
    else if (direction == Direction.LEFT)
        success = arrayMove(currentArrayRow, currentArrayCol-2);
    else if (direction == Direction.RIGHT)
        success = arrayMove(currentArrayRow, currentArrayCol+2);

    return success;
}

//This is Maze's enumerated data type: moves can be UP, DOWN, LEFT, RIGHT
public enum Direction
{
    UP, DOWN, LEFT, RIGHT
}

//-------- getMazeArray - returns the mazeArray
public int[][] getMazeArray()
{
    return mazeArray;
}
Paincakes
  • 137
  • 1
  • 2
  • 11
  • You say `ArrayList` but I don't see any `List` of any type. Do you mean array? Unrelated, but `arrayRows` and `arrayCols` are mis-named; they should just be called `currentRow` and `currentCol` to avoid making things harder to reason about. – Dave Newton Mar 29 '16 at 16:44
  • @DaveNewton Sorry, I meant Array 2-D. Even with the renamed `currentRow` and `currentCol` I still get the exception. – Paincakes Mar 29 '16 at 16:49
  • That's why I said it's unrelated--naming things doesn't change their values, it changes their names. They're just named wrong. – Dave Newton Mar 29 '16 at 16:53
  • In any case: without knowing how `move` works, and how it affects `currentRow` and `currentCol`, it's impossible to help. You're trying to access array elements that don't exist, which means you're not accounting for maze edges, and are trying to look beyond them. You will have the same problem with `1 + currentRow` and `1 + currentCol` once your program gets that far along. – Dave Newton Mar 29 '16 at 16:56
  • @DaveNewton I've updated the methods from the Maze class that I used. – Paincakes Mar 29 '16 at 17:00
  • Does the stack trace tell you which line is throwing the exception. Also if you are using eclipse you can set an exception breakpoint. It breaks when an unhandled exception is thrown. Here is a link to set that up... http://stackoverflow.com/q/3066199/2822371 – abhaybhatia Mar 29 '16 at 17:14
  • I have no idea why you're dividing by 2 in anything. Or moving by two spaces. Especially since you check for +1 from current position. In any case, by point remains: you're at 0, you're subtracting one, you get -1, which isn't where the array is. – Dave Newton Mar 29 '16 at 17:18
  • @abhaybhatia OP indicated where the exceptions occur. The data shows what the problem is. – Dave Newton Mar 29 '16 at 17:19

1 Answers1

1

Before checking the left cell, you should check that you are not on the left edge. Otherwise mazeArray [arrayRows][arrayCols - 1] will throw an exception, since arrayCols - 1 = -1.

If I understand your code correctly, your algorithm isn't perfect. It gets stuck at dead ends. I think the shortest path algorithm is the easiest one to implement.

Barett
  • 5,826
  • 6
  • 51
  • 55
Denis Kokorin
  • 887
  • 8
  • 17