-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMazeGame.java
2402 lines (1990 loc) · 70.8 KB
/
MazeGame.java
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 java.awt.Color;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Random;
import javalib.impworld.World;
import javalib.worldimages.*;
import tester.*;
// Assignment 10
// Dhesikan Anish
// anishd
// McDonough Kevin
// kmacdoug
// NOTE: for Kruskal's algorithm, since the union-find structure is operating
// on a very specific subset of Posns (first quadrant rectangle cornered
// at the origin), we decided to use a 2 dimensional ArrayList instead of
// a HashMap as our underlying data structure for union-find
// to represent a stack of Ts
class Stack<T> {
// stores all items in the stack, with the last item being the top
ArrayList<T> stack;
Stack() {
this.stack = new ArrayList<T>();
}
// EFFECT: add an element to the end of stack
void push(T item) {
stack.add(item);
}
// EFFECT: remove the last element of stack, and return it
T pop() {
return this.stack.remove(stack.size() - 1);
}
// the size of this stack
int size() {
return stack.size();
}
// is this stack empty?
boolean isEmpty() {
return this.size() == 0;
}
}
// to represent a queue of Ts
class Queue<T> {
// location of first item in queue
int head;
// stores all items in the queue with given head and last item at the end
ArrayList<T> queue;
Queue() {
this.head = 0;
this.queue = new ArrayList<T>();
}
// EFFECT: adds given item to the end of the queue
void enqueue(T item) {
this.queue.add(item);
}
// EFFECT: gets item at head, increments head, then returns item
T dequeue() {
T item = this.queue.get(head);
head += 1;
// if the extra space at the front is too wasteful, reset
if (head > 20 && this.head > this.queue.size() / 3) {
this.reset();
}
return item;
}
// the size of this queue
int size() {
return this.queue.size() - this.head;
}
// is this queue empty?
boolean isEmpty() {
return this.size() == 0;
}
// EFFECT: creates a new ArrayList with all items in queue and head = 0
void reset() {
ArrayList<T> newQueue = new ArrayList<T>(this.size());
// add all items to the new list
while (!this.isEmpty()) {
// dequeue code without if block
newQueue.add(this.queue.get(head));
head += 1;
}
this.queue = newQueue;
this.head = 0;
}
}
// to represent a square in the maze game
class Cell {
// position in grid coordinates
int x;
int y;
// edges of the cell
Edge left;
Edge top;
Edge right;
Edge bot;
// has this cell been traversed?
boolean traversed;
// is this cell on the direct path to the exit?
boolean onPath;
// Default constructor
Cell(int x, int y, Edge left, Edge top, Edge right, Edge bot,
boolean traversed, boolean onPath) {
this.x = x;
this.y = y;
this.left = left;
this.top = top;
this.right = right;
this.bot = bot;
this.traversed = traversed;
this.onPath = onPath;
}
// Initializing constructor
Cell(int x, int y) {
this.x = x;
this.y = y;
this.left = null;
this.top = null;
this.right = null;
this.bot = null;
this.traversed = false;
}
// draws this cell, along with its bottom and right edges
WorldImage draw(int cellSize) {
Posn center = new Posn(this.x * cellSize + cellSize / 2,
this.y * cellSize + cellSize / 2);
WorldImage cellImage =
new RectangleImage(center, cellSize, cellSize, this.getColor());
WorldImage leftEdgeImage = this.left.draw(cellSize);
WorldImage topEdgeImage = this.top.draw(cellSize);
WorldImage rightEdgeImage = this.right.draw(cellSize);
WorldImage botEdgeImage = this.bot.draw(cellSize);
return cellImage.overlayImages(
leftEdgeImage, topEdgeImage, rightEdgeImage, botEdgeImage);
}
// get the color of this cell
Color getColor() {
if (this.onPath) {
return new Color(250, 70, 70);
}
else if (this.traversed) {
return new Color(150, 150, 220);
}
else {
// Hue 282
return new Color(203, 188, 227);
}
}
}
// to represent a wall between two cells
abstract class Edge {
// default color of edges
static final Color COLOR = new Color(73, 46, 116);
// the two cells this edge connects to; order is important but depends
// on orientation
Cell cell1;
Cell cell2;
// is this edge blocking movement?
boolean isBlocking;
// traversal weight of this edge
double weight;
// Create an edge connecting given cells with given attributes
Edge(Cell cell1, Cell cell2, boolean isBlocking, double weight) {
this.cell1 = cell1;
this.cell2 = cell2;
this.isBlocking = isBlocking;
this.weight = weight;
}
// create a picture of this edge if on screen
WorldImage draw(int cellSize) {
// non-existant walls draw off screen (for lack of dummy image type)
if (!isBlocking) {
return new RectangleImage(new Posn(-1, -1), 0, 0, Color.BLACK);
}
return this.getImage(cellSize);
}
// gets the image of this edge regardless of isBlocking
abstract WorldImage getImage(int cellSize);
// get the color of this edge
Color getColor() {
// Hue 282
return Edge.COLOR;
}
}
// to represent an edge that connects left and right cells
class LREdge extends Edge {
LREdge(Cell left, Cell right, boolean isBlocking, double weight) {
super(left, right, isBlocking, weight);
}
// returns the image of this edge
WorldImage getImage(int cellSize) {
int rightCellX = this.cell2.x;
int rightCellY = this.cell2.y;
Posn center = new Posn(rightCellX * cellSize,
rightCellY * cellSize + cellSize / 2);
return new RectangleImage(
center, cellSize / 10 + 1, cellSize, this.getColor());
}
}
// to represent an edge that connects top and bottom cells
class TBEdge extends Edge {
TBEdge(Cell top, Cell bot, boolean isBlocking, double weight) {
super(top, bot, isBlocking, weight);
}
// returns the image of this edge
WorldImage getImage(int cellSize) {
// coordinates of the lower cell in grid coordinates
int botCellX = this.cell2.x;
int botCellY = this.cell2.y ;
// graphical center of edge
Posn center = new Posn(botCellX * cellSize + cellSize / 2,
botCellY * cellSize);
return new RectangleImage(
center, cellSize, cellSize / 10 + 1, this.getColor());
}
}
// to represent an edge that is on the border of the maze
class BorderEdge extends Edge {
// Default Constructor
BorderEdge(Cell cell) {
super(cell, cell, true, 0);
}
// returns the image of this edge
WorldImage getImage(int cellSize) {
// an image off the canvas for lack of a dummy Image
return new RectangleImage(new Posn(-1, -1), 0, 0, Color.GRAY);
}
}
// to compare edges by weight
class EdgeWeightComp implements Comparator<Edge> {
// compare the given edges
public int compare(Edge edge1, Edge edge2) {
return Double.compare(edge1.weight, edge2.weight);
}
}
// to represent a maze board
class Maze {
// width and height of Maze in cells;
int width;
int height;
// all the cells of the board represented as a list of columns of cells
ArrayList<ArrayList<Cell>> cells;
// a list of all non-border edges in the game
// INV: sorted by weight
ArrayList<Edge> edges;
// random instance to be used throughout maze generation
Random rand;
// Constructor creating a maze with given width and height (in cells)
Maze(int width, int height) {
this.rand = new Random();
initializeBoard(width, height);
}
///////////////////////////////////////////////////////////////////////////
// Initialization functions
// EFFECT: modify cells as a board of given width and height (in cells)
// with all walls present
void initializeBoard(int width, int height) {
this.width = width;
this.height = height;
this.constructCells(width, height);
this.connectCells();
}
// EFFECT: modify cells as a matrix of cells that aren't connected
void constructCells(int width, int height) {
cells = new ArrayList<ArrayList<Cell>>(width);
for (int x = 0; x < width; x += 1) {
cells.add(new ArrayList<Cell>(height));
for (int y = 0; y < height; y += 1) {
cells.get(x).add(new Cell(x, y));
}
}
}
// EFFECT: modify cells' edges to connect cells
void connectCells() {
this.edges = new ArrayList<Edge>();
this.connectCellsHorizontal();
this.connectCellsVertical();
}
// EFFECT: modify cells' left and right edges
void connectCellsHorizontal() {
for (int x = 0; x < cells.size() - 1; x += 1) {
for (int y = 0; y < cells.get(x).size(); y += 1) {
Cell curCell = cells.get(x).get(y);
Cell rightCell = cells.get(x + 1).get(y);
Edge edge = new LREdge(curCell, rightCell, true, 1);
curCell.right = edge;
rightCell.left = edge;
this.edges.add(edge);
}
}
for (int y = 0; y < cells.get(0).size(); y += 1) {
Cell leftmostCell = cells.get(0).get(y);
leftmostCell.left = new BorderEdge(leftmostCell);
Cell rightmostCell = cells.get(cells.size() - 1).get(y);
rightmostCell.right = new BorderEdge(rightmostCell);
}
}
// EFFECT: modify cells' top and bottom edges
void connectCellsVertical() {
for (int x = 0; x < cells.size(); x += 1) {
for (int y = 0; y < cells.get(x).size() - 1; y += 1) {
Cell curCell = cells.get(x).get(y);
Cell downCell = cells.get(x).get(y + 1);
Edge edge = new TBEdge(curCell, downCell, true, 1);
curCell.bot = edge;
downCell.top = edge;
this.edges.add(edge);
}
}
for (int x = 0; x < cells.size(); x += 1) {
Cell topmostCell = cells.get(x).get(0);
topmostCell.top = new BorderEdge(topmostCell);
Cell bottommostCell = cells.get(x).get(cells.get(x).size() - 1);
bottommostCell.bot = new BorderEdge(bottommostCell);
}
}
///////////////////////////////////////////////////////////////////////////
// Drawing functions
// draws this maze onto the given background image
WorldImage drawOnto(int cellSize, WorldImage bg) {
WorldImage mazeImage =
this.drawSection(cellSize, 0, 0,
this.width, this.height);
// width and height of maze in pixels
int pWidth = this.width * cellSize;
int pHeight = this.height * cellSize;
///////////////////////////////////////////////////////////////////////
// Draw Border
// positions of the centers of the sides
Posn topC = new Posn(pWidth / 2, 0);
Posn botC = new Posn(pWidth / 2, pHeight);
Posn leftC = new Posn(0, pHeight / 2);
Posn rightC = new Posn(pWidth, pHeight / 2);
int borderW = cellSize / 10 + 1;
WorldImage top =
new RectangleImage(topC, pWidth, borderW, Edge.COLOR);
WorldImage bot =
new RectangleImage(botC, pWidth, borderW, Edge.COLOR);
WorldImage left =
new RectangleImage(leftC, borderW, pWidth, Edge.COLOR);
WorldImage right =
new RectangleImage(rightC, borderW, pWidth, Edge.COLOR);
WorldImage border = top.overlayImages(bot, left, right);
return mazeImage.overlayImages(border);
}
// draws a section of cells within the given bounds
// minX and minY are inclusive, maxX and maxY are exclusive
WorldImage drawSection(int cellSize,
int minX, int minY, int maxX, int maxY) {
int width = maxX - minX; // in cells
int height = maxY - minY; // in cells
// should never be tasked with drawing empty sections
if (width <= 0 || height <= 0) {
throw new RuntimeException("Drawing Empty Section");
}
// base case: draw 1 cell
else if (width == 1 && height == 1) {
return this.cells.get(minX).get(minY).draw(cellSize);
}
// halve the drawing vertically
else if (width == 1) {
// CONTEXT: height is greater than 1
int midY = (minY + maxY) / 2;
WorldImage topHalf =
this.drawSection(cellSize, minX, minY, maxX, midY);
WorldImage botHalf =
this.drawSection(cellSize, minX, midY, maxX, maxY);
return topHalf.overlayImages(botHalf);
}
// halve the drawing horizontally
else {
int midX = (minX + maxX) / 2;
WorldImage leftHalf =
this.drawSection(cellSize, minX, minY, midX, maxY);
WorldImage rightHalf =
this.drawSection(cellSize, midX, minY, maxX, maxY);
return leftHalf.overlayImages(rightHalf);
}
}
///////////////////////////////////////////////////////////////////////////
// Maze creation functions
// EFFECT: modifies edges to put all walls in maze back up
void wallsUp() {
for (Edge edge: edges) {
edge.isBlocking = true;
}
}
// EFFECT: modifies the weights of the edges to give them random weights
void assignRandomWeights() {
for (Edge edge: edges) {
edge.weight = rand.nextDouble();
}
Collections.sort(edges, new EdgeWeightComp());
}
// the bottom-right cell of this maze
Cell getFinalCell() {
ArrayList<Cell> lastCol = this.cells.get(this.cells.size() - 1);
return lastCol.get(lastCol.size() - 1);
}
// the top-left cell of this maze
Cell getFirstCell() {
return this.cells.get(0).get(0);
}
// EFFECT: modifies cells to resets every cell in this maze to be marked
// as not traversed and not on path
void resetTraversals() {
for (ArrayList<Cell> col: cells) {
for (Cell cell: col) {
cell.traversed = false;
cell.onPath = false;
}
}
}
}
// a union-find structure for creating groups of Posns among a rectangular,
// strictly positive set of Posns cornered at the origin
class UnionFindPosn {
ArrayList<ArrayList<Posn>> map;
// Constructor initializing every posn to be grouped with itself
UnionFindPosn(int width, int height) {
this.map = new ArrayList<ArrayList<Posn>>();
// connect each posn to itself
for (int x = 0; x < width; x += 1) {
map.add(new ArrayList<Posn>());
for (int y = 0; y < height; y += 1) {
map.get(x).add(new Posn(x, y));
}
}
}
// are the given posns in the same group?
boolean sameGroup(Posn p1, Posn p2) {
return samePosn(this.getGroup(p1), this.getGroup(p2));
}
// EFFECT: modifies map to connect the two groups
// containing the given Posns
void connect(Posn p1, Posn p2) {
Posn group1 = this.getGroup(p1);
Posn group2 = this.getGroup(p2);
this.map.get(group1.x).set(group1.y, group2);
}
// are the 2 given posns the same?
boolean samePosn(Posn p1, Posn p2) {
return p1.x == p2.x && p1.y == p2.y;
}
// find the posn labeling the group containing p
Posn getGroup(Posn p) {
Posn curr = p;
Posn next = this.map.get(curr.x).get(curr.y);
while (!this.samePosn(curr, next)) {
curr = next;
next = this.map.get(curr.x).get(curr.y);
}
// by the test condition returning curr or next is equivalent
// using this.samePosn
return next;
}
}
// to animate algorithms on a maze
abstract class MazeAnimator {
Maze maze;
MazeAnimator(Maze maze) {
this.maze = maze;
}
// EFFECT: update animation on tick
abstract void onTick();
// draw an the frame of the animation onto given background
WorldImage drawOnto(int cellSize, WorldImage bg) {
WorldImage mazeImg = this.maze.drawOnto(cellSize, bg);
// bottom middle of screen
Posn textLoc = new Posn(bg.getWidth() / 2, bg.getHeight() - 7);
return mazeImg.overlayImages(
new TextImage(textLoc, this.status(), 15, Color.BLACK));
}
// EFFECT: unknown to react to keystrokes from user
void onKeyEvent(String ke) {
// DO NOTHING: most animators don't need to react to keystrokes; those
// that do can override this method
}
// does this animator guarantee termination?
boolean alwaysTerminates() {
return true;
}
// get the status text of this animation
abstract String status();
// is this animation complete?
abstract boolean isComplete();
// next animator to use when done
abstract MazeAnimator nextAnimator();
}
// to represent animators for solving the maze
abstract class SolveAnimator extends MazeAnimator {
// Note: cameFromCell connects each cell to its previous cell
HashMap<Cell, Cell> cameFromCell;
boolean completed;
int moves;
SolveAnimator(Maze maze) {
super(maze);
this.cameFromCell = new HashMap<Cell, Cell>();
completed = false;
moves = 0;
}
// EFFECT: modify work list and cameFromCell to try to make the move
// between given cells
void tryAddMove(Cell to, Cell from) {
if (!to.traversed) {
this.addWork(to);
this.cameFromCell.put(to, from);
}
}
// EFFECT: add given cell to the worklist
abstract void addWork(Cell cell);
// EFFECT: modify cells onPath field to find direct path from end to start
void reconstruct(Cell cell) {
Cell curCell = cell;
Cell prevCell = this.cameFromCell.get(curCell);
while (curCell != this.maze.getFirstCell()/* && !curCell.onPath*/) {
curCell.onPath = true;
curCell = prevCell;
prevCell = this.cameFromCell.get(prevCell);
}
curCell.onPath = true;
}
// is this animation completed?
boolean isComplete() {
return this.completed;
}
}
// to represent an automatic solver animation
abstract class AutoSolveAnimator extends SolveAnimator {
AutoSolveAnimator(Maze maze) {
super(maze);
}
// EFFECT: changes the maze and fields on this animator to progress
// algorithm
void onTick() {
if (!this.hasWork()) {
this.completed = true;
}
else if (!this.isComplete()) {
Cell next = this.getWork();
// indicate we've now visited this cell
next.traversed = true;
if (next == this.maze.getFinalCell()) {
this.reconstruct(next);
this.completed = true;
}
// try adding all unblocked neighbor cells
else {
if (!next.left.isBlocking) {
this.tryAddMove(next.left.cell1, next);
}
if (!next.top.isBlocking) {
this.tryAddMove(next.top.cell1, next);
}
if (!next.right.isBlocking) {
this.tryAddMove(next.right.cell2, next);
}
if (!next.bot.isBlocking) {
this.tryAddMove(next.bot.cell2, next);
}
this.moves += 1;
}
}
}
// get next cell to work on
abstract Cell getWork();
// are there more cells to work on?
abstract boolean hasWork();
}
// animate a depth-first search of a maze
class DFSAnimator extends AutoSolveAnimator {
Stack<Cell> worklist;
DFSAnimator(Maze maze) {
super(maze);
this.worklist = new Stack<Cell>();
this.worklist.push(this.maze.getFirstCell());
}
// get the status text of this animation
String status() {
return "Depth First Searching. Moves: " + this.moves;
}
// next animator to use when done
MazeAnimator nextAnimator() {
return new MsgAnimator(this.maze,
"Completed Depth First Search. Moves: " + this.moves);
}
// EFFECT: add given cell to the worklist
void addWork(Cell cell) {
this.worklist.push(cell);
}
// EFFECT: remove and return next item from worklist to work on
Cell getWork() {
return this.worklist.pop();
}
// are there more cells to work on?
boolean hasWork() {
return !this.worklist.isEmpty();
}
}
//animate a breadth-first search of a maze
class BFSAnimator extends AutoSolveAnimator {
Queue<Cell> worklist;
BFSAnimator(Maze maze) {
super(maze);
this.worklist = new Queue<Cell>();
this.worklist.enqueue(this.maze.getFirstCell());
}
// is this animation complete?
boolean isComplete() {
return this.completed;
}
// get the status text of this animation
String status() {
return "Breadth First Searching. Moves: " + this.moves;
}
// next animator to use when done
MazeAnimator nextAnimator() {
return new MsgAnimator(this.maze,
"Completed Breadth First Search. Moves: " + this.moves);
}
// EFFECT: modify worklist to add given cell
void addWork(Cell cell) {
this.worklist.enqueue(cell);
}
// EFFECT: remove and return next item from worklist to work on
Cell getWork() {
return this.worklist.dequeue();
}
// are there more cells to work on?
boolean hasWork() {
return !this.worklist.isEmpty();
}
}
// User-controlled animator for maze traversal
class PlayAnimator extends SolveAnimator {
Cell head;
PlayAnimator(Maze maze) {
super(maze);
this.head = maze.getFirstCell();
head.traversed = true;
head.onPath = true;
}
// EFFECT: update this Animator's fields to progress one step
void onTick() {
// do nothing since only operates on keypresses
}
// EFFECT: modify this Animator's fields to react to keystrokes
void onKeyEvent(String ke) {
if (!this.isComplete()) {
// move left
if (ke.equals("left") && !this.head.left.isBlocking) {
tryAddMove(this.head.left.cell1, this.head);
moves += 1;
}
// move left
if (ke.equals("up") && !this.head.top.isBlocking) {
tryAddMove(this.head.top.cell1, this.head);
moves += 1;
}
// move left
if (ke.equals("right") && !this.head.right.isBlocking) {
tryAddMove(this.head.right.cell2, this.head);
moves += 1;
}
// move left
if (ke.equals("down") && !this.head.bot.isBlocking) {
tryAddMove(this.head.bot.cell2, this.head);
moves += 1;
}
}
}
// is this animation complete?
boolean isComplete() {
return this.completed;
}
// get the status text of this animation
String status() {
return "Player Solving Puzzle. Moves: " + this.moves;
}
// next animator to use when done
MazeAnimator nextAnimator() {
return new MsgAnimator(this.maze,
"Puzzle Complete! Moves: " + this.moves);
}
// EFFECT: modify this animator and from and to Cells to move player
// between given cells
void tryAddMove(Cell to, Cell from) {
super.tryAddMove(to, from);
to.onPath = true;
to.traversed = true;
from.onPath = false;
this.head = to;
// check to see if this keystroke completed maze
if (this.head == this.maze.getFinalCell()) {
this.completed = true;
this.reconstruct(head);
}
}
// EFFECT: modify worklist to add given cell
void addWork(Cell cell) {
// DO NOTHING since no worklist
}
// does this animator always terminate?
boolean alwaysTerminates() {
return false;
}
}
// a blank maze animator that just shows a maze
class IdleAnimator extends MazeAnimator {
IdleAnimator(Maze maze) {
super(maze);
}
// EFFECT: update this Animator's fields to progress one step
void onTick() {
// DO NOTHING: animator is idle
}
// is this animation complete?
boolean isComplete() {
return true;
}
// get the status text of this animation
String status() {
return "Idle";
}
// next animator to use when done
MazeAnimator nextAnimator() {
return this;
}
}
// a blank maze animator that allows for a specific message to be played
class MsgAnimator extends IdleAnimator {
String msg;
// Construct a message animator displaying given message
MsgAnimator(Maze maze, String msg) {
super(maze);
this.msg = msg;
}
// get the status text of this animation
String status() {
return msg;
}
}
// animate Kruskal generation of a maze
class KruskalAnimator extends MazeAnimator {
// union find structure, representing Cells as Posns of their location
UnionFindPosn uFind;
// number of edges used in current tree
int edgesUsed;
// number of edges for a spanning tree
int edgesNeeded;
// index of edge about to be checked
int currEdge;
KruskalAnimator(Maze maze) {
super(maze);
this.maze.wallsUp();
int width = maze.width;
int height = maze.height;
uFind = new UnionFindPosn(width, height);
edgesUsed = 0;
edgesNeeded = width * height - 1;
currEdge = 0;
}
// EFFECT: update this Animator's fields to progress one step
void onTick() {
if (!this.isComplete()) {
Edge nextEdge = this.maze.edges.get(currEdge);
Posn cell1Posn = new Posn(nextEdge.cell1.x, nextEdge.cell1.y);
Posn cell2Posn = new Posn(nextEdge.cell2.x, nextEdge.cell2.y);
currEdge += 1;
// try to find a working edge each tick
while (uFind.sameGroup(cell1Posn, cell2Posn)) {
nextEdge = this.maze.edges.get(currEdge);
cell1Posn = new Posn(nextEdge.cell1.x, nextEdge.cell1.y);
cell2Posn = new Posn(nextEdge.cell2.x, nextEdge.cell2.y);
currEdge += 1;
}
nextEdge.isBlocking = false;
this.edgesUsed += 1;
this.uFind.connect(cell1Posn, cell2Posn);
}
}
// is this animation complete?
boolean isComplete() {
return edgesUsed >= edgesNeeded;
}
// get the status text of this animation
String status() {
return "Generating maze: " + this.edgesUsed + "/" + this.edgesNeeded;
}
// next animator to use when done
MazeAnimator nextAnimator() {
return new IdleAnimator(this.maze);
}
}
// to instantly complete any other animator
class InstantAnimator extends MazeAnimator {
MazeAnimator anim;
InstantAnimator(Maze maze, MazeAnimator anim) {
super(maze);
this.anim = anim;
}
// instantly complete animation
void onTick() {
if (this.anim.alwaysTerminates()) {
while (!this.anim.isComplete()) {
this.anim.onTick();
}
}
}
// get status for this animator
String status() {
return "Completing Animation";
}
// is this animation complete?
boolean isComplete() {
if (this.anim.alwaysTerminates()) {
return this.anim.isComplete();
}
else {
return true;
}
}
// next animator to use when done
MazeAnimator nextAnimator() {
if (this.anim.alwaysTerminates()) {
return this.anim.nextAnimator();
}
else {
return this.anim;
}
}
}