-
Notifications
You must be signed in to change notification settings - Fork 0
/
DFS.pde
218 lines (189 loc) · 3.9 KB
/
DFS.pde
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import java.util.LinkedList;
import java.util.Stack;
import java.util.Set;
import java.util.HashSet;
import java.util.Random;
import javafx.util.Pair;
int cols = 20;
int rows = 20;
int w, h;
int start_i=0, start_j=0;
int target_i = rows-1, target_j=cols-1;
Stack<Cell> s;
Set<Pair<Integer, Integer>> walls;
Cell root, end;
Cell grid[][];
Cell child;
boolean found;
boolean target_identified;
boolean start_identified;
boolean clicked;
boolean finished;
void setup()
{
size(800, 805);
background(255);
target_identified = false;
start_identified = false;
clicked = false;
finished = false;
found = false;
w = width / rows - 5;
h = height / cols;
grid = new Cell[rows][cols];
randomize_walls();
init();
end = grid[target_i][target_j];
child = null;
s = new Stack<Cell>();
}
void draw()
{
if(keyPressed)
{
if(key == ' ')
{
setup();
}
if(key == '\n')
{
exit();
}
}
if(!clicked && mousePressed && !start_identified)
{
start_i = mouseY / w;
start_j = mouseX / h;
if(start_i < rows && start_j < cols)
{
start_identified = true;
root = grid[start_i][start_j];
if(root.wall)
{
start_identified = false;
clicked = false;
}
else
{
root.parent = null;
root.setVisited(true);
root.col = color(0, 255, 0);
s.push(root);
}
}
}
else if(clicked && mousePressed && !target_identified)
{
target_i = mouseY / w;
target_j = mouseX / h;
if(start_i < rows && start_j < cols)
{
target_identified = true;
end = grid[target_i][target_j];
if(end.wall)
target_identified = false;
else
end.col = color(255, 0, 0);
}
}
if(start_identified && target_identified)
{
if(!s.isEmpty() && !found)
{
Cell current = s.pop();
if(current == end)
{
found = true;
child = end;
end.setCol(color(0, 255, 0));
println("Found\n" + end.i + "\t" + end.j);
}
else
{
if(current.explore_Neighbours(grid, s, end))
{
found = true;
child = end;
end.setCol(color(0, 255, 0));
println("Found\n" + end.i + "\t" + end.j);
}
}
}
else if(s.isEmpty())
{
finished = true;
}
else if(found)
{
child = find_parent(child);
}
}
fill(0);
textSize(20);
text("Choose the starting cell, then the end cell.\nPress SPACE to start again.\nPress ENTER to exit.", 10, 722);
show();
}
Cell find_parent(Cell temp)
{
Cell current = temp;
if(current.parent != null)
{
current = current.parent;
current.setCol(color(0, 255, 0));
}
else
{
current.setCol(color(0, 192, 255));
end.col = color(255, 0, 0);
finished = true;
}
return current;
}
void show()
{
for(int i=0; i<rows; i++)
{
for(int j=0; j<cols; j++)
{
grid[i][j].show();
}
}
}
void init()
{
for(int i=0; i<rows; i++)
{
for(int j=0; j<cols; j++)
{
grid[i][j] = new Cell(i, j, w, h, rows, cols);
if(walls.contains(new Pair(i, j)))
grid[i][j].setWall(true);
}
}
}
void randomize_walls()
{
walls = new HashSet<Pair<Integer, Integer>>();
Pair<Integer, Integer> p;
int number_of_walls = ((rows*cols) / 3);
Random rnd = new Random();
for(int n=0; n<number_of_walls; n++)
{
int i = rnd.nextInt(rows);
int j = rnd.nextInt(cols);
p = new Pair(i, j);
if(walls.contains(p))
{
n--;
}
else
walls.add(p);
}
}
void mouseClicked()
{
if(!clicked && start_identified)
{
clicked = true;
}
}