-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSquare.pde
295 lines (263 loc) · 5.7 KB
/
Square.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import java.lang.Math;
import java.util.LinkedList;
public class Square
{
private Maze maze;
public int l;
public int c;
private String attribute;
private boolean wall;
private int g;
private double h;
private double f;
/*
* Constructor for a "walkable" Square in the maze
* l: Line position
* c: Column position
* a: Square state
* -> S = Start position
* -> E = End position
* -> [Space] = Open
* -> * = Closed (when the solving algorithms run through the maze)
* -> o = Current position
*
* -- Note: The wall attribute will be set as false because it can't be a wall.
*/
public Square(int l, int c, String a) {
this.l = l;
this.c = c;
this.attribute = a; //S = Start; E = End; ' ' = Playable; * = Closed
this.wall = false;
this.g = 0;
this.h = 0;
this.f = 0;
}
/*
* Constructor for a wall square in the maze
* l: Line poisition
* c: Column position
* w: Wall state
* -> Set as true to define the square as a wall
*/
public Square(int l, int c, boolean w) {
this.l = l;
this.c = c;
this.attribute = " ";
this.wall = w;
this.g = 0;
this.h = 0;
this.f = 0;
}
public Square(int l, int c) {
this(l, c, false);
}
public void assignMaze(Maze m) {
this.maze = m;
}
/*
* Returns the line position of the square in the maze
*/
public int getLine() {
return l;
}
/*
* Sets the line position of the square in the maze
* l: Line position
*/
public void setLine(int l) {
this.l = l;
}
/*
* Returns the column position of the square in the maze
*/
public int getCol() {
return c;
}
/*
* Sets the column position of the square in the maze
* c: Column position
*/
public void setCol(int c) {
this.c = c;
}
/*
*
* c: The origin Square from where to get the next squares
*/
public LinkedList<Maze> getNexts() {
LinkedList<Maze> nexts = new LinkedList<Maze>();
for(int i = 0; i < 4; i++)
{
Maze tempMaze = this.maze.clone();
if(this.maze.order[i] == 'N')
{
if(this.getNorth() != null && !this.getNorth().isWall())
{
tempMaze.setNextState(this.getNorth());
nexts.push(tempMaze);
}
}
else if (this.maze.order[i] == 'E')
{
if(this.getEast() != null && !this.getEast().isWall())
{
tempMaze.setNextState(this.getEast());
nexts.push(tempMaze);
}
}
else if (this.maze.order[i] == 'S')
{
if(this.getSouth() != null && !this.getSouth().isWall())
{
tempMaze.setNextState(this.getSouth());
nexts.push(tempMaze);
}
}
else if (this.maze.order[i] == 'W')
{
if(this.getWest() != null && !this.getWest().isWall())
{
tempMaze.setNextState(this.getWest());
nexts.push(tempMaze);
}
}
}
return nexts;
}
/*
* Returns the Square at North from the given Square
* c: The origin Square from where to get the North Square
*/
public Square getNorth() {
if(this.l - 1 < 0)
return null;
else
return this.maze.getGrid()[this.l - 1][this.c];
}
/*
* Returns the Square at West from the given Square
* c: The origin Square from where to get the West Square
*/
public Square getWest() {
if(this.c - 1 < 0)
return null;
else
return this.maze.getGrid()[this.l][this.c - 1];
}
/*
* Returns the Square at South from the given Square
* c: The origin Square from where to get the South Square
*/
public Square getSouth() {
if(this.l + 1 == this.maze.lMax)
return null;
else
return this.maze.getGrid()[this.l + 1][this.c];
}
/*
* Returns the Square at East from the given Square
* c: The origin Square from where to get the East Square
*/
public Square getEast() {
if(this.c + 1 == this.maze.cMax)
return null;
else
return this.maze.getGrid()[this.l][this.c + 1];
}
/*
* Returns the square attribute
* If the square is a wall, it returns null
*/
public String getAttribute() {
return attribute;
}
/*
* Sets the square attribute
* If the attribute given is not correct, it changes nothing
*/
public void setAttribute(String attribute) {
if(attribute == " " || attribute == "S" || attribute == "E" || attribute == "*")
{
this.attribute = attribute;
this.wall = false;
}
}
/*
* Returns wall attribute
*/
public boolean isWall() {
return this.wall;
}
/*
* Sets the square as a wall
* Also sets the square attribute as null
*/
public void setWall() {
this.wall = true;
this.attribute = null;
}
//----------------------
// H Value
//----------------------
/*
* Returns H value
*/
public double getH() {
return this.h;
}
/*
* Computes the H value with the Manhattan distance
* end: The ending Square in the maze
*/
public void calcManhattanH() {
this.h = Math.abs( this.getLine() - this.maze.getEnd().getLine() )
+ Math.abs( this.getCol() - this.maze.getEnd().getCol() );
}
/*
* Computes the H value with the Euclidean distance
* end: The ending Square in the maze
*/
public void calcEuclidH() {
this.h = Math.sqrt(
Math.pow( this.getLine() - this.maze.getEnd().getLine(), 2 )
+ Math.pow( this.getCol() - this.maze.getEnd().getCol(), 2 )
);
}
//----------------------
// G Value
//----------------------
/*
* Returns G value
*/
public int getG() {
return g;
}
/*
* Increases the G value
*/
public void incG(int prev) {
this.g = 1 + prev;
}
//----------------------
// F Value
//----------------------
/*
* Computes F value by addition of H and G
*/
public double calcF() {
this.f = this.g + this.h;
return this.f;
}
/*
* Returns F value
*/
public double getF() {
return this.f;
}
/*
* Returns the Square in a string with the format "[LINE, COLUMN](F)"
*/
public String toString() {
return "[" + this.l + ", " + this.c + "](" + this.f + ")";
}
}