-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPart2Maze.java
More file actions
1575 lines (1430 loc) · 52.4 KB
/
Part2Maze.java
File metadata and controls
1575 lines (1430 loc) · 52.4 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
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import javalib.impworld.World;
import javalib.impworld.WorldScene;
import javalib.worldimages.*;
import tester.Tester;
import java.awt.*;
import java.util.*;
/* MAZE NOTES PLEASE READ:
* How to:
* You can press 'r' to get a new maze either after you won
* or are bored of the current maze
* You can press 'd' to see the DFS, which is shown in pink
* - once it reaches the end it shows the traces back the correct
* path in dark blue
* You can press ' b' to see the BFS, yet again also shown in pink
* and traces back correct path
* You are able to manually traverse the maze using the arrow keys
* - your path is shown in light blue and your player is dark green
* - you want to reach the purple square at the end
* If you want to change the size of the maze just scroll all the way to
* the bottom, that's where the big bang is. More instructions there.
*
* Bells and Whistles:
* WHISTLES:
* - Allow the user the ability to start a new maze without
* restarting the program.
* BELLS:
* - Allow the user to traverse the maze manually - using the keys to
* select the next move, preventing illegal moves and notifying the
* user of completion of the game.
* - (Tricky) Construct mazes with a bias in a particular direction
* a preference for horizontal or vertical corridors.
* NOTE:(If you want to change the bias I made some examples by the bigbang,
* if you want to see them, simply uncomment them and plug in the Maze name
* infront of the bigbang)
*
* Side Note: We weren't able to figure out how to smoothly make the
* path so it is just squares, sorry
*
* Enjoy :)
*
*
*/
//represents the Vertex class
class Vertex {
int x;
int y;
//determines if the right should be made
boolean makeRight;
//determines if the down should be made
boolean makeBottom;
//determines if Vertex has been visited
boolean visited;
//the previous Vertex
Vertex previous;
Vertex left;
Vertex right;
Vertex top;
Vertex bottom;
ArrayList<Edge> arrayEdge = new ArrayList<Edge>();
Vertex(int x, int y) {
this.x = x;
this.y = y;
//this makes the maze edges
this.makeRight = true;
this.makeBottom = true;
}
//draws a right maze wall
WorldImage drawEdgeRight() {
return new LineImage(new Posn(0, Maze.cellSize), Color.black)
.movePinhole(- Maze.cellSize, Maze.cellSize / - 1.8);
}
//draws a bottom maze wall
WorldImage drawEdgeBottom() {
return new LineImage(new Posn(Maze.cellSize, 0), Color.black)
.movePinhole(Maze.cellSize / -1.8, - Maze.cellSize);
}
//draws the path rect
WorldImage draw(int wid, Color color) {
return new RectangleImage(Maze.cellSize - 3, Maze.cellSize - 3,
OutlineMode.SOLID, color).movePinhole(-wid * Maze.cellSize / wid / 2,
- wid * Maze.cellSize / wid / 2);
}
//finds the previous cell
void findPrevious() {
if (this.top != null && !this.top.makeBottom && this.top.previous
== null) {
this.previous = this.top;
}
else if (this.left != null && !this.left.makeRight && this.left.previous
== null) {
this.previous = this.left;
}
else if (this.bottom != null && !this.makeBottom && this.bottom.previous
== null) {
this.previous = this.bottom;
}
else if (this.right != null && !this.makeRight && this.right.previous
== null) {
this.previous = this.right;
}
}
//computes this x times cell Size
public int xVertTCell() {
return this.x * Maze.cellSize;
}
//computes this y times cell Size
public int yVertTCell() {
return this.y * Maze.cellSize;
}
//computes this x times cell Size
public int xPreviousTCell() {
return this.previous.xVertTCell();
}
//computes this y times cell Size
public int yPreviousTCell() {
return this.previous.yVertTCell();
}
//determines if this previous is not null
public boolean notNullPrev() {
return this.previous != null;
}
//determines if this to x == from x
public boolean xFromEqual(Vertex from) {
return this.x == from.x;
}
//EFFECT: sets this.makeBottom to false
public void bottomFalse() {
this.makeBottom = false;
}
//EFFECT: sets this.makeRight to false
public void rightFalse() {
this.makeRight = false;
}
//determines if this to y == from y
public boolean yFromEqual(Vertex from) {
return this.y == from.y;
}
//EFFECT: sets this right vertex to given inputs
public void setRight(ArrayList<ArrayList<Vertex>> vert2D,
int i, int j) {
this.right = vert2D.get(i).get(j + 1);
}
//EFFECT: sets this left vertex to given inputs
public void setLeft(ArrayList<ArrayList<Vertex>> vert2D,
int i, int j) {
this.left = vert2D.get(i).get(j - 1);
}
//EFFECT: sets this up vertex to given inputs
public void setUp(ArrayList<ArrayList<Vertex>> vert2D,
int i, int j) {
this.top = vert2D.get(i - 1).get(j);
}
//EFFECT: sets this bottom vertex to given inputs
public void setBottom(ArrayList<ArrayList<Vertex>> vert2D,
int i, int j) {
this.bottom = vert2D.get(i + 1).get(j);
}
//determines if this left is not null
public boolean leftNotNull() {
return this.left.notNullPrev();
}
//determines if this top is not null
public boolean topNotNull() {
return this.top.notNullPrev();
}
//EFFECT: sets this previous to this left
public void previousELeft() {
this.previous = this.left;
}
//EFFECT: sets this previous to this top
public void previousETop() {
this.previous = this.top;
}
//EFFECT: sets this previous to given Vertex
public void previousENext(Vertex next) {
this.previous = next;
}
//determines if this top is true
public boolean isTopBottom(boolean bol) {
return this.top.isBottom(bol);
}
//determines if this not makeBottom and bol is true
boolean isBottom(boolean bol) {
return !this.makeBottom && bol;
}
//determines if this left is true
public boolean isLeftMakeRight(boolean bol) {
return this.left.isRight(bol);
}
//determines if this not makeRight and bol is true
boolean isRight(boolean bol) {
return !this.makeRight && bol;
}
//EFFECT: Sets this visited to true
public void wasVisited() {
this.visited = true;
}
}
//represents the player class
class Player {
Vertex playerVert;
Player(Vertex playerVert) {
this.playerVert = playerVert;
}
// Checks if each key input results in a valid move
boolean isValid(String move) {
if (move.equals("up") && this.playerVert.top != null) {
return !this.playerVert.top.makeBottom;
}
else if (move.equals("down") && this.playerVert.bottom != null) {
return !this.playerVert.makeBottom;
}
else if (move.equals("left") && this.playerVert.left != null) {
return !this.playerVert.left.makeRight;
}
else if (move.equals("right") && this.playerVert.right != null) {
return !this.playerVert.makeRight;
}
else {
return false;
}
}
//draws the player
WorldImage drawPlayer() {
return new RectangleImage(Maze.cellSize - 3, Maze.cellSize - 3,
OutlineMode.SOLID, Color.GREEN.darker().darker())
.movePinhole(- Maze.cellSize / 1.8, - Maze.cellSize / 1.8);
}
//computes this players vert x times cellSize
public int playerXTCell() {
return this.playerVert.xVertTCell();
}
//computes this players vert y times cellSize
public int playerYTCell() {
return this.playerVert.yVertTCell();
}
//determines if this playerVert equals given vert
public boolean vertEBoard(Vertex vert) {
return this.playerVert == vert;
}
//EFFECT: Sets player vert visited to true
public void playerVertVisited() {
this.playerVert.wasVisited();
}
//EFFECT: sets this playerVert to right player Vert
public void playerVertRight() {
this.playerVert = this.playerVert.right;
}
//EFFECT: sets this playerVert to left player Vert
public void playerVertLeft() {
this.playerVert = this.playerVert.left;
}
//EFFECT: sets this playerVert to top player Vert
public void playerVertUp() {
this.playerVert = this.playerVert.top;
}
//EFFECT: sets this playerVert to bottom player Vert
public void playerVertBottom() {
this.playerVert = this.playerVert.bottom;
}
}
//represents the edge class
class Edge {
Vertex from;
Vertex to;
int weight;
Edge(Vertex from, Vertex to, int weight) {
this.from = from;
this.to = to;
this.weight = weight;
}
//determines if this to and this from x's are equal
public boolean isToFromX() {
return this.to.xFromEqual(this.from);
}
//determines if this to and this from y's are equal
public boolean isToFromY() {
return this.to.yFromEqual(this.from);
}
}
//represents the WeightComparator class that compares
// edge weight
class WeightComparator implements Comparator<Edge> {
//computes the edge weight difference
public int compare(Edge edge1, Edge edge2) {
return edge1.weight - edge2.weight;
}
}
//represents the maze world
class Maze extends World {
int width;
int height;
Player player;
Vertex endVert;
boolean isComplete;
Random rand = new Random();
HashMap<Vertex, Vertex> map = new HashMap<Vertex, Vertex>();
ArrayList<Edge> arrayEdge = new ArrayList<Edge>();
ArrayList<Edge> arrayEdge1 = new ArrayList<Edge>();
ArrayList<Vertex> mazePath = new ArrayList<Vertex>();
WorldScene mazeScene = new WorldScene(this.height * Maze.cellSize,
this.width * Maze.cellSize);
ArrayList<ArrayList<Vertex>> board;
int bias;
static int cellSize = 15;
Maze(int width, int height) {
this.width = width;
this.height = height;
this.bias = 100;
this.board = this.makeGrid(width, height);
this.createEdges(this.board);
this.createMap(board);
this.kruskals();
this.player = new Player(board.get(0).get(0));
this.endVert = this.board.get(height - 1).get(width - 1);
this.makeMaze();
this.isComplete = false;
}
//this is if you want to bias the maze
//ie more veritcal or horizontal
Maze(int width, int height, int bias) {
this.width = width;
this.height = height;
this.bias = bias;
this.board = this.makeGrid(width, height);
this.createEdges(this.board);
this.createMap(board);
this.kruskals();
this.player = new Player(board.get(0).get(0));
this.endVert = this.board.get(height - 1).get(width - 1);
this.makeMaze();
this.isComplete = false;
}
//makes the maze
WorldScene makeMaze() {
//draw ending rect
this.mazeScene.placeImageXY(board.get(this.height - 1).get(this.width - 1)
.draw(this.width, new Color(88, 24, 161)),
(width - 1) * cellSize, (height - 1) * cellSize);
//draws
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
Vertex boardGetIGetJ = board.get(i).get(j);
this.makeBottom(this.board.get(i).get(j));
this.makeRight(this.board.get(i).get(j));
if (boardGetIGetJ.visited) {
this.mazeScene.placeImageXY(board.get(i).get(j).draw(this.width,
new Color(89, 153, 255)), j * cellSize + 1,
i * cellSize + 1);
}
if (boardGetIGetJ.makeRight) {
this.mazeScene.placeImageXY(board.get(i).get(j).drawEdgeRight(),
(Maze.cellSize * j),
(Maze.cellSize * i));
}
if (boardGetIGetJ.makeBottom) {
this.mazeScene.placeImageXY(board.get(i).get(j).drawEdgeBottom(),
(Maze.cellSize * j),
(Maze.cellSize * i));
}
}
}
//draws the player(green rect)
this.mazeScene.placeImageXY(player.drawPlayer(), this.player.playerXTCell(),
this.player.playerYTCell());
return mazeScene;
}
//makes the games WorldScene
public WorldScene makeScene() {
TextImage winText = new TextImage("You Win! :)", this.height + 5, Color.BLACK);
if (mazePath.size() > 1) {
this.findEnd();
}
else if (mazePath.size() > 0) {
this.drawEnd();
}
else if (this.isComplete && this.endVert.notNullPrev()) {
this.traceBackPath();
}
//if player solves the maze
if (this.player.vertEBoard(this.board.get(height - 1).get(width - 1))) {
this.mazeScene.placeImageXY(winText, width * cellSize / 2,
height * cellSize / 2);
}
return mazeScene;
}
//EFFECT: updates the maze scene with the end scene
void drawEnd() {
Vertex next = mazePath.remove(0);
if (this.endVert.isLeftMakeRight(this.endVert.leftNotNull())) {
this.endVert.previousELeft();
}
else if (this.endVert.isTopBottom(this.endVert.topNotNull())) {
this.endVert.previousETop();
}
else {
this.endVert.previousENext(next);
}
this.isComplete = true;
}
// Changes if the right wall should be rendered for the given vertex
// Effect: Changes the makeRight field of the vertex
void makeRight(Vertex v) {
for (Edge edge : this.arrayEdge1) {
if (edge.isToFromY()) {
edge.from.rightFalse();
}
}
}
// Changes whether the bottom wall should be rendered for the given vertex
// Effect: Changes the makeRight field of the vertex
void makeBottom(Vertex v) {
for (Edge edge : this.arrayEdge1) {
if (edge.isToFromX()) {
edge.from.bottomFalse();
}
}
}
//creates the grid for each cell in the maze
ArrayList<ArrayList<Vertex>> makeGrid(int width, int height) {
ArrayList<ArrayList<Vertex>> board = new ArrayList<ArrayList<Vertex>>();
for (int i = 0; i < height; i++) {
board.add(new ArrayList<Vertex>());
ArrayList<Vertex> arrayVert = board.get(i);
for (int j = 0; j < width; j++) {
arrayVert.add(new Vertex(j, i));
}
}
this.connectVerts(board);
this.createEdges(board);
this.createMap(board);
return board;
}
//creates the grid for each cell in the maze, but with a string input
// this is for testing
ArrayList<ArrayList<Vertex>> makeGrid(int width, int height, String s) {
ArrayList<ArrayList<Vertex>> board = new ArrayList<ArrayList<Vertex>>();
for (int i = 0; i < height; i++) {
board.add(new ArrayList<Vertex>());
ArrayList<Vertex> vert = board.get(i);
for (int j = 0; j < width; j++) {
vert.add(new Vertex(j, i));
}
}
this.connectVerts(board);
return board;
}
//creates the arraylist of edges in the maze game
ArrayList<Edge> createEdges(ArrayList<ArrayList<Vertex>> vert2D) {
for (int i = 0; i < vert2D.size(); i++) {
for (int j = 0; j < vert2D.get(i).size(); j++) {
if (j < vert2D.get(i).size() - 1) {
Vertex getIJ = vert2D.get(i).get(j);
this.arrayEdge.add(new Edge(getIJ, getIJ.right,
this.rand.nextInt(this.bias)));
}
if (i < vert2D.size() - 1) {
Vertex getIJ = vert2D.get(i).get(j);
this.arrayEdge.add(new Edge(getIJ, getIJ.bottom,
this.rand.nextInt(100)));
}
}
}
Collections.sort(this.arrayEdge, new WeightComparator());
return this.arrayEdge;
}
// creates an initial hashmap where each node is linked to itself
// map = HashMap<Vertex, Vertex>();
HashMap<Vertex, Vertex> createMap(ArrayList<ArrayList<Vertex>> vertex) {
for (int i = 0; i < vertex.size(); i++) {
for (int j = 0; j < vertex.get(i).size(); j++) {
this.map.put(vertex.get(i).get(j), vertex.get(i).get(j));
}
}
return this.map;
}
//EFFECT: connects the verts in given vert list
void connectVerts(ArrayList<ArrayList<Vertex>> vert2D) {
for (int i = 0; i < this.height; i++) {
for (int j = 0; j < this.width; j++) {
if (j + 1 < this.width) {
vert2D.get(i).get(j).setRight(vert2D, i, j);
//right = vert2D.get(i).get(j + 1);
}
if (j - 1 >= 0) {
vert2D.get(i).get(j).setLeft(vert2D, i, j);
}
if (i + 1 < this.height) {
vert2D.get(i).get(j).setBottom(vert2D, i, j);
}
if (i - 1 >= 0) {
vert2D.get(i).get(j).setUp(vert2D, i, j);
}
}
}
}
//makes kruskal algorithm
ArrayList<Edge> kruskals() {
int n = 0;
while (this.arrayEdge1.size() < this.arrayEdge.size()
&& n < this.arrayEdge.size()) {
Edge e = arrayEdge.get(n);
if (this.find(this.find(e.from)).equals(this.find(this.find(e.to)))) {
// do nothing
}
else {
arrayEdge1.add(e);
switchVert(this.find(e.from), this.find(e.to));
}
n += 1;
}
// Adds all the arrayEdge for each vertex
//has to be in this method because it uses e (Edge)
for (int i = 0; i < this.height; i ++) {
for (int j = 0; j < this.width; j ++) {
for (Edge e : this.arrayEdge1) {
if (this.board.get(i).get(j).equals(e.from)
|| this.board.get(i).get(j).equals(e.to)) {
this.board.get(i).get(j).arrayEdge.add(e);
}
}
}
}
return this.arrayEdge1;
}
//EFFECT: changes the hashMap values with given Verts
void switchVert(Vertex vert1, Vertex vert2) {
this.map.put(this.find(vert1), this.find(vert2));
}
//finds the representative of this node
Vertex find(Vertex vert) {
if (vert.equals(this.map.get(vert))) {
return vert;
}
else {
return this.find(this.map.get(vert));
}
}
//EFFECT: changes the world based on key clicks
public void onKeyEvent(String ke) {
//this restarts the maze/ makes a new maze
if (ke.equals("r")) {
this.mazeScene = this.getEmptyScene();
this.board = this.makeGrid(width, height);
this.createEdges(this.board);
this.createMap(board);
this.kruskals();
this.player = new Player(board.get(0).get(0));
this.endVert = this.board.get(this.height - 1).get(this.width - 1);
this.makeMaze();
}
else if (ke.equals("right") && player.isValid("right")) {
player.playerVertVisited();
player.playerVertRight();
}
else if (ke.equals("left") && player.isValid("left")) {
player.playerVertVisited();
player.playerVertLeft();
}
else if (ke.equals("up") && player.isValid("up")) {
player.playerVertVisited();
player.playerVertUp();
}
else if (ke.equals("down") && player.isValid("down")) {
player.playerVertVisited();
player.playerVertBottom();
}
//this displays the DFS
else if (ke.equals("d")) {
this.endVert = this.board.get(this.height - 1).get(this.width - 1);
this.mazePath = new Paths().mazeDFS(this.board.get(0).get(0),
this.board.get(this.height - 1).get(this.width - 1));
}
//this displays the BFS
else if (ke.equals("b")) {
this.endVert = this.board.get(this.height - 1).get(this.width - 1);
this.mazePath = new Paths().mazeBFS(this.board.get(0).get(0),
this.board.get(this.height - 1).get(this.width - 1));
}
this.makeMaze();
}
//EFFECT: Updates the mazePath with pink squares
void findEnd() {
Vertex next = mazePath.remove(0);
this.mazeScene.placeImageXY(next.draw(this.width,
//draws the BFS and DFS in pink
new Color(255, 153, 255)), next.x * cellSize, next.y * cellSize);
}
//EFFECT: updates the maze mazeScene and draws back the correct mazePath
void traceBackPath() {
if (this.endVert.x == this.width - 1 && this.endVert.y == this.height - 1) {
this.mazeScene.placeImageXY(this.endVert.draw(this.width,
Color.BLUE.darker()), this.endVert.xVertTCell(),
this.endVert.yVertTCell());
}
this.mazeScene.placeImageXY(this.endVert.previous.draw(this.width,
Color.BLUE.darker()), this.endVert.xPreviousTCell(),
this.endVert.yPreviousTCell());
this.endVert = this.endVert.previous;
}
}
//represents the ICollection interface
interface ICollection<T> {
//EFFECT: adds the given item to this ICollection
void add(T item);
//removes a T from this ICollection
T remove();
//returns the size of this ICollection
int size();
}
//represents the Queue class
class Queue<T> implements ICollection<T> {
Deque<T> dequeT;
Queue() {
this.dequeT = new Deque<T>();
}
//EFFECT: adds the given item to this Queue
public void add(T item) {
this.dequeT.addAtTail(item);
}
//removes an item from this Queue
public T remove() {
return this.dequeT.removeFromHead();
}
//computes the size of this Queue
public int size() {
return this.dequeT.size();
}
}
//represents the Stack class
class Stack<T> implements ICollection<T> {
Deque<T> dequeT;
Stack() {
this.dequeT = new Deque<T>();
}
//EFFECT: adds the given item to a Stack
public void add(T item) {
this.dequeT.addAtHead(item);
}
//removes and item to a Stack
public T remove() {
return this.dequeT.removeFromHead();
}
//computes the size of this Stack
public int size() {
return this.dequeT.size();
}
}
//represents the paths class
class Paths {
ArrayList<Vertex> allVertices;
Paths() { }
//finds the BFS maze path
ArrayList<Vertex> mazeBFS(Vertex from, Vertex to) {
return this.findMazePath(from, to, new Queue<Vertex>());
}
//finds the DFS maze path
ArrayList<Vertex> mazeDFS(Vertex from, Vertex to) {
return this.findMazePath(from, to, new Stack<Vertex>());
}
//finds the mazePath using ICollection
ArrayList<Vertex> findMazePath(Vertex from, Vertex to,
ICollection<Vertex> worklist) {
ArrayList<Vertex> mazePath = new ArrayList<Vertex>();
worklist.add(from);
while (worklist.size() > 0) {
Vertex vert = worklist.remove();
if (vert == to) {
return mazePath;
}
else if (mazePath.contains(vert)) {
//do nothing
}
else {
for (Edge e : vert.arrayEdge) {
worklist.add(e.from);
worklist.add(e.to);
if (mazePath.contains(e.from)) {
vert.previous = e.from;
}
else if (mazePath.contains(e.to)) {
vert.previous = e.to;
}
}
mazePath.add(vert);
}
}
return mazePath;
}
}
//represents the examples class
class ExamplesMaze {
Maze mazeEx = new Maze(2, 3);
Maze mazeEx1 = new Maze(1, 1);
Paths paths = new Paths();
Paths paths1 = new Paths();
Vertex vert00 = new Vertex(0, 0);
Vertex vert01 = new Vertex(0, 1);
Vertex vert10 = new Vertex(1, 0);
Vertex vert11 = new Vertex(1, 1);
Vertex vert12 = new Vertex(1, 2);
Vertex vert02 = new Vertex(0, 2);
Edge edge1 = new Edge(vert00, vert10, 1);
Edge edge2 = new Edge(vert00, vert01, 2);
Edge edge3 = new Edge(vert10, vert11, 3);
Edge edge4 = new Edge(vert01, vert11, 4);
Edge edge5 = new Edge(vert01, vert02, 5);
Edge edge6 = new Edge(vert11, vert12, 6);
//initializes the maze examples
void initMaze() {
mazeEx.board = mazeEx.makeGrid(2, 3, "test");
mazeEx1.board = mazeEx1.makeGrid(1, 1, "test");
mazeEx.board.get(0).get(0).makeRight = false;
mazeEx.board.get(0).get(1).makeRight = true;
mazeEx.board.get(1).get(0).makeRight = false;
mazeEx.board.get(1).get(1).makeRight = false;
mazeEx.board.get(2).get(0).makeRight = false;
mazeEx.board.get(2).get(1).makeRight = false;
mazeEx.map.put(mazeEx.board.get(0).get(0), mazeEx.board.get(0).get(0));
mazeEx.map.put(mazeEx.board.get(0).get(1), mazeEx.board.get(0).get(1));
mazeEx.map.put(mazeEx.board.get(1).get(0), mazeEx.board.get(1).get(0));
mazeEx.map.put(mazeEx.board.get(1).get(1), mazeEx.board.get(1).get(1));
mazeEx.map.put(mazeEx.board.get(2).get(0), mazeEx.board.get(2).get(0));
mazeEx.map.put(mazeEx.board.get(2).get(1), mazeEx.board.get(2).get(1));
mazeEx.board.get(0).get(0).makeBottom = false;
mazeEx.board.get(0).get(1).makeBottom = false;
mazeEx.board.get(1).get(0).makeBottom = false;
mazeEx.board.get(1).get(1).makeBottom = false;
mazeEx.board.get(2).get(0).makeBottom = true;
mazeEx.board.get(2).get(1).makeBottom = true;
mazeEx.arrayEdge = new ArrayList<Edge>(Arrays.asList(
edge1, edge2, edge3, edge4, edge5, edge6,
new Edge(vert02, vert12, 7)));
mazeEx.arrayEdge1 = new ArrayList<Edge>(Arrays.asList(
edge1, edge2, edge3, edge5, edge6));
mazeEx.player = new Player(mazeEx.board.get(0).get(0));
mazeEx.isComplete = false;
mazeEx.mazePath = new ArrayList<Vertex>();
mazeEx.endVert = mazeEx.board.get(2).get(1);
}
//tests the makeGrid method
void testMakeGrid(Tester t) {
this.initMaze();
t.checkExpect(mazeEx.board, new ArrayList<ArrayList<Vertex>>(Arrays.asList(
new ArrayList<Vertex>(Arrays.asList(mazeEx.board.get(0)
.get(0), mazeEx.board.get(0).get(1))),
new ArrayList<Vertex>(Arrays.asList(mazeEx.board.get(1)
.get(0), mazeEx.board.get(1).get(1))),
new ArrayList<Vertex>(Arrays.asList(mazeEx.board.get(2)
.get(0), mazeEx.board.get(2).get(1))))));
}
//tests connectVerts method
void testConnectVerts(Tester t) {
this.initMaze();
t.checkExpect(mazeEx.board.get(0).get(0).right, mazeEx.board.get(0).get(1));
t.checkExpect(mazeEx.board.get(0).get(0).bottom, mazeEx.board.get(1).get(0));
t.checkExpect(mazeEx.board.get(0).get(0).top, null);
t.checkExpect(mazeEx.board.get(0).get(0).left, null);
}
//tests the createMap method
void testCreateMap(Tester t) {
this.initMaze();
t.checkExpect(mazeEx.map.get(mazeEx.board.get(0).get(0)),
mazeEx.board.get(0).get(0));
t.checkExpect(mazeEx.map.get(mazeEx.board.get(0).get(1)),
mazeEx.board.get(0).get(1));
t.checkExpect(mazeEx.map.get(mazeEx.board.get(1).get(0)),
mazeEx.board.get(1).get(0));
t.checkExpect(mazeEx.map.get(mazeEx.board.get(1).get(1)),
mazeEx.board.get(1).get(1));
t.checkExpect(mazeEx.map.get(mazeEx.board.get(2).get(0)),
mazeEx.board.get(2).get(0));
t.checkExpect(mazeEx.map.get(mazeEx.board.get(2).get(1)),
mazeEx.board.get(2).get(1));
}
//tests the kruskals method
void testKruskals(Tester t) {
this.initMaze();
mazeEx.makeGrid(mazeEx.width, mazeEx.height);
t.checkExpect(mazeEx.arrayEdge1.get(0), new Edge(mazeEx.arrayEdge1
.get(0).from, mazeEx.arrayEdge1.get(0).to, 1));
t.checkExpect(mazeEx.arrayEdge1.get(1), new Edge(mazeEx.arrayEdge1
.get(1).from, mazeEx.arrayEdge1.get(1).to, 2));
t.checkExpect(mazeEx.arrayEdge1.get(2), new Edge(mazeEx.arrayEdge1
.get(2).from, mazeEx.arrayEdge1.get(2).to, 3));
t.checkExpect(mazeEx.arrayEdge1.get(3), new Edge(mazeEx.arrayEdge1
.get(3).from, mazeEx.arrayEdge1.get(3).to, 5));
t.checkExpect(mazeEx.arrayEdge1.get(4), new Edge(mazeEx.arrayEdge1
.get(4).from, mazeEx.arrayEdge1.get(4).to, 6));
}
//tests switchVert method
void testswitchVert(Tester t) {
this.initMaze();
mazeEx.switchVert(mazeEx.board.get(0).get(0), mazeEx.board.get(0).get(1));
t.checkExpect(mazeEx.find(mazeEx.board.get(0).get(0)), mazeEx.board.get(0).get(1));
mazeEx.switchVert(mazeEx.board.get(0).get(1), mazeEx.board.get(1).get(1));
t.checkExpect(mazeEx.find(mazeEx.board.get(0).get(1)), mazeEx.board.get(1).get(1));
mazeEx.switchVert(mazeEx.board.get(2).get(0), mazeEx.board.get(0).get(1));
t.checkExpect(mazeEx.find(mazeEx.board.get(0).get(0)), mazeEx.board.get(1).get(1));
}
//tests the find method
void testFind(Tester t) {
this.initMaze();
t.checkExpect(mazeEx.find(mazeEx.board.get(0).get(0)),
mazeEx.board.get(0).get(0));
t.checkExpect(mazeEx.find(mazeEx.board.get(1).get(0)),
mazeEx.board.get(1).get(0));
t.checkExpect(mazeEx.find(mazeEx.board.get(0).get(1)),
mazeEx.board.get(0).get(1));
t.checkExpect(mazeEx.find(mazeEx.board.get(2).get(0)),
mazeEx.board.get(2).get(0));
}
//tests the onKeyEvent method
void testOnKeyEvent(Tester t) {
this.initMaze();
mazeEx.onKeyEvent("right");
t.checkExpect(mazeEx.player.playerVert, mazeEx.board.get(0).get(1));
mazeEx.onKeyEvent("down");
t.checkExpect(mazeEx.player.playerVert, mazeEx.board.get(1).get(1));
mazeEx.onKeyEvent("up");
t.checkExpect(mazeEx.player.playerVert, mazeEx.board.get(0).get(1));
mazeEx.onKeyEvent("left");
t.checkExpect(mazeEx.player.playerVert, mazeEx.board.get(0).get(0));
mazeEx.onKeyEvent("d");
t.checkExpect(mazeEx.mazePath,
new ArrayList<Vertex>(Arrays.asList(mazeEx.board.get(0).get(0))));
mazeEx.onKeyEvent("b");
t.checkExpect(mazeEx.mazePath,
new ArrayList<Vertex>(Arrays.asList(mazeEx.board.get(0).get(0))));
//unable to test "r" because it gives a null pointer
//mazeEx.onKeyEvent("r");
}
//tests isValid method
void testIsValid(Tester t) {
this.initMaze();
t.checkExpect(mazeEx.player.isValid("right"), true);
t.checkExpect(mazeEx.player.isValid("left"), false);
t.checkExpect(mazeEx.player.isValid("up"), false);
t.checkExpect(mazeEx.player.isValid("down"), true);
t.checkExpect(mazeEx.player.isValid("d"), false);
t.checkExpect(mazeEx.player.isValid("b"), false);
t.checkExpect(mazeEx.player.isValid("r"), false);
}
//tests the makeRight method
void testmakeRight(Tester t) {
this.initMaze();
mazeEx.makeRight(mazeEx.board.get(0).get(0));
t.checkExpect(mazeEx.board.get(0).get(0).makeRight, false);
mazeEx.makeRight(mazeEx.board.get(0).get(1));
t.checkExpect(mazeEx.board.get(0).get(1).makeRight, true);
mazeEx.makeRight(mazeEx.board.get(1).get(0));
t.checkExpect(mazeEx.board.get(1).get(0).makeRight, false);
mazeEx.makeRight(mazeEx.board.get(1).get(1));
t.checkExpect(mazeEx.board.get(1).get(1).makeRight, false);
mazeEx.makeRight(mazeEx.board.get(2).get(0));
t.checkExpect(mazeEx.board.get(2).get(0).makeRight, false);
}
//tests the makeBottom method
void testmakeBottom(Tester t) {
this.initMaze();
mazeEx.makeBottom(mazeEx.board.get(0).get(0));
t.checkExpect(mazeEx.board.get(0).get(0).makeBottom, false);
mazeEx.makeBottom(mazeEx.board.get(0).get(1));
t.checkExpect(mazeEx.board.get(0).get(1).makeBottom, false);
mazeEx.makeBottom(mazeEx.board.get(2).get(0));
t.checkExpect(mazeEx.board.get(2).get(0).makeBottom, true);
mazeEx.makeBottom(mazeEx.board.get(1).get(0));
t.checkExpect(mazeEx.board.get(1).get(0).makeBottom, false);
mazeEx.makeBottom(mazeEx.board.get(1).get(1));
t.checkExpect(mazeEx.board.get(1).get(1).makeBottom, false);
mazeEx.makeBottom(mazeEx.board.get(2).get(1));
t.checkExpect(mazeEx.board.get(2).get(1).makeBottom, true);
}
//tests the add method for Queue
void testAdd(Tester t) {
Queue<Vertex> queueVert = new Queue<Vertex>();
t.checkExpect(queueVert.size(), 0);
queueVert.add(new Vertex(0, 0));
t.checkExpect(queueVert.size(), 1);
queueVert.add(new Vertex(0, 0));
t.checkExpect(queueVert.size(), 2);
queueVert.add(new Vertex(0, 1));
t.checkExpect(queueVert.size(), 3);
}
//tests the size method
void testSize(Tester t) {
Queue<Vertex> queueVert = new Queue<Vertex>();
Stack<Vertex> stackVert = new Stack<Vertex>();
t.checkExpect(queueVert.size(), 0);
queueVert.add(new Vertex(0, 0));
t.checkExpect(queueVert.size(), 1);
queueVert.add(new Vertex(1, 0));
t.checkExpect(queueVert.size(), 2);
queueVert.add(new Vertex(0, 1));
t.checkExpect(queueVert.size(), 3);
t.checkExpect(stackVert.size(), 0);
stackVert.add(new Vertex(1, 0));