-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMazeIterator.java
More file actions
51 lines (47 loc) · 1.69 KB
/
MazeIterator.java
File metadata and controls
51 lines (47 loc) · 1.69 KB
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
/**
* This class calls the maze for iteration.
* @author Saroj Tripathi
*
*/
public class MazeIterator {
public static int[][] vektoren = {{0,1},{0,-1},{1,0},{-1,0}};
/**
* Get starting point intialize path and call recursive maze Algorithm
* @param myMaze Maze object to play with
* @return Solved Maze Path (Linked list)
*/
public static WaypointList iterateMaze(Maze myMaze) {
int[] startPos = myMaze.getStart();
WaypointList myPath = new WaypointList(startPos[0],startPos[1]);
myPath = MazeIterator.iterateMazeRec(myMaze,myPath);
return myPath;
}
/**
* Iterate through maze and find path using 4 direction vectors
* @param myMaze Maze object to play with
* @return Null if Path not found else Path
*/
public static WaypointList iterateMazeRec(Maze myMaze, WaypointList myPath) {
for (int[] vektor : MazeIterator.vektoren)
{
int newXpos = myPath.getXcord() + vektor[0];
int newYpos = myPath.getYcord() + vektor[1];
if(newXpos >= 0 && newXpos < myMaze.getWidth() &&
newYpos >= 0 && newYpos < myMaze.getHeight()) {
if(myMaze.getPos(newXpos, newYpos) == MazeEntry.PATH &&
myPath.contains(newXpos, newYpos) == 0) {
WaypointList copyPath = new WaypointList(newXpos, newYpos, myPath);
copyPath = MazeIterator.iterateMazeRec(myMaze, copyPath);
if(copyPath != null) {
myPath = new WaypointList(newXpos, newYpos, copyPath);
return myPath;
}
} else if(myMaze.getPos(newXpos, newYpos) == MazeEntry.EXIT){
myPath = new WaypointList(newXpos, newYpos, myPath);
return myPath;
}
}
}
return null;
}
}