-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaze.java
100 lines (85 loc) · 2.3 KB
/
Maze.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/**
* Maze Class to implement Maze as Object
* @author Saroj Tripathi
*/
public class Maze {
private MazeEntry[][] maze2D;
private int height;
private int width;
public final int dimension = 2;
/**
* Constructor function for Maze
* @param mazeString Structure of maze as string
*/
Maze(String mazeString){
String mazeDimension = mazeString.substring(0,mazeString.indexOf('\n'));
String mazeHeight = mazeDimension.substring(0, mazeDimension.indexOf(' '));
String mazeWidth = mazeDimension.substring(mazeDimension.indexOf(' ') + 1);
this.height = Integer.parseInt(mazeHeight);
this.width = Integer.parseInt(mazeWidth);
this.maze2D = new MazeEntry[this.height][this.width];
String mazePath = mazeString.substring(mazeString.indexOf('\n') + 1);
char[] mazeChar = mazePath.toCharArray();
int k = 0;
for (int i = 0; i < this.height ; i++) {
for (int j = 0;j < this.width ; j++) {
this.maze2D[i][j] = MazeEntry.zuweisen(mazeChar[k]);
k++;
}
k++;
}
}
// Setter and Getter functions start here
public void setPos(int x, int y, MazeEntry data) {
this.maze2D[x][y] = data;
}
public MazeEntry getPos(int x, int y) {
return this.maze2D[x][y];
}
public int getHeight() {
return this.height;
}
public int getWidth() {
return this.width;
}
// Getter and Setter Functions end here
/**
* Method to give starting point in maze
* @return Starting point coordinates as array
*/
public int[] getStart(){
int[] pos = new int[this.dimension];
for (int i = 0; i < this.height; i++) {
for (int j = 0; j < this.width ; j++) {
if(this.maze2D[i][j] == MazeEntry.ENTRY) {
pos[0] = i;
pos[1] = j;
}
}
}
return pos;
}
/**
* Print the whole maze structure
*/
public void print() {
for (int i = 0; i < this.height ; i++) {
for (int j = 0; j < this.width ; j++) {
System.out.print(MazeEntry.getchar(this.maze2D[i][j]));
}
System.out.println();
}
}
/**
* Set the solution in the structure of maze
* @param path Way to follow for solution
*/
public void setSolution(WaypointList path) {
for (WaypointList list = path; list != null; list = list.getNext()) {
MazeEntry current = this.getPos(list.getXcord(), list.getYcord());
if(current == MazeEntry.PATH) {
this.setPos(list.getXcord(), list.getYcord(), MazeEntry.SOLUTION);
}
}
}
}