Good evening everyone
I've been working on this programming assignment for quite a bit and can't seem to get a certain recursive method to work.
Before posting the code (since theprogram is pretty long) here's all the info you might ask me for(thanks in advance!):
It is a program design to find and
highlight a solution to a maze.
A maze is composed of a grid of rooms
held in a 2D array.
Each room has at least one open door,
and several walls.
If a wall is in place, I can't continue
down that path.
The requirements are:
I must have a method that gives me an
array of doors (a sub class of the Maze class) that are highlighted
and show a path that is displayed when the program is run.
I have to use a boolean array to mark
my path to make sure I don't go in circles.
Each wall has an int designating it,
the North wall is 0, east is 1, etc.
So far, this is my code:
public class MazeSolution implements MazeSolver {
boolean[][] marked;
static int lastDoor = -1;
ArrayList<Maze.Door> correctPath = new ArrayList<>();
public ArrayList<Maze.Door> solveMaze(int startRow, int finishRow, int startCol, int finishCol, Maze maze) {
marked = new boolean[maze.getRows()][maze.getColumns()];
return solveMaze(startRow, finishRow, startCol, finishCol, maze, marked);
} // end getPath
public ArrayList<Maze.Door> solveMaze(int startRow, int finishRow, int startCol, int finishCol, Maze maze, boolean[][] marked) {
int row = startRow;
int column = startCol;
boolean door = true;
if (row == finishRow && column == finishCol) {
correctPath.add(new Maze.Door(row, column, maze.NO_DIRECTION, Color.red));
return correctPath;
}
for (int wall = 0; wall < 4; wall++) {
boolean wallExists = maze.isClosed(row, column, wall);
if (!wallExists) {
if (wall == 0) {
if (!marked[row - 1][column]) {
lastDoor = wall;
door = wallExists;
}
}
if (wall == 1) {
if (!marked[row][column + 1]) {
lastDoor = wall;
door = wallExists;
}
}
if (wall == 2) {
if (!marked[row + 1][column]) {
lastDoor = wall;
door = wallExists;
}
if (wall == 3) {
if (!marked[row][column - 1]) {
lastDoor = wall;
door = wallExists;
}
}
}
}
System.out.println(lastDoor + "\t" + door + "\t");
if (door) {
System.out.println(lastDoor + "::" + door);
marked[row][column] = true;
int doorLocale = lastDoor;
if (doorLocale == 0) {
return solveMaze(row + 1, finishRow, column, finishCol, maze, marked);
}
if (doorLocale == 1) {
return solveMaze(row, finishRow, column - 1, finishCol, maze, marked);
}
if (doorLocale == 2) {
return solveMaze(row - 1, finishRow, column, finishCol, maze, marked);
}
if (doorLocale == 3) {
return solveMaze(row, finishRow, column+1, finishCol, maze, marked);
}
}
if (!door && lastDoor == 0) {
marked[row][column] = true;
correctPath.add(new Maze.Door(row, column, maze.NO_DIRECTION, Color.red));
return solveMaze(row - 1, finishRow, column, finishCol, maze, marked);
}
if (!door && lastDoor == 1) {
marked[row][column] = true;
correctPath.add(new Maze.Door(row, column, maze.NO_DIRECTION, Color.red));
return solveMaze(row, finishRow, column + 1, finishCol, maze, marked);
}
if (!door && lastDoor == 2) {
marked[row][column] = true;
correctPath.add(new Maze.Door(row, column, maze.NO_DIRECTION, Color.red));
return solveMaze(row + 1, finishRow, column, finishCol, maze, marked);
}
if (!door && lastDoor == 3) {
marked[startRow][startCol] = true;
correctPath.add(new Maze.Door(startRow, startCol, maze.NO_DIRECTION, Color.red));
return solveMaze(row, finishRow, column - 1, finishCol, maze, marked);
}
}
return null;
}
}
Can someone help me figure out why my code isn't working? I've been stuck on this for days and can't see what's wrong.
Thank you!
Edited by zerolink, 05 March 2013 - 06:13 PM.

















