-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMazeGenerator.java
More file actions
733 lines (633 loc) · 22.9 KB
/
MazeGenerator.java
File metadata and controls
733 lines (633 loc) · 22.9 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
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
package maze;
import java.util.Scanner;
import java.util.Stack;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Queue;
/**
* This program generates mazes and solves them using Breadth-first Search and
* Depth-first Search algorithms
*
* @author Nhat Nguyen
* @author Jasmine Mai
*/
public class MazeGenerator {
public static void main(String[] args) {
while (true) {
System.out.println("-------------------------------------------------------------------");
// User Input for Maze Size
@SuppressWarnings("resource")
Scanner scan = new Scanner(System.in);
int size = 0;
do {
System.out.print("Input the size for the maze: ");
while (!scan.hasNextInt()) {
System.out.println("Needs a valid integer for maze size (3 or Higher)");
System.out.print("Input the size for the maze: ");
scan.next();
}
size = scan.nextInt();
} while (size <= 2);
System.out.println();
// Constructs a new 2D array
String[][] maze2D = maze2D(size);
// print2D(maze2D);
// System.out.println("Generated 2D Array of Size " + size + "x" + size);
// System.out.println();
// Prints out new maze as 2D array
String[][] mazeGenerated = generator(maze2D);
// print2D(mazeGenerated);
// System.out.println("Generated Maze as 2D Array");
// System.out.println();
// Prints the string representation of maze
System.out.println(convert2D(mazeGenerated));
System.out.println("String Representation of Generated " + size + "x" + size + " Maze");
System.out.println();
// Delete the Hash symbol in the maze
mazeGenerated = emptyHash(mazeGenerated);
// print2D(mazeGenerated);
// System.out.println("Empty Generated Maze");
// System.out.println();
// Depth First Search
String[][] mazeDFS = DFS(clone(mazeGenerated));
// print2D(mazeDFS);
// System.out.println("DFS Maze as 2D Array");
System.out.println();
// String representation of DFS
System.out.println(convert2D(mazeDFS));
System.out.println("String representation of DFS Maze");
System.out.println();
// Breadth First Search
String[][] mazeBFS = BFS(clone(mazeGenerated));
print2D(mazeBFS);
System.out.println("BFS Maze as 2D Array");
System.out.println();
// String representation of DFS
System.out.println(convert2D(mazeBFS));
System.out.println("String representation of BFS Maze");
System.out.println();
// Queue<Cell> q = new LinkedList<Cell>();
// q.add(new Cell(1, 1));
// q.add(new Cell(1, 2));
// q.add(new Cell(2, 1));
// q.add(new Cell(2, 2));
// System.out.println("Elements of queue: " + q);
// System.out.println("Queue removed: " + q.remove());
// System.out.println("Head of the queue: " + q.peek());
// System.out.println("Elements of queue: " + q);
// ArrayList<String> direction = new ArrayList<>();
// Collections.addAll(direction, "NORTH", "EAST", "SOUTH", "WEST");
// direction.add("FAKE");
// System.out.println("Stack removed: " + direction.remove(direction.size() -
// 1));
System.out.println();
// System.out.println("Press anything to repeat the program.");
// Object repeat = scan.next();
// //System.out.println("Press anything to repeat the program.");
System.out.println(Integer.parseInt("10P"));
}
}
/**
* Prints the 2D matrix
*
* @param array2D
* The 2D matrix that represents the maze
*/
public static void print2D(String[][] array2D) {
for (String[] row : array2D) {
System.out.println(Arrays.toString(row));
}
}
/**
* Clones 2D array to new 2D array
*
* @param maze2D
* The 2D matrix that represents the maze
* @return clone2D The copy of the 2D matrix that represents the maze
*/
public static String[][] clone(String[][] maze2D) {
String[][] clone2D = new String[maze2D.length][maze2D.length];
for (int columnIndex = 0; columnIndex < maze2D.length; columnIndex++) {
for (int rowIndex = 0; rowIndex < maze2D.length; rowIndex++) {
clone2D[columnIndex][rowIndex] = maze2D[columnIndex][rowIndex];
}
}
return clone2D;
}
// Creates a 2D array with walls
public static String[][] maze2D(int size) {
String[][] maze2D = new String[2 * size + 1][2 * size + 1];
// 2D Array
for (int columnIndex = 0; columnIndex < (2 * size + 1); columnIndex++) {
for (int rowIndex = 0; rowIndex < (2 * size + 1); rowIndex++) {
// Start of maze
if (rowIndex == 1 && columnIndex == 0) {
maze2D[columnIndex][rowIndex] = "S";
// End of maze
} else if (rowIndex == 2 * size - 1 && columnIndex == 2 * size) {
maze2D[columnIndex][rowIndex] = "E";
// Row is even
} else if (rowIndex % 2 == 0) {
// Column is even
if (columnIndex % 2 == 0) {
maze2D[columnIndex][rowIndex] = "+";
// Column is odd
} else {
maze2D[columnIndex][rowIndex] = "|";
}
// Row is odd
} else {
// Column is even
if (columnIndex % 2 == 0) {
maze2D[columnIndex][rowIndex] = "-";
// Column is odd
} else {
maze2D[columnIndex][rowIndex] = "0";
}
}
}
}
return maze2D;
}
// Generates a maze
public static String[][] generator(String[][] maze2D) {
Stack<Cell> location = new Stack<Cell>();
int size = (maze2D.length - 1) / 2;
int totalCells = size * size;
int visitedCells = 1;
Cell current = new Cell(0, 0);
while (visitedCells < totalCells) {
// Generates a unique direction
ArrayList<String> direction = new ArrayList<>();
Collections.addAll(direction, "NORTH", "EAST", "SOUTH", "WEST");
Collections.shuffle(direction);
String random = validSpot(maze2D, current, direction);
if (random == "BACKTRACK") {
// // DEBUGGING: Prints BACKTRACKING
// System.out.println("\t PROCESSS: " + random);
current = location.pop();
continue;
}
current = move(maze2D, current, random);
visitedCells = visitedCells + 1;
location.push(current);
}
return maze2D;
}
// Finds valid spot to move for maze generation
public static String validSpot(String[][] maze2D, Cell current, ArrayList<String> direction) {
int size = (maze2D.length - 1) / 2;
int x = 2 * current.getx() + 1;
int y = 2 * current.gety() + 1;
// When the size of the list is 0, return -1
if (direction.size() == 0) {
return "BACKTRACK";
}
String random = direction.remove(0);
// // DEBUGGING: Prints current direction
// System.out.println("DIRECTION: " + random);
if (random == "NORTH") {
if (current.gety() - 1 < 0) {
// System.out.println("Do not go NORTH because outside of range of the 2D
// array");
return validSpot(maze2D, current, direction);
}
if ((maze2D[y - 3][x] == "#" || maze2D[y - 1][x] == "#")
|| (maze2D[y - 2][x - 1] == "#" || maze2D[y - 2][x + 1] == "#")) {
// System.out.println("Do not go NORTH because that cell is not enclosed by
// walls");
return validSpot(maze2D, current, direction);
}
} else if (random == "EAST") {
if (current.getx() + 1 >= size) {
// System.out.println("Do not go EAST because outside of range of the 2D
// array");
return validSpot(maze2D, current, direction);
}
if (((maze2D[y + 1][x + 2] == "#" || maze2D[y - 1][x + 2] == "#")
|| (maze2D[y][x + 1] == "#" || maze2D[y][x + 3] == "#"))) {
// System.out.println("Do not go EAST because that cell is not enclosed by
// walls");
return validSpot(maze2D, current, direction);
}
} else if (random == "SOUTH") {
if (current.gety() + 1 >= size) {
// System.out.println("Do not go SOUTH because outside of range of the 2D
// array");
return validSpot(maze2D, current, direction);
}
if (((maze2D[y + 1][x] == "#" || maze2D[y + 3][x] == "#")
|| (maze2D[y + 2][x - 1] == "#" || maze2D[y + 2][x + 1] == "#"))) {
// System.out.println("Do not go SOUTH because that cell is not enclosed by
// walls");
return validSpot(maze2D, current, direction);
}
} else if (random == "WEST") {
if (current.getx() - 1 < 0) {
// System.out.println("Do not go WEST because outside of range of the 2D
// array");
return validSpot(maze2D, current, direction);
}
if (((maze2D[y - 1][x - 2] == "#" || maze2D[y + 1][x - 2] == "#")
|| (maze2D[y][x - 3] == "#" || maze2D[y][x - 1] == "#"))) {
// System.out.println("Do not go WEST because that cell is not enclosed by
// walls");
return validSpot(maze2D, current, direction);
}
}
return random;
}
// Moves the next cell and breaks the wall in between
public static Cell move(String[][] maze2D, Cell current, String random) {
// // Prints out the coordinates of the current cell object
// System.out.println(" X-coordinate: " + current.getx() + ", Y-coordinate: " +
// current.gety());
maze2D[1][1] = "#";
if (random == "NORTH") {
// NORTH and delete wall from bottom from next cell
current.setNext(new Cell(current.getx(), current.gety() - 1));
current = current.getNext();
// Breaks the bottom wall from next cell
maze2D[2 * current.gety() + 2][2 * current.getx() + 1] = "#";
// DEBUGGING: Visualizing
maze2D[2 * current.gety() + 1][2 * current.getx() + 1] = "#";
} else if (random == "EAST") {
// EAST and delete wall from left from next cell
current.setNext(new Cell(current.getx() + 1, current.gety()));
current = current.getNext();
// Breaks the left wall from next cell
maze2D[2 * current.gety() + 1][2 * current.getx()] = "#";
// DEBUGGING: Visualizing
maze2D[2 * current.gety() + 1][2 * current.getx() + 1] = "#";
} else if (random == "SOUTH") {
// SOUTH and delete wall from top from next cell
current.setNext(new Cell(current.getx(), current.gety() + 1));
current = current.getNext();
// Breaks the top wall from next cell
maze2D[2 * current.gety()][2 * current.getx() + 1] = "#";
// DEBUGGING: Visualizing
maze2D[2 * current.gety() + 1][2 * current.getx() + 1] = "#";
} else if (random == "WEST") {
// WEST and delete wall from right from next cell
current.setNext(new Cell(current.getx() - 1, current.gety()));
current = current.getNext();
// Breaks the right wall from next cell
maze2D[2 * current.gety() + 1][2 * current.getx() + 2] = "#";
// DEBUGGING: Visualizing
maze2D[2 * current.gety() + 1][2 * current.getx() + 1] = "#";
}
// // DEBUGGING: Printing maze at each step
// System.out.println("NEW X-coordinate: " + current.getx() + ", NEW
// Y-coordinate: " + current.gety());
// for (String[] row : maze2D) {
// System.out.println(Arrays.toString(row));
// }
// System.out.println();
return current;
}
// Converts 2D array maze to string representation
public static String convert2D(String[][] maze2D) {
String maze = "";
int size = maze2D.length;
for (int columnIndex = 0; columnIndex < size; columnIndex++) {
for (int rowIndex = 0; rowIndex < size; rowIndex++) {
if (maze2D[columnIndex][rowIndex] == "+") {
maze = maze + "+";
} else if (maze2D[columnIndex][rowIndex] == "-") {
maze = maze + "---";
} else if (maze2D[columnIndex][rowIndex] == "|") {
maze = maze + "|";
} else if (maze2D[columnIndex][rowIndex] == "#" && columnIndex % 2 == 1) {
// Hash symbol and column is odd
if (rowIndex % 2 == 0) {
maze = maze + " ";
} else if (rowIndex % 2 == 1) {
maze = maze + " ";
}
} else if (maze2D[columnIndex][rowIndex] == "#" && columnIndex % 2 == 0) {
// Hash symbol and column is even
maze = maze + " ";
} else if (maze2D[columnIndex][rowIndex] == "S" || maze2D[columnIndex][rowIndex] == "E") {
maze = maze + " ";
} else if (maze2D[columnIndex][rowIndex] == " " && columnIndex % 2 == 1 && rowIndex % 2 == 0) {
// Spacing for the wall
maze = maze + " ";
} else if (maze2D[columnIndex][rowIndex] == " ") {
// Spacing for the cell
maze = maze + " ";
} else {
maze = maze + " " + maze2D[columnIndex][rowIndex] + " ";
}
// When rowIndex is at end AND columnIndex is not at end, add a new line
if (rowIndex == (size - 1) && columnIndex != (size - 1)) {
maze = maze + System.lineSeparator();
}
}
}
return maze;
}
// Delete the Hash symbol in the maze
public static String[][] emptyHash(String[][] maze2D) {
int size = maze2D.length;
for (int columnIndex = 0; columnIndex < size; columnIndex++) {
for (int rowIndex = 0; rowIndex < size; rowIndex++) {
if (maze2D[columnIndex][rowIndex] == "#") {
maze2D[columnIndex][rowIndex] = " ";
}
}
}
return maze2D;
}
// Depth-first Search
public static String[][] DFS(String[][] maze2D) {
Stack<Cell> location = new Stack<Cell>();
int size = (maze2D.length - 1) / 2;
int totalCells = size * size;
int visitedCells = 1;
Cell current = new Cell(0, 0);
maze2D[1][1] = "0";
while (visitedCells < totalCells) {
// Generates a unique direction
ArrayList<String> direction = new ArrayList<>();
Collections.addAll(direction, "NORTH", "EAST", "SOUTH", "WEST");
Collections.shuffle(direction);
// Finds a valid spot on the 2D array
String random = DFSValid(maze2D, current, direction);
// System.out.println("The FINAL DIRECTION: " + random);
if (random == "BACKTRACK") {
// path.remove(0);
current = location.pop();
continue;
}
current = DFSMove(maze2D, current, random, visitedCells);
visitedCells = visitedCells + 1;
location.push(current);
if (current.getx() == size - 1 && current.gety() == size - 1) {
return maze2D;
}
}
return maze2D;
}
// Checks if DFS is valid
public static String DFSValid(String[][] maze2D, Cell current, ArrayList<String> direction) {
int size = (maze2D.length - 1) / 2;
int x = 2 * current.getx() + 1;
int y = 2 * current.gety() + 1;
// When the size of the list is 0, return "BACKTRACK"
if (direction.size() == 0) {
// maze2D[2 * current.gety() + 1][2 * current.getx() + 1] = " ";
return "BACKTRACK";
}
String random = direction.remove(0);
if (random == "NORTH") {
if (current.gety() - 1 < 0) {
// System.out.println("Do not go NORTH because outside of range of the 2D
// array");
return DFSValid(maze2D, current, direction);
}
if (maze2D[y - 1][x] != " ") {
// System.out.println("Do not go NORTH because there is a wall");
return DFSValid(maze2D, current, direction);
}
} else if (random == "EAST") {
if (current.getx() + 1 >= size) {
// System.out.println("Do not go EAST because outside of range of the 2D
// array");
return DFSValid(maze2D, current, direction);
}
if (maze2D[y][x + 1] != " ") {
// System.out.println("Do not go EAST because there is a wall");
return DFSValid(maze2D, current, direction);
}
} else if (random == "SOUTH") {
if (current.gety() + 1 >= size) {
// System.out.println("Do not go SOUTH because outside of range of the 2D
// array");
return DFSValid(maze2D, current, direction);
}
if (maze2D[y + 1][x] != " ") {
// System.out.println("Do not go SOUTH because there is a wall");
return DFSValid(maze2D, current, direction);
}
} else if (random == "WEST") {
if (current.getx() - 1 < 0) {
// System.out.println("Do not go WEST because outside of range of the 2D
// array");
return DFSValid(maze2D, current, direction);
}
if (maze2D[y][x - 1] != " ") {
// System.out.println("Do not go WEST because there is a wall");
return DFSValid(maze2D, current, direction);
}
}
return random;
}
// Move DFS location
public static Cell DFSMove(String[][] maze2D, Cell current, String random, int count) {
// Prints out the coordinates of the current cell object
// System.out.println(" X-coordinate: " + current.getx() + ", Y-coordinate: " +
// current.gety());
String path = Integer.toString(count % 10);
if (random == "NORTH") {
// NORTH and delete wall from bottom from next cell
current.setNext(new Cell(current.getx(), current.gety() - 1));
current = current.getNext();
// Breaks the bottom wall from next cell
maze2D[2 * current.gety() + 2][2 * current.getx() + 1] = "#";
// DEBUGGING: Visualizing
maze2D[2 * current.gety() + 1][2 * current.getx() + 1] = path;
} else if (random == "EAST") {
// EAST and delete wall from left from next cell
current.setNext(new Cell(current.getx() + 1, current.gety()));
current = current.getNext();
// Breaks the left wall from next cell
maze2D[2 * current.gety() + 1][2 * current.getx()] = "#";
// DEBUGGING: Visualizing
maze2D[2 * current.gety() + 1][2 * current.getx() + 1] = path;
} else if (random == "SOUTH") {
// SOUTH and delete wall from top from next cell
current.setNext(new Cell(current.getx(), current.gety() + 1));
current = current.getNext();
// Breaks the top wall from next cell
maze2D[2 * current.gety()][2 * current.getx() + 1] = "#";
// DEBUGGING: Visualizing
maze2D[2 * current.gety() + 1][2 * current.getx() + 1] = path;
} else if (random == "WEST") {
// WEST and delete wall from right from next cell
current.setNext(new Cell(current.getx() - 1, current.gety()));
current = current.getNext();
// Breaks the right wall from next cell
maze2D[2 * current.gety() + 1][2 * current.getx() + 2] = "#";
// DEBUGGING: Visualizing
maze2D[2 * current.gety() + 1][2 * current.getx() + 1] = path;
}
// DEBUGGING: Printing maze at each step
// System.out.println("NEW X-coordinate: " + current.getx() + ", NEW
// Y-coordinate: " + current.gety());
// print2D(maze2D);
// System.out.println();
return current;
}
// Breadth-first Search
public static String[][] BFS(String[][] maze2D) {
Queue<Cell> neighborQueue = new LinkedList<Cell>();
int size = (maze2D.length - 1) / 2;
int totalCells = size * size;
int visitedCells = 1;
Cell current = new Cell(0, 0);
neighborQueue.add(current);
maze2D[1][1] = "0";
while (visitedCells < totalCells) {
ArrayList<String> direction = new ArrayList<>();
direction = BFSValid(maze2D, current);
current = BFSMove(maze2D, current, neighborQueue, direction, visitedCells);
visitedCells = visitedCells + 1;
if (current.getx() == size - 1 && current.gety() == size - 1) {
return maze2D;
}
}
return maze2D;
}
// Checks if BFS is valid
public static ArrayList<String> BFSValid(String[][] maze2D, Cell current) {
int size = (maze2D.length - 1) / 2;
int x = 2 * current.getx() + 1;
int y = 2 * current.gety() + 1;
// Generates a unique direction
ArrayList<String> direction = new ArrayList<>();
Collections.addAll(direction, "NORTH", "EAST", "SOUTH", "WEST");
// System.out.println("BEGINNING: " + direction);
// Remove NORTH
if (current.gety() - 1 < 0) {
// System.out.println("Do not go NORTH because outside of range of the 2D
// array");
direction.remove("NORTH");
} else if (maze2D[y - 1][x] != " " || maze2D[y - 2][x] != " ") {
// System.out.println("Do not go NORTH because there is a wall");
direction.remove("NORTH");
}
// Remove EAST
if (current.getx() + 1 >= size) {
// System.out.println("Do not go EAST because outside of range of the 2D
// array");
direction.remove("EAST");
} else if (maze2D[y][x + 1] != " " || maze2D[y][x + 2] != " ") {
// System.out.println("Do not go EAST because there is a wall");
direction.remove("EAST");
}
// Remove SOUTH
if (current.gety() + 1 >= size) {
// System.out.println("Do not go SOUTH because outside of range of the 2D
// array");
direction.remove("SOUTH");
} else if (maze2D[y + 1][x] != " " || maze2D[y + 2][x] != " ") {
// System.out.println("Do not go SOUTH because there is a wall");
direction.remove("SOUTH");
}
// Remove WEST
if (current.getx() - 1 < 0) {
// System.out.println("Do not go WEST because outside of range of the 2D
// array");
direction.remove("WEST");
} else if (maze2D[y][x - 1] != " " || maze2D[y][x - 2] != " ") {
// System.out.println("Do not go WEST because there is a wall");
direction.remove("WEST");
}
Collections.shuffle(direction);
// System.out.println(direction);
return direction;
}
// Move BFS location
public static Cell BFSMove(String[][] maze2D, Cell current, Queue<Cell> neighborQueue, ArrayList<String> direction,
int count) {
String path = Integer.toString(count % 10);
while (direction.size() > 0) {
// System.out.println("Enters while loop");
String random = direction.remove(0);
if (random == "NORTH") {
// System.out.println("Removes NORTH");
neighborQueue.add(new Cell(current.getx(), current.gety() - 1));
} else if (random == "EAST") {
// System.out.println("Removes EAST");
neighborQueue.add(new Cell(current.getx() + 1, current.gety()));
} else if (random == "SOUTH") {
// System.out.println("Removes SOUTH");
neighborQueue.add(new Cell(current.getx(), current.gety() + 1));
} else if (random == "WEST") {
// System.out.println("Removes WEST");
neighborQueue.add(new Cell(current.getx() - 1, current.gety()));
}
}
// System.out.println("Elements: " + neighborQueue);
// System.out.println();
neighborQueue.remove();
current = neighborQueue.peek();
maze2D[2 * current.gety() + 1][2 * current.getx() + 1] = path;
return current;
}
// Creating the path
public static String[][] path(String[][] maze2D) {
int size = (maze2D.length - 1) / 2;
Cell current = new Cell(size, size);
int x = 2 * current.getx() + 1;
int y = 2 * current.gety() + 1;
int currentNumber = Integer.parseInt(maze2D[2 * size][2 * size]);
maze2D[2 * size][2 * size] = "#";
// Generates a unique direction
ArrayList<String> direction = new ArrayList<>();
Collections.addAll(direction, "NORTH", "EAST", "SOUTH", "WEST");
// Remove NORTH
if (current.gety() - 1 < 0) {
direction.remove("NORTH");
} else if (maze2D[y - 1][x] == "-") {
direction.remove("NORTH");
} else if (maze2D[y - 2][x] == " ") {
direction.remove("NORTH");
}
// Remove EAST
if (current.getx() + 1 >= size) {
direction.remove("EAST");
} else if (maze2D[y][x + 1] == "|") {
direction.remove("EAST");
} else if (maze2D[y][x + 2] == " ") {
direction.remove("EAST");
}
// Remove SOUTH
if (current.gety() + 1 >= size) {
direction.remove("SOUTH");
} else if (maze2D[y + 1][x] == "-") {
direction.remove("SOUTH");
} else if (maze2D[y + 2][x] == " ") {
direction.remove("SOUTH");
}
// Remove WEST
if (current.getx() - 1 < 0) {
direction.remove("WEST");
} else if (maze2D[y][x - 1] == "|") {
direction.remove("WEST");
} else if (maze2D[y][x - 1] == " ") {
direction.remove("WEST");
}
if (direction.size() == 2) {
if (direction.contains("NORTH")) {
int northNumber = Integer.parseInt(maze2D[y-2][x]);
Path north = new Path(northNumber, "NORTH");
}
if (direction.contains("EAST")) {
int eastNumber = Integer.parseInt(maze2D[y][x+2]);
Path east = new Path(eastNumber, "EAST");
}
if (direction.contains("SOUTH")) {
int southNumber = Integer.parseInt(maze2D[y+2][x]);
Path south = new Path(southNumber, "SOUTH");
}
if (direction.contains("WEST")) {
int westNumber = Integer.parseInt(maze2D[y][x-2]);
Path west = new Path(westNumber, "WEST");
}
}
return null;
}
}