I'm writing a code to read in a txt file with a maze. Then solve that maze using recursion.
I have completely rewrote my code several time but it seem my code doesn't move from the starting point. I don't know why it's doing that.
public static void main(String[] args){
//Read in maze file...
//Find starting point
int startRow = 0;
int startColumn = 0;
for(int i = 0; i <maze[0].length; i++)
{
if(maze[6][i] == 's')
{
startRow = 6;
startColumn = i;
}
}
if(solve(maze,startRow,startColumn))
{
System.out.println("Success");
for(int r = 0; r < 7; r++)
{
for(int c = 0; c < 20; c++)
{
System.out.print(maze[r][c]);
}
System.out.println();
}
}
else
{
System.out.println("Fail");
for(int r = 0; r < 7; r++)
{
for(int c = 0; c < 20; c++)
{
System.out.print(maze[r][c]);
}
System.out.println();
}
}
}
public static boolean solve(char[][] maze, int row, int column)
{
boolean success = false;
if(valid(maze, row, column))
{
maze[row][column] = 'v'; //mark as visited
if (maze[row][column] == 'f') //check for finish
success = true;
else
{
success = solve(maze, row - 1, column); //north
if(!success)
success = solve(maze, row, column + 1); //west
if(!success)
success = solve(maze, row, column - 1); //east
if(!success)
success = solve(maze, row + 1, column); //south
}
if(success) //mark as path
maze[row][column] = 'p';
}
return success;
}
public static boolean valid(char[][] maze, int row, int column)
{
boolean a = false;
if(row >= 0 && row < maze.length && column >= 0 && column < maze[0].length)
if (maze[row][column] == ' ')
a = true;
return a;
}
}
I'm using a 7x20 text file for testing:
xxxxxxxxxxxxxxxxxxfx
x x xxxx x
x xxxxx xxxxx xx x
x xxxxx xxxxxxx xx x
x xx xx x
x xxxxxxxxxx xx x
xxxxxxxxxxxxsxxxxxxx
'x' = wall
's' = start
'f' = finish
My output:
Fail
xxxxxxxxxxxxxxxxxxfx
x x xxxx x
x xxxxx xxxxx xx x
x xxxxx xxxxxxx xx x
x xx xx x
x xxxxxxxxxx xx x
xxxxxxxxxxxxsxxxxxxx
Your search never gets started because your valid
method reports the starting point, s
as invalid.
The quick fix is to change:
if (maze[row][column] == ' ')
a = true;
to
if (maze[row][column] == ' ' || maze[row][column] == 's')
a = true;
The other problem is that you overwrite the finish cell as visited before checking for it:
maze[row][column] = 'v'; //mark as visited
if (maze[row][column] == 'f') //check for finish
success = true;
You need to restructure your solve method to look something like this:
public static boolean solve(char[][] maze, int row, int column)
{
boolean success = false;
if (maze[row][column] == 'f') //check for finish
success = true;
else if(valid(maze, row, column))
{
maze[row][column] = 'v'; //mark as visited
success = solve(maze, row - 1, column); //north
if(!success)
success = solve(maze, row, column + 1); //west
if(!success)
success = solve(maze, row, column - 1); //east
if(!success)
success = solve(maze, row + 1, column); //south
if(success) //mark as path
maze[row][column] = 'p';
}
return success;
}
With these changes your code works beautifully:
Success
xxxxxxxxxxxxxxxxxxfx
x xpppppppxxxxpx
x xxxxxpxxxxxpppxxpx
x xxxxxpxxxxxxxpxxpx
x ppppppxxpxxpx
x xxxxxxxxxxpxxppppx
xxxxxxxxxxxxpxxxxxxx