|
1 | 1 | import java.util.List; |
2 | 2 | import java.util.ArrayList; |
| 3 | +import controlP5.*; |
3 | 4 |
|
4 | | -int scale = 20, rows, cols; |
| 5 | +int scale, rows, cols; |
5 | 6 | List< List< Cell > > array; |
6 | 7 | List< Cell > openList, closedList, path; |
7 | 8 | Cell startNode, endNode, q; |
8 | | -int dist = 1; |
9 | | -boolean searchDone = false; |
| 9 | +boolean MousePress, searchDone, searchStarted, mouseDraw; |
| 10 | + |
| 11 | +ControlP5 startButton, pauseButton; |
| 12 | + |
| 13 | +// TODO colors |
| 14 | + |
10 | 15 | void setup(){ |
11 | 16 | size(601, 601); |
12 | | - frameRate(30); |
13 | | - background(30); |
14 | | - // PART 1 & 2 |
| 17 | + frameRate(60); |
| 18 | + background(255); |
| 19 | + |
15 | 20 | initialize(); |
| 21 | +} |
| 22 | +// DRAW CELLS ON SCREEN |
| 23 | +void draw(){ |
| 24 | + background(255); |
| 25 | + drawDownNav(); |
16 | 26 |
|
| 27 | + if( searchStarted && !searchDone ) |
| 28 | + AStar(); |
17 | 29 | } |
| 30 | +// INITIALIZE ALL |
| 31 | +void initialize(){ |
| 32 | + scale = 20; |
18 | 33 |
|
19 | | -void draw(){ |
20 | | - background(15); |
| 34 | + MousePress = false; |
| 35 | + searchDone = false; |
| 36 | + |
| 37 | + array = new ArrayList< List< Cell > >(); |
| 38 | + openList = new ArrayList< Cell >(); |
| 39 | + closedList = new ArrayList< Cell >(); |
| 40 | + path = new ArrayList< Cell >(); |
| 41 | + |
| 42 | + rows = (height - 40) / scale; |
| 43 | + cols = width / scale; |
| 44 | + |
| 45 | + for(int i = 0; i < rows; i ++){ |
| 46 | + array.add( new ArrayList<Cell>() ); |
| 47 | + for(int j = 0; j < cols; j ++){ |
| 48 | + array.get( i ).add( new Cell(i, j) ); |
| 49 | + } |
| 50 | + } |
21 | 51 |
|
22 | | - // PART 3 . |
| 52 | + mouseDraw = true; |
| 53 | + |
| 54 | + startNode = array.get( 0 ).get( 0 ); |
| 55 | + endNode = array.get( rows - 1 ).get( cols - 1 ); |
| 56 | + endNode.isBlocked = false; |
| 57 | + startNode.isBlocked = false; |
| 58 | + startNode.f = 0; |
| 59 | + startNode.g = 0; |
| 60 | + |
| 61 | + openList.add( startNode ); |
| 62 | + |
| 63 | + startButton = new ControlP5(this); |
| 64 | + startButton.addButton("Start") |
| 65 | + .setPosition(10, height - 30) |
| 66 | + .setSize(60, 20) |
| 67 | + ; |
| 68 | + pauseButton = new ControlP5(this); |
| 69 | + pauseButton.addButton("Pause") |
| 70 | + .setPosition(80, height - 30) |
| 71 | + .setSize(60, 20) |
| 72 | + ; |
| 73 | +} |
| 74 | + |
| 75 | +// A* ALGORITHM |
| 76 | +void AStar(){ |
23 | 77 | if( !openList.isEmpty() ){ |
24 | 78 |
|
25 | | - // PART 3.a . |
26 | 79 | q = openList.get(0); |
27 | 80 | for(Cell c : openList){ |
28 | 81 | if( c.f < q.f ){ |
29 | 82 | q = c; |
30 | 83 | } |
31 | 84 | } |
32 | | - // PART 3.b . |
| 85 | + |
33 | 86 | openList.remove( q ); |
34 | 87 |
|
35 | | - // PART 3.c . |
36 | 88 | List< Cell> successors = getSuccessors( q ); |
37 | 89 |
|
38 | | - // PART 3.d |
39 | 90 | float gNew, hNew, fNew; |
40 | 91 | for(Cell s : successors){ |
41 | | - // i |
| 92 | + |
42 | 93 | if( !closedList.contains( s ) && !s.isBlocked){ |
43 | | - |
| 94 | + |
44 | 95 | if(s == endNode ){ |
45 | 96 | s.parent = q; |
46 | 97 | print("Done!"); |
47 | 98 | searchDone = true; |
48 | | - noLoop(); |
| 99 | + break; |
49 | 100 | } |
50 | | - if( !closedList.contains( s ) ){ |
51 | | - |
52 | | - gNew = q.g + 1;// sqrt(2) if there a succesor is in the corner |
53 | | - hNew = heuristic(s, endNode); |
54 | | - fNew = gNew + hNew; |
55 | | - |
56 | | - if( s.f == Float.MAX_VALUE || s.f > fNew ){ |
57 | | - openList.add( s ); |
58 | | - |
59 | | - s.f = fNew; |
60 | | - s.g = gNew; |
61 | | - s.h = hNew; |
62 | | - |
63 | | - s.parent = q; |
64 | | - } |
65 | | - |
| 101 | + |
| 102 | + gNew = q.g + 1; |
| 103 | + hNew = heuristic(s, endNode); |
| 104 | + fNew = gNew + hNew; |
| 105 | + |
| 106 | + if( s.f == Float.MAX_VALUE || s.f > fNew ){ |
| 107 | + openList.add( s ); |
| 108 | + |
| 109 | + s.f = fNew; |
| 110 | + s.g = gNew; |
| 111 | + s.h = hNew; |
| 112 | + |
| 113 | + s.parent = q; |
66 | 114 | } |
67 | 115 | } |
68 | 116 | } |
69 | 117 |
|
70 | 118 | closedList.add(q); |
71 | 119 |
|
72 | | - drawArray(); |
73 | | - |
74 | | - } else { |
| 120 | + } else if( !searchDone ) { |
75 | 121 | println( "Done!" ); |
76 | 122 | searchDone = true; |
77 | | - noLoop(); |
78 | 123 | } |
79 | 124 |
|
80 | | - if( searchDone ){ |
81 | | - drawArray(); |
| 125 | + |
| 126 | + endNode.cellColor( color(234, 2, 43) ); |
| 127 | + |
| 128 | + if( !searchDone ){ |
| 129 | + for(Cell c : closedList) |
| 130 | + c.cellColor( color(110, 50, 70) ); |
| 131 | + for(Cell c : openList) |
| 132 | + c.cellColor( color(249, 85, 85) ); |
82 | 133 | } |
83 | 134 |
|
84 | | - endNode.show(234, 2, 43); |
| 135 | + path.clear(); |
85 | 136 |
|
86 | | -} |
| 137 | + Cell temp = q; |
| 138 | + path.add(temp); |
| 139 | + // GET THE PATH AND cellColor IT |
| 140 | + while( temp.parent != null ){ |
| 141 | + path.add( temp.parent ); |
| 142 | + temp = temp.parent; |
| 143 | + } |
87 | 144 |
|
| 145 | + for(Cell c : path) |
| 146 | + c.cellColor( color(43, 239, 127) ); |
| 147 | +} |
| 148 | +// GET ALL POSSIBLE SUCCESSORS |
88 | 149 | List< Cell > getSuccessors(Cell cell){ |
89 | 150 | List< Cell > s = new ArrayList< Cell >(); |
90 | 151 | try { |
@@ -113,68 +174,33 @@ List< Cell > getSuccessors(Cell cell){ |
113 | 174 | } catch (Exception e){} |
114 | 175 |
|
115 | 176 | return s; |
116 | | - |
117 | 177 | } |
118 | | - |
| 178 | +// HEURISTIC DISTANCE |
119 | 179 | float heuristic(Cell a, Cell b){ |
120 | 180 | return dist( a.j, a.i, b.j, b.i ); |
121 | | - //return abs( a.i - b.i ) + abs( a.j - b.j ); |
122 | 181 | } |
123 | 182 |
|
124 | | -void initialize(){ |
125 | | - array = new ArrayList< List< Cell > >(); |
126 | | - openList = new ArrayList< Cell >(); |
127 | | - closedList = new ArrayList< Cell >(); |
128 | | - path = new ArrayList< Cell >(); |
129 | | - |
130 | | - rows = height / scale; |
131 | | - cols = width / scale; |
132 | | - |
133 | | - for(int i = 0; i < rows; i ++){ |
134 | | - array.add( new ArrayList<Cell>() ); |
135 | | - for(int j = 0; j < cols; j ++){ |
136 | | - array.get( i ).add( new Cell(i, j) ); |
137 | | - if( random(0, 1) < 0.3 ) |
138 | | - array.get(i).get(j).isBlocked = true; |
139 | | - } |
140 | | - } |
141 | | - |
142 | | - |
143 | | - startNode = array.get( 0 ).get( 0 ); |
144 | | - endNode = array.get( rows - 1 ).get( cols - 1 ); |
145 | | - endNode.isBlocked = false; |
146 | | - startNode.isBlocked = false; |
147 | | - startNode.f = 0; |
148 | | - startNode.g = 0; |
149 | | - |
150 | | - openList.add( startNode ); |
151 | | - |
| 183 | +// DRAW THE BOTTOM BAR |
| 184 | +void drawDownNav(){ |
| 185 | + noStroke(); |
| 186 | + fill( color(38, 50, 56) ); |
| 187 | + rect(0, height - 40, width, 40 ); |
152 | 188 | } |
153 | | - |
154 | | -void drawArray(){ |
155 | | - for(int i = 0; i < rows; i ++){ |
156 | | - for(int j = 0; j < cols; j ++){ |
157 | | - array.get( i ).get( j ).show(100, 100, 100); |
158 | | - } |
159 | | - } |
160 | | - if( !searchDone ){ |
161 | | - for(Cell c : closedList) |
162 | | - c.show(110, 50, 70); |
163 | | - for(Cell c : openList) |
164 | | - c.show(249, 85, 85); |
165 | | - } |
166 | | - // GET THE PATH AND SHOW IT |
167 | | - path.clear(); |
168 | | - |
169 | | - Cell temp = q; |
170 | | - path.add(temp); |
171 | | - |
172 | | - while( temp.parent != null ){ |
173 | | - path.add( temp.parent ); |
174 | | - temp = temp.parent; |
175 | | - } |
176 | | - |
177 | | - for(Cell c : path) |
178 | | - c.show(43, 239, 127); |
179 | | - return; |
| 189 | +// MOUSE METHODS |
| 190 | +void mousePressed(){ |
| 191 | + if(mouseDraw) |
| 192 | + MousePress = true; |
| 193 | +} |
| 194 | +void mouseReleased(){ |
| 195 | + MousePress = false; |
| 196 | +} |
| 197 | +// BUTTONS |
| 198 | +void Start(){ |
| 199 | + print("Searching"); |
| 200 | + searchStarted = true; |
| 201 | + mouseDraw = false; |
| 202 | +} |
| 203 | +void Pause(){ |
| 204 | + print("Search Pause"); |
| 205 | + searchStarted = false; |
180 | 206 | } |
0 commit comments