-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGridGraph.java
More file actions
136 lines (115 loc) · 4.92 KB
/
GridGraph.java
File metadata and controls
136 lines (115 loc) · 4.92 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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import java.util.HashMap;
import java.util.Map;
import java.util.ArrayList;
public class GridGraph extends Graph {
public GridGraph(int numOfRows, int numOfCols){
this.buildGridGraph(numOfRows, numOfCols);
} // end constructor
public void buildGridGraph(int numOfRows, int numOfCols){
int totalVertices = numOfRows * numOfCols;
int currentRow = 0;
int x = 0;
int y = 0;
for(int i=0; i < totalVertices; i++){
// increment the current row
if (((i % numOfCols) == 0) && (i != 0)){
currentRow += 1;
y += 50;
x = 0;
}
String vertexId = "V" + i;
Vertex vertex = new Vertex(vertexId, x, y);
int topLeftCornerIndex = 0;
int topRightCornerIndex = numOfCols-1;
int bottomLeftCornerIndex = totalVertices-numOfCols;
int bottomRightCornerIndex = totalVertices-1;
int leftColumnIndex = (currentRow * numOfCols);
int rightColumnIndex = (currentRow * numOfCols) + (numOfCols - 1);
if(i == topLeftCornerIndex){
// vertex to right of top left corner
vertex.addEdge(vertexId, "V1");
// vertex under top left corner
vertex.addEdge(vertexId, ("V"+numOfCols));
} else if (i == topRightCornerIndex){
// vertex before
vertex.addEdge(vertexId, ("V"+(i-1)));
// vertex under
vertex.addEdge(vertexId, ("V"+(i+numOfCols)));
} else if (i == bottomLeftCornerIndex){
// vertex above
vertex.addEdge(vertexId, ("V"+(i-numOfCols)));
// vertex to right
vertex.addEdge(vertexId, ("V"+(i+1)));
} else if (i == bottomRightCornerIndex){
// vertex above
vertex.addEdge(vertexId, ("V"+(i-numOfCols)));
// vertex to left
vertex.addEdge(vertexId, ("V"+(i-1)));
} else if ((i > topLeftCornerIndex) && (i < topRightCornerIndex)){
// vertex before
vertex.addEdge(vertexId, ("V"+(i-1)));
//vertex after
vertex.addEdge(vertexId, ("V"+(i+1)));
//vertex below
vertex.addEdge(vertexId, ("V"+(i+numOfCols)));
} else if (i == leftColumnIndex){
// vertex above
vertex.addEdge(vertexId, ("V"+(i-numOfCols)));
// vertex to right
vertex.addEdge(vertexId, ("V"+(i+1)));
// vertex below
vertex.addEdge(vertexId, ("V"+(i+numOfCols)));
} else if (i == rightColumnIndex){
// vertex above
vertex.addEdge(vertexId, ("V"+(i-numOfCols)));
// vertex to left
vertex.addEdge(vertexId, ("V"+(i-1)));
// vertex below
vertex.addEdge(vertexId, ("V"+(i+numOfCols)));
} else if ((i > bottomLeftCornerIndex) && (i < bottomRightCornerIndex)){
// vertex above
vertex.addEdge(vertexId, ("V"+(i-numOfCols)));
// vertex to left
vertex.addEdge(vertexId, ("V"+(i-1)));
// vertex to right
vertex.addEdge(vertexId, ("V"+(i+1)));
} else {
// vertex above
vertex.addEdge(vertexId, ("V"+(i-numOfCols)));
// vertex to left
vertex.addEdge(vertexId, ("V"+(i-1)));
// vertex to right
vertex.addEdge(vertexId, ("V"+(i+1)));
// vertex below
vertex.addEdge(vertexId, ("V"+(i+numOfCols)));
} // end if/else chain
// now that proper edges have been placed put the vertex in the hashmap
this.vertices.put(vertexId, vertex);
x += 50;
// y += 35;
} // end for loop
} // end buildGridGraph
public void reset(){
// reset the vertices
this.vertices = new HashMap<>();
// reset visited vertices
this.visitedVertices = new StringStack();
// reset vertices to animate
this.verticesToAnimate = new ArrayList<Vertex>();
// build new graph
this.buildGridGraph(10, 10);
// and carve new maze
this.createMazeWithDFS("V0");
}
public static void main(String[] args){
GridGraph g = new GridGraph(4, 4);
g.createMazeWithDFS("V0");
// iterate through the vertices using for each loop
for(Map.Entry<String, Vertex> vertex : g.vertices.entrySet()){
System.out.println("Vertex: " + vertex.getKey());
System.out.println("Visited: " + vertex.getValue().isVisited());
vertex.getValue().getEdgesOverview();
System.out.println();
}
} // end main
} // end GridGraph