-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMazeGenerator.java
More file actions
940 lines (822 loc) · 29 KB
/
MazeGenerator.java
File metadata and controls
940 lines (822 loc) · 29 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
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
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 {
/**
* Constructor for JUnit
*/
public MazeGenerator(int size) {
System.out.println("-------------------------------------------------------------------");
System.out.println();
// Constructs a new 2D array
String[][] maze2D = maze2D(size);
// Prints out new maze as 2D array
String[][] mazeGenerated = generator(maze2D);
// Prints the string representation of maze
System.out.println(convert2D(mazeGenerated));
System.out.println("String Representation of Generated " + size + "x" + size + " Maze");
// Delete the Hash symbol in the maze
mazeGenerated = emptyHash(mazeGenerated);
// Depth First Search
String[][] mazeDFS = DFS(clone(mazeGenerated));
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));
System.out.println(convert2D(mazeBFS));
System.out.println("String representation of BFS Maze");
System.out.println();
// Creates an single path of the maze
String[][] mazePath = backtrackingDelete(clone(mazeGenerated));
emptyHash(mazePath);
hashList(mazePath);
// Prints the string representation of maze with path
System.out.println(printPath(mazePath));
System.out.println("Hash Single Path");
}
/**
* 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 an empty non-generated maze
*
* @param size The size of the maze
* @return maze2D The empty non-generated maze
*/
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;
}
/**
* Creates a valid generated maze that has a path from the begining to end
*
* @param maze2D The empty non-generated maze
* @return maze2D The empty generated 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;
}
/**
* The valid spot returns all the valid spot given a cell location
*
* @param maze2D The maze from maze2D method
* @param current The current located cell
* @param direction The list of directions
* @return random The valid random direction
*/
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;
}
/**
* Move the next cell and break the wall in between
*
* @param maze2D The maze from the maze2D
* @param current
* The current located cell
* @param random
* The valid random direction
* @return current The new current cell
*/
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;
}
/**
* Returns the total amount of visited cells
*
* @param size The size of 2D array
* @return visitedCell The total amount
*/
public int visitedCells(int size) {
return size*size;
}
/**
* Converts 2D array maze to the string representation
*
* @param maze2D The maze that will be convert to string representation
* @return maze The string representation of the maze
*/
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 all the hash symbols in the maze
*
* @param maze2D The maze with hash symbols
* @return maze2D The maze with deleted hash symbols
*/
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 (DFS)
*
* @param maze2D
* The empty generated maze
* @return maze2D The DFS generated maze
*/
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") {
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 direction is valid in DFS
*
* @param maze2D
* The maze from DFS method
* @param current
* The current located cell
* @param direction
* The list of directions
* @return random The valid random direction
*/
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 to the direction given in DFS
*
* @param maze2D
* The maze from DFS method
* @param current
* The current located cell
* @param random
* The valid random direction
* @param count
* The number presented in each cell
* @return current The new current cell
*/
public static Cell DFSMove(String[][] maze2D, Cell current, String random, int count) {
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 (BFS)
*
* @param maze2D
* The empty generated maze
* @return maze2D The BFS generated maze
*/
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 direction is valid in BFS
*
* @param maze2D
* The maze from DFS method
* @param current
* The current located cell
* @return direction The valid random direction
*/
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");
// Removes 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");
}
// Removes 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");
}
// Removes 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");
}
// Removes 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);
return direction;
}
/**
* Move to the direction given in BFS
*
* @param maze2D
* The maze from BFS method
* @param current
* The current located cell
* @param neighborQueue
* The queue for all the neighbors
* @param direction
* The valid random direction
* @param count
* The number presented in each cell
* @return current The new current cell
*/
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;
}
/**
* Deletes the numbers from the bracktracking
*
* @param maze2D
* The DFS or BFS maze
* @return maze2D The maze with a single path
*/
public static String[][] backtrackingDelete(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") {
maze2D[2 * current.gety() + 1][2 * current.getx() + 1] = " ";
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;
}
/**
* Delete every other number not on path and convert path to hash
*
* @param maze2D
* The maze2D from after process through backtrackingDelete method
* @return path The list the cell locations
*/
public static ArrayList<Cell> hashList(String[][] maze2D) {
int size = (maze2D.length - 1) / 2;
ArrayList<Cell> path = new ArrayList<>();
Cell current = new Cell(0, 0);
path.add(current);
while (current.getx() != size - 1 || current.gety() != size - 1) {
// NORTH
if (current.gety() - 1 < 0) {
// Do Nothing
} else if (maze2D[2 * current.gety()][2 * current.getx() + 1] == " "
&& maze2D[2 * current.gety() - 1][2 * current.getx() + 1] != " "
&& maze2D[2 * current.gety() - 1][2 * current.getx() + 1] != "#") {
path.add(new Cell(current.getx(), current.gety() - 1));
current.setNext(new Cell(current.getx(), current.gety() - 1));
}
// EAST
if (current.getx() + 1 >= size) {
// Do Nothing
} else if (maze2D[2 * current.gety() + 1][2 * current.getx() + 2] == " "
&& maze2D[2 * current.gety() + 1][2 * current.getx() + 3] != " "
&& maze2D[2 * current.gety() + 1][2 * current.getx() + 3] != "#") {
path.add(new Cell(current.getx() + 1, current.gety()));
current.setNext(new Cell(current.getx() + 1, current.gety()));
}
// SOUTH
if (current.gety() + 1 >= size) {
// Do Nothing
} else if (maze2D[2 * current.gety() + 2][2 * current.getx() + 1] == " "
&& maze2D[2 * current.gety() + 3][2 * current.getx() + 1] != " "
&& maze2D[2 * current.gety() + 3][2 * current.getx() + 1] != "#") {
path.add(new Cell(current.getx(), current.gety() + 1));
current.setNext(new Cell(current.getx(), current.gety() + 1));
}
// WEST
if (current.getx() - 1 < 0) {
// Do Nothing
} else if (maze2D[2 * current.gety() + 1][2 * current.getx()] == " "
&& maze2D[2 * current.gety() + 1][2 * current.getx() - 1] != " "
&& maze2D[2 * current.gety() + 1][2 * current.getx() - 1] != "#") {
path.add(new Cell(current.getx() - 1, current.gety()));
current.setNext(new Cell(current.getx() - 1, current.gety()));
}
maze2D[2 * current.gety() + 1][2 * current.getx() + 1] = "#";
current = current.getNext();
}
maze2D[2 * current.gety() + 1][2 * current.getx() + 1] = "#";
// Deletes all the extra numbers
for (int columnIndex = 0; columnIndex < maze2D.length; columnIndex++) {
for (int rowIndex = 0; rowIndex < maze2D.length; rowIndex++) {
if (!(maze2D[columnIndex][rowIndex] == "+" || maze2D[columnIndex][rowIndex] == "-"
|| maze2D[columnIndex][rowIndex] == "|" || maze2D[columnIndex][rowIndex] == "#")) {
maze2D[columnIndex][rowIndex] = " ";
}
}
}
return path;
}
/**
* Special printing method for hash path
*
* @param maze2D
* The maze2D from after process through hashList method
* @return maze The string representation of the maze
*/
public static String printPath(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;
}
/**
* Main method for running the program and displaying to the console
*/
public static void main(String[] args) {
// Repeat Program
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()) {
// Repeat message when bad input
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);
// Prints out new maze as 2D array
String[][] mazeGenerated = generator(maze2D);
// Prints the string representation of maze
System.out.println(convert2D(mazeGenerated));
System.out.println("String Representation of Generated " + size + "x" + size + " Maze");
// Delete the Hash symbol in the maze
mazeGenerated = emptyHash(mazeGenerated);
// Depth First Search
String[][] mazeDFS = DFS(clone(mazeGenerated));
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));
System.out.println(convert2D(mazeBFS));
System.out.println("String representation of BFS Maze");
System.out.println();
// Creates an single path of the maze
String[][] mazePath = backtrackingDelete(clone(mazeGenerated));
emptyHash(mazePath);
hashList(mazePath);
// Prints the string representation of maze with path
System.out.println(printPath(mazePath));
System.out.println("Hash Single Path");
}
}
}