RGB Jacob
RGB Jacob

Reputation: 13

What's causing my stackoverflowerror in my maze solver?

I have to solve a maze using recursion and everything was going fine until I ran the program and ran into a stackoverflowerror. I've read a couple other questions on the site and they all say it's because of infinite recursion however none of their issues seem to be the exact same as mine.

my netID_maze.java file

import java.util.Random;

public class netID_Maze
{

int exitRow,
    entranceRow;
char[][] map = null;


// Method Name  : Maze (Constructor)
// Parameters  : None
// Partners  : None
// Description  : No Parameter Constructor for the maze
public netID_Maze()
{
    // omitted code generating a random maze

    setEntranceRow(rnger.nextInt(row - 2) + 1);
    setExitRow(rnger.nextInt(row - 2) + 1);

    map[getEntranceRow()][0] = '.';
    map[getExitRow()][column - 1] = '.'; 

} // end netID_Maze (without parameters)


// Method Name  : Maze (Constructor)
// Parameters  : exitTemp (int), entranceTemp(int), mapTemp (char[][])
// Partners  : None
// Description  : Parameter Constructor for the maze
public netID_Maze(char[][] mapTemp, int exitTemp, int entranceTemp)
{

    map = mapTemp;
    exitRow = exitTemp;
    entranceRow = entranceTemp;

} // end netID_Maze (with parameters)


// Method Name  : getCell
// Parameters  : row (int), column (int), character in cell (character)
// Partners  : None
// Returns : The character in the cell that's being called (character)
// Description  : Returns the character of the cell that's being called
public char getCell(int r, int c)
{
    return map[r][c];

} // end getCell()

// Method Name  : setCell
// Parameters  : row (int), column (int), character in cell (character)
// Partners  : None
// Returns : None
// Description  : Changes the character of the cell that's being called
public void setCell(int r, int c, char val)
{
    this.map[r][c] = val;

} // end setCell()

public int getEntranceRow ()
{
    return entranceRow;
}

public int getExitRow()
{
    return exitRow;
}

public void setEntranceRow(int entranceTemp)
{
    entranceRow = entranceTemp;
}

public void setExitRow(int exitTemp)
{
    exitRow = exitTemp;
}

public int getRows()
{
    return map.length;
}

public int getColumns()
{
    return map[1].length;
}

public boolean isExit(int r, int c)
{
    boolean isExit = false;

    if (getExitRow() == r && map[1].length - 1 == c)
    {
        isExit = true;
    }

    return isExit;
}

public boolean isEntrance(int r, int c)
{
    boolean isEntrance = false;

    if (getEntranceRow() == r && 0 == c)
    {
        isEntrance = true;
    }

    return isEntrance;
}

public boolean isOpen(int r, int c)
{

    boolean isOpen = true;

    if (r < 0 || c < 0 || r >= getRows() || c >= getColumns())
    {
        isOpen = false;
    }
    else if (map[r][c] == '.')
    {
        isOpen = false;
    }

    return isOpen;
}

}

and my netID_MazeSolver.java file

public class netID_MazeSolver {
int steps = 0;
netID_Maze maze = new netID_Maze();

public netID_MazeSolver(netID_Maze mazeTemp)
{
    setSteps(0);
    maze = mazeTemp;
}

public boolean solveMaze(int r, int c)
{
    //Finding whether Current Cell is outside the maze
    if (r < 0 || c < 0 || r >= maze.getRows() || c >= maze.getColumns()) 
    {
        return false;
    }

    //Finding whether the current cell is the exit
    if (maze.isExit(r,c) == true)
    {
        return true;
    }

    //Finding whether current cell is NOT open
    if (maze.isOpen(r,c) == false)
    {
        return false;
    }

    //Setting current cell as part of the solution path


    //Finding out whether solve maze(cell below current) == true
    if (solveMaze(r - 1,c) == true)
    {
        return true;
    }

    //Finding out whether solve maze(cell to the right of current) == true
    if (solveMaze(r,c + 1) == true)
    {
        return true;
    }

    //Finding out whether solve maze(cell to the left of current) == true
    if (solveMaze(r,c - 1) == true)
    {
        return true;
    }

    //Finding out whether solve maze(cell above current) == true
    if (solveMaze(r + 1,c) == true)
    {
        return true;
    }

    //setting current cell to NOT part of the solution path


    return false;

}

public void setSteps(int stepsTemp)
{
    steps = stepsTemp;
}

public int getSteps()
{
    return steps;
}

}

The actual error just keeps repeating: at netID_MazeSolver.solveMaze(netID_MazeSolver.java:53) at netID_MazeSolver.solveMaze(netID_MazeSolver.java:71)

Upvotes: 1

Views: 163

Answers (1)

user4668606
user4668606

Reputation:

The basic mistake you made, was that you don't set any flags for visited cells. Due to this, your algorithm can visit the same cell again and again. If your generated maze contains any cycles, you're pretty likely to end up in an endlessloop causing a stackoverflow. And btw, you don't need, to write if(maze.isOpen(r , c) == true). if(maze.isOpen(r , c)) gives the same result with less code.

Upvotes: 3

Related Questions