private void findSolution() {
// Keep track of our current path. We may need to backtrack along it if we encounter a dead end.
List<Location> solutionPath = new LinkedList<Location>();
Location currentPosition = maze.getStartPosition();
MazeCell currentCell = maze.getCell(currentPosition);
// push the initial moves
stack.pushMoves( currentPosition, new IntLocation(0, 1), 1);
panel_.paintAll();
Location dir;
int depth;
boolean solved = false;
// while there are still paths to try and we have not yet encountered the finish
while ( !stack.isEmpty() && !solved ) {
GenState state = stack.remove(0); // pop
currentPosition = state.getPosition();
solutionPath.add(0, currentPosition);
if (currentPosition.equals(maze.getStopPosition())) {
solved = true;
}
dir = state.getDirection();
depth = state.getDepth();
if ( depth > currentCell.getDepth() ) {
currentCell.setDepth(depth);
}
currentCell = maze.getCell(currentPosition);
Location nextPosition = currentCell.getNextPosition(currentPosition, dir);
search(solutionPath, currentCell, dir, depth, nextPosition);
}
}