The purpose of my code is to create a program that will read in a maze and store it into a 2D array and solve it. I got the program to read the maze and put it into the array but my recursive algorithm is not working and this is the third different time I have changed it trying to get it to work. So could you help me get this to work?
Edit:I found out the problem with my original algorithm in the sense I got it to solve the maze but I have ran into another problem. The program is not printing out the correct things. Also this is just for my curiosity. I have already found out how to get it to work another way.
Maze Code:
public static boolean goNorth(){
boolean success;
if (maze[currCol][currRow - 1] == maze[finishCol][finishRow]) {
success = true;
}
if(maze[currCol][currRow - 1] == CLEAR){
maze[currCol][currRow - 1] = PATH;
currRow = currRow - 1;
success = goNorth();
if(!success){
success = goWest();
if(!success){
success = goEast();
if(!success){
maze[currCol][currRow] = VISITED;
currRow = currRow + 1;
}
}
}
return success;
} else {
return false;
}
}
public static boolean goWest(){
boolean success;
if (maze[currCol - 1][currRow] == maze[finishCol][finishRow]) {
success = true;
}
if(maze[currCol - 1][currRow] == CLEAR){
maze[currCol - 1][currRow] = PATH;
currCol = currCol - 1;
success = goWest();
if(!success){
success = goSouth();
if(!success){
success = goNorth();
if(!success){
maze[currCol][currRow] = VISITED;
currCol = currCol + 1;
}
}
}
return success;
} else {
return false;
}
}
public static boolean goEast(){
boolean success;
if (maze[currCol + 1][currRow] == maze[finishCol][finishRow]) {
success = true;
}
if(maze[currCol + 1][currRow] == CLEAR){
maze[currCol + 1][currRow] = PATH;
currCol = currCol + 1;
success = goEast();
if(!success){
success = goNorth();
if(!success){
success = goSouth();
if(!success){
maze[currCol][currRow] = VISITED;
currCol = currCol - 1;
}
}
}
return success;
} else {
return false;
}
}
public static boolean goSouth(){
boolean success;
if (maze[currCol][currRow + 1] == maze[finishCol][finishRow]){
success = true;
}
if(maze[currCol][currRow + 1] == CLEAR){
maze[currCol][currRow + 1] = PATH;
currRow = currRow + 1;
success = goSouth();
if(!success){
success = goEast();
if(!success){
success = goWest();
if(!success){
maze[currCol][currRow] = VISITED;
currRow = currRow - 1;
}
}
}
return success;
} else {
return false;
}
}
}
Maze Solver Code:
public class SolveMaze1
{
public static void main (String[] args)
{
Maze1 maze = new Maze1();
maze.readMaze();
maze.printMaze();
maze.goNorth();
maze.printMaze();
}
}
Desired outcome:
xxxxxxxxxxxxxxxxxxFx
x x xxxx x
x xxxxx xxxxx xx x
x xxxxx xxxxxxx xx x
x xx xx x
x xxxxxxxxxx xx x
xxxxxxxxxxxxSxxxxxxx
xxxxxxxxxxxxxxxxxxFx
xVVVVVxPPPPPPPxxxxPx
xVxxxxxPxxxxxPPPxxPx
xVxxxxxPxxxxxxxPxxPx
xVVVVVVPPPPPPxxPxxPx
xVxxxxxxxxxxPxxPPPPx
xxxxxxxxxxxxSxxxxxxx
My Outcome:
xxxxxxxxxxxxxxxxxxFx
xVVVVVxVVVVVVVxxxxVx
xVxxxxxVxxxxxVVVxxVx
xVxxxxxVxxxxxxxVxxVx
xVVVVVVVVVVVVxxVxxVx
xVxxxxxxxxxxVxxVVVVx
xxxxxxxxxxxxSxxxxxxx