-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMazeSolver.java
49 lines (49 loc) · 983 Bytes
/
MazeSolver.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import java.util.List;
public abstract class MazeSolver {
private Maze maze;
private boolean isSolved, isSolvable;
public MazeSolver(Maze maze) {
this.maze = maze;
isSolved = false;
isSolvable = true;
};
abstract void makeEmpty();
abstract boolean isEmpty();
abstract void add(Square s);
abstract Square next();
public boolean isSolved(){
return isSolved;
};
public void step() {
if(isEmpty()) {
System.out.println("You did it!");
isSolvable = false;
return;
}
Square currSq = next();
List<Square> neighbors = maze.getNeighbors(currSq);
for(int x = 0; x < neighbors.size(); x++) {
if(neighbors.get(x).getType() == 0) {
currSq = neighbors.get(x);
}
else if(neighbors.get(x).getType() == 1) {
neighbors.remove(x);
}
}
};
public String getPath() {
if(isSolved)
return "Solved! You did it!";
else if(!isSolvable) {
return "Sorry! Find another maze!";
}
else {
return "Keep solving!";
}
};
void solve() {
while(!isSolved) {
step();
}
};
}