-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMazeSolver.java
78 lines (63 loc) · 1.91 KB
/
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package com.mazechallange;
import java.util.ArrayDeque;
import java.util.List;
import javax.enterprise.event.Observes;
import javax.inject.Inject;
import org.jboss.weld.environment.se.bindings.Parameters;
import org.jboss.weld.environment.se.events.ContainerInitialized;
import com.mazechallange.model.Cell;
import com.mazechallange.model.DirectionEnum;
import com.mazechallange.model.Maze;
import com.mazechallange.model.RouteSituationEnum;
// Challenge: http://www.epdeveloperchallenge.com/
public class MazeSolver {
@Inject private Maze maze;
private Cell currentCell;
private ArrayDeque<Path> possiblePaths;
private int moves;
public String solveMaze(@Observes ContainerInitialized event, @Parameters List<String> parameters) {
initMaze();
String note = findMazeSolution();
System.out.println(note);
System.out.println("moves:" + moves);
return note;
}
public void setMaze(Maze maze) {
this.maze = maze;
}
private void initMaze() {
currentCell = maze.getCurrentCell();
possiblePaths = new ArrayDeque<>();
}
private String findMazeSolution() {
while (!currentCell.isAtMazeEnd()) {
findPossiblePaths();
Path nextPath = possiblePaths.removeFirst();
followPath(nextPath);
}
return maze.getNote();
}
private void findPossiblePaths() {
for (DirectionEnum direction : DirectionEnum.values()) {
if (isDirectionUnexplored(direction)) {
possiblePaths.addFirst(new Path(currentCell, direction));
}
}
}
private void followPath(Path path) {
goToPathStart(path.getStartCell());
System.out.println("Moving to: " + path);
maze.moveTo(path.getDirection());
moves++;
currentCell = maze.getCurrentCell();
}
private void goToPathStart(Cell nextCell) {
if (!nextCell.equals(currentCell)) {
maze.moveTo(nextCell);
moves++;
}
}
private boolean isDirectionUnexplored(DirectionEnum direction) {
return maze.getRouteSituation(direction).equals(RouteSituationEnum.UNEXPLORED);
}
}