0

I am about to complete an assignment and I have an error that I can seem to debug. I think this is because I have been looking at it for too long and I need some fresh eyes. my compiler gives me an ArrayIndexOutOfBoundsExpection for lines 111, and 63. I am not sure if this question is to be relocated in code review or not, so if it should please feel free to mark it as such. I am working on the classic prompt of maze traversal using recursion. I think my logic is solid I just can't run it until I get rid of the error. I understand what the error is, but my issue is resolving it. Anything helps, thanks!

public class mazeTraversal 
{
private static int xStart = 2;
private static int yStart = 0;
private final static int TRIED = 0;
private final static char PATH = 'X';
private static boolean mazeStart = false; 
private static int printCount = 0;
private static boolean solved = false;

private static char[][] maze = {
        { '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' },
        { '#', '.', '.', '.', '#', '.', '.', '.', '.', '.', '.', '#' },
        { 'S', '.', '#', '.', '#', '.', '#', '#', '#', '#', '.', '#' },
        { '#', '#', '#', '.', '#', '.', '.', '.', '.', '#', '.', '#' },
        { '#', '.', '.', '.', '.', '#', '#', '#', '.', '#', '.', 'F' },
        { '#', '#', '#', '#', '.', '#', '.', '#', '.', '#', '.', '#' },
        { '#', '.', '.', '#', '.', '#', '.', '#', '.', '#', '.', '#' },
        { '#', '#', '.', '#', '.', '#', '.', '#', '.', '#', '.', '#' },
        { '#', '.', '.', '.', '.', '.', '.', '.', '.', '#', '.', '#' },
        { '#', '#', '#', '#', '#', '#', '.', '#', '#', '#', '.', '#' },
        { '#', '.', '.', '.', '.', '.', '.', '#', '.', '.', '.', '#' },
        { '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' }};

public static void printMaze()
{
    int rows = 12;
    int columns = 12;
    //maze = new char[rows][columns];

    for(int i = 0; i<rows; i++)
    {
        for(int j = 0; j<columns; j++)
        {
            System.out.print(maze[i][j]);
            printCount ++;
        }
        System.out.println();
    }
}

public static boolean getSolved()
{
    return solved;
}


public static boolean Traversal (char[][] maze, int row, int col)
{   
    //if-else to see if this is the first time the maze has been printed. Ie, is this the beginning?
    if(printCount == 1)
    {
        mazeStart = true;
        row = xStart;
        col = yStart;
    }
    else
    {
        mazeStart = false;
    }

    boolean done = false;
    boolean result = valid(row,col); //ERROR THROWN HERE


    if( result == false)
    {
        maze[row][col] = TRIED; //Tried = 0
    }

    if(result == true)
    {
        //flag that maze is solved
        if(row == maze.length - 1 && col == maze[0].length - 1)
        {
            done = true; //maze is solved
            solved = true;
        }
        else
        {               
            done = Traversal(maze, row+1, col); //down
            if (!done)
            {
                done = Traversal(maze, row, col+1); //right
            }
            if (!done)
            {
                done = Traversal(maze, row-1, col); //up
            }
            if (!done)
            {
                done = Traversal(maze, row, col-1); //left
            }
        }

        if (done)
        {
            maze[row][col] = PATH;
        }

        //return done;
    }
    return done;

}

//method to check if the space to move to next is vaild and not a wall
private static boolean valid(int row, int col)
{
    boolean result = false;
    if(maze[row][col] == '.') //ERROR THROWN HERE
    {
        result = true;
    }
    return result;
}
}

Main:

public class MazeTraversal_Hard 
{
private static char[][] maze = {
        { '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' },
        { '#', '.', '.', '.', '#', '.', '.', '.', '.', '.', '.', '#' },
        { 'S', '.', '#', '.', '#', '.', '#', '#', '#', '#', '.', '#' },
        { '#', '#', '#', '.', '#', '.', '.', '.', '.', '#', '.', '#' },
        { '#', '.', '.', '.', '.', '#', '#', '#', '.', '#', '.', 'F' },
        { '#', '#', '#', '#', '.', '#', '.', '#', '.', '#', '.', '#' },
        { '#', '.', '.', '#', '.', '#', '.', '#', '.', '#', '.', '#' },
        { '#', '#', '.', '#', '.', '#', '.', '#', '.', '#', '.', '#' },
        { '#', '.', '.', '.', '.', '.', '.', '.', '.', '#', '.', '#' },
        { '#', '#', '#', '#', '#', '#', '.', '#', '#', '#', '.', '#' },
        { '#', '.', '.', '.', '.', '.', '.', '#', '.', '.', '.', '#' },
        { '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' }};

private static int row = 12;
private static int col = 12;
private static boolean solved = true;

public static void main(String[] args)
{   

    while(mazeTraversal.getSolved() != true)
    {
        mazeTraversal.printMaze();
        mazeTraversal.Traversal(maze, row, col);
    }


}
user3712626
  • 115
  • 1
  • 9

1 Answers1

2

In an array with dimensions [12][12] as you introduce as static array, the max index is 11.

With

private static int row = 12;
private static int col = 12;

you access the 12,12 index, but there is none. This two lines should be

private static int row = 11;
private static int col = 11;
Marcinek
  • 2,144
  • 1
  • 19
  • 25