I\'ve been assigned with the task of creating a maze solver in Java. Here\'s the assignment:
Write an application that finds a path through a maze.
The maze sh
I tried to implement this using DFS algorithm utilizing some Java OOP concepts.
See complete solution on my github repository
private boolean solveDfs() {
Block block = stack.peekFirst();
if (block == null) {
// stack empty and not reached the finish yet; no solution
return false;
} else if (block.equals(maze.getEnd())) {
// reached finish, exit the program
return true;
} else {
Block next = maze.getNextAisle(block);
// System.out.println("next:" + next);
if (next == null) {
// Dead end, chose alternate path
Block discard = stack.pop();
discard.setInPath(false);
// System.out.println("Popped:" + discard);
} else {
// Traverse next block
next.setVisited(true);
next.setInPath(true);
stack.push(next);
}
}
return solveDfs();
}