-
Notifications
You must be signed in to change notification settings - Fork 0
/
Maze.pde
441 lines (387 loc) · 12.1 KB
/
Maze.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class Maze
{
private Square[][] grid;
private Square start;
private Square end;
private Square currState;
public int lMax;
public int cMax;
public char[] order = {'N', 'E', 'S', 'W'}; //Shift order in the grid during solving in cardinals
public Minoutar[] minoutars;
public Player player;
public Square[] minPos;
public Stor stor;
public Gun gun;
public Square gunPos;
/*
* Constructor with no file
* lRange: Number of lines in the maze
* cRange: Number of columns in the maze
* start: The starting square of the maze
* end: the ending square of the maze
*/
public Maze(int[][] maze, Square start, Square end, Square[] minPos, Square gunPos, Stor stor) {
//Set max values
this.lMax = maze.length;
this.cMax = maze[0].length;
this.minPos = minPos;
this.stor = stor;
//Init grid
this.grid = new Square[lMax][cMax];
for (int l = 0; l < lMax; l++) {
for (int c = 0; c < cMax; c++) {
this.grid[l][c] = new Square(l,c, maze[l][c] == 0);
}
}
this.assignMazeToGridSquares();
//Create start and end MazeState objects contaning the start and end squares (stated)
this.end = end;
this.start = start;
this.currState = this.getStart();
//At this point, the grid is and stay the inital Maze unsolved.
player = new Player(start.c, start.l, this);
minoutars = new Minoutar[minPos.length];
int i = 0;
for (Square s : minPos) {
minoutars[i] = new Minoutar(s.c, s.l, this, player);
i++;
}
this.gunPos = gunPos;
gun = new Gun(gunPos.c, gunPos.l, this);
}
public void keyPressed() {
player.keyPressed();
}
public void update() {
draw();
player.update();
gun.update();
for (Minoutar m : minoutars) {
m.update();
}
player.moved = false;
}
public boolean validPos(int x, int y) {
return x >= 0 && x < cMax && y >= 0 && y < lMax && !grid[y][x].isWall();
}
public Maze(Square[][] grid, Square start, Square end, Square currState, int lMax, int cMax) {
this.grid = grid;
this.start = start;
this.end = end;
this.currState = currState;
this.lMax = lMax;
this.cMax = cMax;
}
/**
* Resets the maze
*/
public void reset() {
this.currState = this.getStart();
player = new Player(start.c, start.l, this);
minoutars = new Minoutar[minPos.length];
int i = 0;
for (Square s : minPos) {
minoutars[i] = new Minoutar(s.c, s.l, this, player);
i++;
}
gun = new Gun(gunPos.c, gunPos.l, this);
transitionIn.reset();
}
/*
* Retuns the starting Square
*/
public Square getStart() {
return start;
}
/*
* Sets the starting Square for the attribute and the grid
* start: The square to set as starting square
*/
public void setStart(Square start) {
this.start = start;
this.grid[start.getLine()][start.getCol()] = start;
}
/*
* Returns the ending Square
*/
public Square getEnd() {
return end;
}
/*
* Sets the ending Square for the attribute and the grid
* end: The square to set as ending square
*/
public void setEnd(Square end) {
this.end = end;
this.grid[end.getLine()][end.getCol()] = end;
}
/*
* Sets a wall in the maze
* l: Line position of the wall
* c: Column position of the wall
*/
public void setMazeWall(int l, int c) {
this.grid[l][c].setWall();
}
public Square getCurrState() {
return this.currState;
}
public void setCurrState(Square c) {
this.currState = c;
}
public void setNextState(Square c) {
this.grid[this.currState.getLine()][this.currState.getCol()].setAttribute("*");
this.currState = c;
}
public void assignMazeToGridSquares() {
for (int i = 0; i < this.lMax; i++)
{
for (int j = 0; j < this.cMax; j++) {
this.grid[i][j].assignMaze(this);
}
}
}
/*
* Initiates the maze
*/
public void initMaze() {
//Init grid
this.resetGrid();
this.currState = this.getStart();
}
public void resetGrid() {
for (int i = 0; i < this.lMax; i++) {
for (int j = 0; j < this.cMax; j++) {
if (this.grid[i][j].getAttribute() == "*")
this.grid[i][j].setAttribute(" ");
}
}
}
/*
* Sets a new shift order
* newOrder: 4 entries char array, each char (in caps) means a shift direction, for example : ['N', 'S', 'W', 'E'] will shifts North -> South -> West -> East.
*/
public void setOrder(char[] newOrder) {
if (newOrder.length == 4) {
this.order = newOrder;
}
}
/*
* Resets the default order North -> East -> South -> West (Clockwise)
*/
public void resetOrder() {
char[] o = {'N', 'E', 'S', 'W'};
this.order = o;
}
/*
* Returns the maze grid
*/
public Square[][] getGrid() {
return grid;
}
public Maze clone() {
return new Maze(this.grid, this.start, this.end, this.currState, this.lMax, this.cMax);
}
/*
* Get the maze in a unicode box draw format
*/
public String toString() {
String res = " ";
String res_under = "";
Square temp = null;
Square templineunder = null;
Square tempnextcol = null;
Square tempdiag = null;
//Columns numbers
for (int i = 0; i < cMax; i++) {
if (i < 10) res += " " + i + " "; else res += " " + i;
}
res += "\n ╔";
//First row : Maze top edge
for (int i = 1; i < this.cMax; i++) {
temp = this.grid[0][i - 1];
tempnextcol = this.grid[0][i];
if (temp.isWall()) res += "═══╤"; else if (tempnextcol.isWall()) res +=
"═══╤"; else res += "════";
}
res += "═══╗\n";
//Browse all squares
// res = the line containing the square states
// res_under = the graphics under the squares line with the corner unicode characters
// contatenation of res + res_under at each line
//Example :
// │ │ │ │ <- res
// └───┼───┼───┘ <- res_under
// │ │ <- res
// └───┘ <- res_under
// etc...
for (int l = 0; l < this.lMax; l++) {
res_under = "";
for (int c = 0; c < this.cMax; c++) {
//Get Squares
temp = this.grid[l][c]; // = A -> Current square
tempnextcol = temp.getEast(); // = B -> Square at the right of temp
templineunder = temp.getSouth(); // = C -> Square below temp
if (l < this.lMax - 1 && c < this.cMax - 1) tempdiag =
templineunder.getEast(); // = D -> Square in the temp lower right-hand diagonal
if (c == 0) { //First colomn of current line l
if (l < 10) res += l + " ║"; else res += l + " ║";
if (templineunder != null) {
if (temp.isWall() || templineunder.isWall()) res_under +=
" ╟"; else res_under += " ║";
}
}
if (temp.isWall()) {
res += " ";
res_under += "───";
} else {
if (
temp.getLine() == this.currState.getLine() &&
temp.getCol() == this.currState.getCol()
) res += " o "; else if (
temp.getLine() == this.start.getLine() &&
temp.getCol() == this.start.getCol()
) res += " S "; else if (
temp.getLine() == this.end.getLine() &&
temp.getCol() == this.end.getCol()
) res += " E "; else res += " " + temp.getAttribute() + " ";
if (l < this.lMax - 1) {
if (templineunder.isWall()) res_under += "───"; else res_under +=
" ";
}
}
//Maze right edge
if (c == this.cMax - 1) {
res += "║";
if (temp != null && templineunder != null) {
if (temp.isWall() || templineunder.isWall()) res_under +=
"╢"; else res_under += "║";
}
} else {
//Squares corners.
// two cases : wall square or not
if (temp.isWall()) {
res += "│";
if (
templineunder != null && tempdiag != null && tempnextcol != null
) {
//"┼" = (B + D).(C + D) -> The most reccurent corner to write
if (
(tempnextcol.isWall() || tempdiag.isWall()) &&
(templineunder.isWall() || tempdiag.isWall())
) res_under += "┼"; else {
if (
!templineunder.isWall() &&
!tempdiag.isWall() &&
!tempnextcol.isWall()
) res_under += "┘"; else if ( //Wall on top left only
!templineunder.isWall() &&
!tempdiag.isWall() &&
tempnextcol.isWall()
) res_under += "┴"; else if ( // Walls on top
templineunder.isWall() &&
!tempdiag.isWall() &&
!tempnextcol.isWall()
) res_under += "┤"; // Walls on left
}
}
} else {
if (tempnextcol != null) {
if (tempnextcol.isWall()) res += "│"; else res += " ";
if (templineunder != null && tempdiag != null) {
//"┼" = (C).(D) -> The most reccurent corner to write
if (templineunder.isWall() && tempnextcol.isWall()) res_under +=
"┼"; else {
if (
!templineunder.isWall() &&
!tempdiag.isWall() &&
!tempnextcol.isWall()
) res_under += " "; else if ( //No wall
!templineunder.isWall() &&
tempdiag.isWall() &&
!tempnextcol.isWall()
) res_under += "┌"; else if ( //Wall on right below
templineunder.isWall() &&
!tempdiag.isWall() &&
!tempnextcol.isWall()
) res_under += "┐"; else if ( //Wall on left below
!templineunder.isWall() &&
!tempdiag.isWall() &&
tempnextcol.isWall()
) res_under += "└"; else if ( //Wall on top right
!templineunder.isWall() &&
tempdiag.isWall() &&
tempnextcol.isWall()
) res_under += "├"; else if ( //Walls on right
templineunder.isWall() &&
tempdiag.isWall() &&
!tempnextcol.isWall()
) res_under += "┬"; //Walls below
}
}
}
}
}
} //<- for each column
if (l < this.lMax - 1) res += "\n" + res_under + "\n"; //Concatenate res + res_under
else {
//Maze bottom edge
res += "\n ╚";
for (int i = 1; i < this.cMax; i++) {
temp = this.getGrid()[l][i - 1];
if (temp.getEast().isWall() || temp.isWall()) res +=
"═══╧"; else res += "════";
}
res += "═══╝\n";
}
}
/*//Affichage simple
String res = " ";
for(int i = 0; i < cMax; i++)
{
if(i >= 9)
res += " " + i;
else
res += " " + i + " ";
}
res += "\n";
for(int i = 0; i < this.lMax; i++)
{
if(i > 9)
res += i + " ";
else
res += i + " ";
for(int j = 0; j < this.cMax; j++)
{
if(this.getGrid()[i][j].getAttribute() != "" && !this.getGrid()[i][j].isWall())
res += "[" + this.getGrid()[i][j].getAttribute() + "]";
else
res += "[■]";
}
res += "\n";
}*/
return res;
}
public void draw() {
push();
fill(0);
for (int x = 0; x < cMax; x++) {
for (int y = 0; y < lMax; y++) {
Square s = grid[y][x];
if (s.isWall()) {
square(x * Settings.STEP, y * Settings.STEP, Settings.STEP);
}
}
}
// Fill in the start square with the color green
fill(0, 255, 0);
square(start.getCol() * Settings.STEP, start.getLine() * Settings.STEP, Settings.STEP);
fill(0, 0, 255);
square(end.getCol() * Settings.STEP, end.getLine() * Settings.STEP, Settings.STEP);
pop();
}
}