-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRandomPathFinder.java
executable file
·46 lines (36 loc) · 1.03 KB
/
RandomPathFinder.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
import java.util.*;
class RandomPathFinder implements PathFinder {
public LinkedList<DirType> findPath(Maze maze){
LinkedList<DirType> solutionList = new LinkedList<DirType>();
int x = 0,y = 0,i = 0;
int maxX = maze.getRows();
int maxY = maze.getColumns();
double rand;
Square[][] mazeArray = maze.getMaze();
boolean finish = false;
while(!finish) {
rand = Math.random();
if(rand < 0.3 && mazeArray[y][x].e == false) {
solutionList.add(DirType.East);
x++;
} else if(rand >= 0.3 && rand < 0.6 && mazeArray[y][x].s == false) {
solutionList.add(DirType.South);
y++;
} else if(rand >= 0.6 && rand <= 0.8 && mazeArray[y][x].w == false) {
solutionList.add(DirType.West);
x--;
} else if(rand > 0.8 && mazeArray[y][x].n == false) {
solutionList.add(DirType.North);
y--;
}
if(x == maxX-1 && y == maxY-1) {
finish = true;
}
}
if(finish == true) {
return solutionList;
} else {
return new LinkedList<DirType>();
}
}
}