-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLightEmAll.java
1373 lines (1186 loc) · 38.6 KB
/
LightEmAll.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.util.*;
import tester.*;
import javalib.impworld.*;
import java.awt.Color;
import javalib.worldimages.*;
// to represent the LightEmAll world
class LightEmAll extends World {
// a list of columns of GamePieces,
// i.e., represents the board in column-major order
ArrayList<ArrayList<GamePiece>> board;
// a list of all edges
ArrayList<Edge> allEdges;
// a list of edges of the minimum spanning tree
ArrayList<Edge> mst;
// the width and height of the board
int width;
int height;
// the current location of the power station,
// as well as its effective radius
int powerRow;
int powerCol;
// for rotations
Random rand;
// win state
boolean win;
// timer values
int seconds;
int minutes;
// click value
int clicks;
// bias for edges
// horizontal, vertical, or normal
String bias;
// representatives in board
HashMap<GamePiece, GamePiece> representatives;
// regular game constructor (no bias)
LightEmAll(int width, int height) {
this.width = width;
this.height = height;
// origin: top left
this.powerRow = 0;
this.powerCol = 0;
// initializes board
this.board = this.makeBoard();
// to randomize tiles and edge weights
this.rand = new Random();
// no for regular game
this.bias = "normal";
// assigning edges
this.allEdges = new ArrayList<Edge>();
this.assignEdges();
// setting representatives
this.representatives = this.setReps();
// using kruskals to reduce edge list
// and get mst
this.mst = this.kruskal();
// connecting pieces based off mst
this.connectPieces();
// to randomly rotate tiles for gameplay
this.randomStart();
// win condition
this.win = false;
// BFS
this.lightEmUp();
// timer values, start at 0
this.seconds = 0;
this.minutes = 0;
// click value, starts at 0
this.clicks = 0;
}
// game constructor for choosing bias
LightEmAll(int width, int height, String bias) {
this.width = width;
this.height = height;
// origin: top left
this.powerRow = 0;
this.powerCol = 0;
// initializes board
this.board = this.makeBoard();
// to randomize tiles and edge weights
this.rand = new Random();
// ability to choose bias based off of String input
// "horizontal", "vertical", or "normal"
this.bias = bias;
// assignign edges
this.allEdges = new ArrayList<Edge>();
this.assignEdges();
// setting representatives
this.representatives = this.setReps();
// using kruskals to reduce edge list
// and get mst
this.mst = this.kruskal();
// connects tiles based off of mst
this.connectPieces();
// randomly rotates the tiles
this.randomStart();
// win condition
this.win = false;
// BFS
this.lightEmUp();
// timer values
// start at 0
this.seconds = 0;
this.minutes = 0;
// click value
// starts at 0
this.clicks = 0;
}
// constructor for testing, takes in a seeded random
LightEmAll(int width, int height, Random rand) {
this.width = width;
this.height = height;
// always in the middle (part 1)
this.powerRow = 0;
this.powerCol = 0;
// initializes board
this.board = this.makeBoard();
// seeded random for testing
this.rand = rand;
// no bias for tests
this.bias = "normal";
// assigning edges
this.allEdges = new ArrayList<Edge>();
this.assignEdges();
// setting representatives
this.representatives = this.setReps();
// using kruskals to reduce edge list
// and get mst
this.mst = this.kruskal();
// connects tiles based off of mst
this.connectPieces();
// randomly rotates the tiles
this.randomStart();
// win condition
this.win = false;
// BFS
this.lightEmUp();
// timer values
// start at 0
this.seconds = 0;
this.minutes = 0;
// click value
// starts at 0
this.clicks = 0;
}
// to randomly rotate tiles at the start of the game
public void randomStart() {
// random amount of rotations
int rotateIt = rand.nextInt(4);
for (int w = 0; w < width; w++) {
for (int h = 0; h < height; h++) {
GamePiece tile = this.board.get(h).get(w);
for (int r = 0; r < rotateIt; r++) {
// rotating tile rotateIt amount of times
tile.rotate();
}
// resetting random amount of rotations for next tile
rotateIt = rand.nextInt(4);
}
}
}
// to make the board of the game
public ArrayList<ArrayList<GamePiece>> makeBoard() {
ArrayList<ArrayList<GamePiece>> board = new ArrayList<ArrayList<GamePiece>>();
for (int h = 0; h < this.height; h++) {
// creating rows
ArrayList<GamePiece> row = new ArrayList<GamePiece>();
for (int w = 0; w < this.width; w++) {
// creating each GamePiece in columns
GamePiece gp = new GamePiece(h, w);
row.add(gp);
// setting powerStation to origin (top left)
if (h == 0 && w == 0) {
gp.powerStation = true;
}
}
// adding everything to board
board.add(row);
}
return board;
}
// to connect the pieces by changing their boolean values
// based on the edges in the mst
void connectPieces() {
for (Edge e : this.mst) {
GamePiece gp1 = e.fromNode;
GamePiece gp2 = e.toNode;
if (gp1.row < gp2.row && gp1.col == gp2.col) {
gp1.bottom = true;
gp2.top = true;
}
if (gp1.row > gp2.row && gp1.col == gp2.col) {
gp1.top = true;
gp2.bottom = true;
}
if (gp1.row == gp2.row && gp1.col < gp2.col) {
gp1.right = true;
gp2.left = true;
}
if (gp1.row == gp2.row && gp1.col > gp2.col) {
gp1.left = true;
gp2.right = true;
}
}
}
// to draw the current scene
public WorldScene makeScene() {
WorldScene scene = new WorldScene(this.width, this.height);
WorldImage clock = new TextImage("【" + this.minutesAsString() + " : "
+ this.secondsAsString() + "】", 30, FontStyle.BOLD, Color.DARK_GRAY);
WorldImage clicks = new TextImage("Rotations: " +
Integer.toString(this.clicks), 25, FontStyle.BOLD, Color.DARK_GRAY);
WorldImage box = new RectangleImage(width * 35, height * 20,
OutlineMode.SOLID, Color.LIGHT_GRAY);
WorldImage winText = new AboveImage(new TextImage("YOU WIN!",
width * 7, FontStyle.BOLD, Color.black),
new AboveImage(new TextImage("Clicks: " + Integer.toString(this.clicks),
width * 3, FontStyle.BOLD, Color.black),
new TextImage("Press r to restart", width * 3, FontStyle.BOLD, Color.black)));
WorldImage winBox = new OverlayImage(winText, box);
for (int w = 0; w < width; w++) {
for (int h = 0; h < height; h++) {
GamePiece tile = this.board.get(h).get(w);
// in-play game scene
if (tile.powerStation) {
scene.placeImageXY(tile.tileImage(70, 5, Color.red, true), w * 70 + 35, h * 70 + 35);
}
else if (tile.powered) {
scene.placeImageXY(tile.tileImage(70, 5, Color.red, false), w * 70 + 35, h * 70 + 35);
}
else {
scene.placeImageXY(tile.tileImage(70, 5, Color.LIGHT_GRAY, false), w * 70 + 35,
h * 70 + 35);
}
}
}
// scene when game is won
if (this.win) {
scene.placeImageXY(winBox, width * 35, height * 35);
}
// clock and click counts
scene.placeImageXY(clock, width * 60, height * 70 + 35);
scene.placeImageXY(clicks, width * 10, height * 70 + 35);
return scene;
}
// timer for how long the game has been running
public void onTick() {
if (!this.win) {
if (this.seconds > 0 && this.seconds % 59 == 0) {
this.seconds = 0;
this.minutes += 1;
}
else {
this.seconds += 1;
}
}
}
// to display one digit seconds with a 0 before it
public String secondsAsString() {
if (this.seconds < 10) {
return "0" + Integer.toString(seconds);
}
else {
return Integer.toString(seconds);
}
}
// to display one digit seconds with a 0 before it
public String minutesAsString() {
if (this.minutes < 10) {
return "0" + Integer.toString(minutes);
}
else {
return Integer.toString(minutes);
}
}
// to assign random edge values for the GamePieces and mst
void assignEdges() {
int val = width * height;
for (int h = 0; h < this.height; h++) {
for (int w = 0; w < this.width; w++) {
GamePiece tile = board.get(h).get(w);
// no bias, all randomly assigned
if (bias.equals("normal")) {
if (h > 0) {
Edge e = new Edge(tile, board.get(h - 1).get(w), rand.nextInt(val));
allEdges.add(e);
}
if (h < height - 1) {
Edge e = new Edge(tile, board.get(h + 1).get(w), rand.nextInt(val));
allEdges.add(e);
}
if (w > 0) {
Edge e = new Edge(tile, board.get(h).get(w - 1), rand.nextInt(val));
allEdges.add(e);
}
if (w < width - 1) {
Edge e = new Edge(tile, board.get(h).get(w + 1), rand.nextInt(val));
allEdges.add(e);
}
}
// horizontal bias
// giving vertical wiring a less edge weight
if (bias.equals("horizontal")) {
if (h > 0) {
Edge e = new Edge(tile, board.get(h - 1).get(w), rand.nextInt(1));
allEdges.add(e);
}
else if (h < height - 1) {
Edge e = new Edge(tile, board.get(h + 1).get(w), rand.nextInt(1));
allEdges.add(e);
}
if (w > 0) {
Edge e = new Edge(tile, board.get(h).get(w - 1), rand.nextInt(val));
allEdges.add(e);
}
else if (w < width - 1) {
Edge e = new Edge(tile, board.get(h).get(w + 1), rand.nextInt(val));
allEdges.add(e);
}
}
// vertical bias
// giving vertical wiring a less edge weight
if (bias.equals("vertical")) {
if (h > 0) {
Edge e = new Edge(tile, board.get(h - 1).get(w), rand.nextInt(val));
allEdges.add(e);
}
else if (h < height - 1) {
Edge e = new Edge(tile, board.get(h + 1).get(w), rand.nextInt(val));
allEdges.add(e);
}
if (w > 0) {
Edge e = new Edge(tile, board.get(h).get(w - 1), rand.nextInt(1));
allEdges.add(e);
}
else if (w < width - 1) {
Edge e = new Edge(tile, board.get(h).get(w + 1), rand.nextInt(1));
allEdges.add(e);
}
}
}
}
// sorting from least to greatest edge values
allEdges.sort(new EdgeSorting());
}
// to set GamePieces as representatives of themselves
HashMap<GamePiece, GamePiece> setReps() {
HashMap<GamePiece, GamePiece> representatives = new HashMap<GamePiece, GamePiece>();
for (ArrayList<GamePiece> g : board) {
for (GamePiece p : g) {
representatives.put(p, p);
}
}
return representatives;
}
// to find whether two nodes are in the same group
public GamePiece find(GamePiece node) {
GamePiece from = this.representatives.get(node);
if (node.equals(from)) {
return from;
}
else {
return find(from);
}
}
// to union two disjoint groups together
void union(GamePiece from, GamePiece to) {
this.representatives.put(find(from), find(to));
}
// implementation of kruskal's algorithm to find the minimum spanning tree for the board
public ArrayList<Edge> kruskal() {
ArrayList<Edge> worklist = new ArrayList<Edge>(); // all edges in graph, sorted by edge weights;
ArrayList<Edge> edgesInTree = new ArrayList<Edge>();
// adding edges to the worklist
worklist.addAll(allEdges);
// reducing to one tree
while (worklist.size() > 1) {
Edge curr = worklist.remove(0);
if (find(curr.fromNode).equals(find(curr.toNode))) {
// do nothing
}
// union the two values together
else {
edgesInTree.add(curr);
union(find(curr.fromNode),
find(curr.toNode));
}
}
return edgesInTree;
}
// handler for mouse clicks clicks
public void onMouseClicked(Posn pos) {
int tileSize = 70;
int w = Math.floorDiv(pos.x, tileSize);
int h = Math.floorDiv(pos.y, tileSize);
// constraints for out of bounds and if won
if (w > this.width - 1 || h > this.height - 1 || this.win) {
return;
}
GamePiece tileClicked = board.get(h).get(w);
tileClicked.rotate();
// updating clicks values for each rotation
this.clicks += 1;
// running BFS method and checking for win after each click
this.lightEmUp();
this.winCondition();
}
// keyEvent for moving around powerStation
public void onKeyEvent(String key) {
// to make the game stop when won
// only an option to restart
if (this.win) {
if (key.equals("r")) {
// using same bias as before
String gameBias = this.bias;
this.powerRow = 0;
this.powerCol = 0;
this.board = this.makeBoard();
this.rand = new Random();
this.bias = gameBias;
this.allEdges = new ArrayList<Edge>();
this.assignEdges();
this.representatives = this.setReps();
this.mst = this.kruskal();
this.connectPieces();
this.randomStart();
this.win = false;
this.lightEmUp();
this.seconds = 0;
this.minutes = 0;
this.clicks = 0;
}
else {
return;
}
}
// tile where the power station is
GamePiece tile = this.board.get(this.powerRow).get(this.powerCol);
// if not leftmost column
if (this.powerCol > 0) {
GamePiece tileLeft = this.board.get(this.powerRow).get(this.powerCol - 1);
if (key.equals("left") && tile.hasLeft() && tileLeft.hasRight()) {
tile.removeStation();
int newPowerCol = this.powerCol - 1;
// tile where station was moved to
GamePiece newTile = this.board.get(this.powerRow).get(newPowerCol);
newTile.placeStation();
this.powerCol = newPowerCol;
this.lightEmUp();
}
}
// if not rightmost column
if (this.powerCol < width - 1) {
GamePiece tileRight = this.board.get(this.powerRow).get(this.powerCol + 1);
if (key.equals("right") && tile.hasRight() && tileRight.hasLeft()) {
tile.removeStation();
int newPowerCol = this.powerCol + 1;
// tile where station was moved to
GamePiece newTile = this.board.get(this.powerRow).get(newPowerCol);
newTile.placeStation();
this.powerCol = newPowerCol;
this.lightEmUp();
}
}
// if not top row
if (this.powerRow > 0) {
GamePiece tileTop = this.board.get(this.powerRow - 1).get(this.powerCol);
if (key.equals("up") && tile.hasTop() && tileTop.hasBottom()) {
tile.removeStation();
int newPowerRow = this.powerRow - 1;
// tile where station was moved to
GamePiece newTile = this.board.get(newPowerRow).get(this.powerCol);
newTile.placeStation();
this.powerRow = newPowerRow;
this.lightEmUp();
}
}
// if not bottom row
if (this.powerRow < height - 1) {
GamePiece tileBottom = this.board.get(this.powerRow + 1).get(this.powerCol);
if (key.equals("down") && tile.hasBottom() && tileBottom.hasTop()) {
tile.removeStation();
int newPowerRow = this.powerRow + 1;
// tile where station was moved to
GamePiece newTile = this.board.get(newPowerRow).get(this.powerCol);
newTile.placeStation();
this.powerRow = newPowerRow;
this.lightEmUp();
}
}
}
// BFS for powering neighboring pieces
public void lightEmUp() {
Queue<GamePiece> worklist = new LinkedList<GamePiece>();
ArrayList<GamePiece> alreadySeen = new ArrayList<GamePiece>();
GamePiece station = this.board.get(this.powerRow).get(this.powerCol);
// initialize worklist with the powerstation
worklist.add(station);
for (int w = 0; w < width; w++) {
for (int h = 0; h < height; h++) {
GamePiece tile = this.board.get(h).get(w);
tile.powerOff();
}
}
// as long as the worklist isn't empty
while (!worklist.isEmpty()) {
GamePiece next = worklist.poll();
next.powerOn();
// we've already seen this one
if (!alreadySeen.contains(next)) {
if (next.col > 0) {
GamePiece tileLeft = this.board.get(next.row).get(next.col - 1);
if (next.hasLeft() && tileLeft.hasRight()) {
worklist.add(tileLeft);
}
}
if (next.col < width - 1) {
GamePiece tileRight = this.board.get(next.row).get(next.col + 1);
if (tileRight.hasLeft() && next.hasRight()) {
worklist.add(tileRight);
}
}
if (next.row > 0) {
GamePiece tileTop = this.board.get(next.row - 1).get(next.col);
if (tileTop.hasBottom() && next.hasTop()) {
worklist.add(tileTop);
}
}
if (next.row < height - 1) {
GamePiece tileBottom = this.board.get(next.row + 1).get(next.col);
if (tileBottom.hasTop() && next.hasBottom()) {
worklist.add(tileBottom);
}
}
alreadySeen.add(next);
}
}
}
// to check if the game has been won
void winCondition() {
int target = width * height;
for (int w = 0; w < width; w++) {
for (int h = 0; h < height; h++) {
GamePiece tile = this.board.get(h).get(w);
if (tile.powered) {
target -= 1;
}
}
}
if (target == 0) {
this.win = true;
}
}
}
// to represent a GamePiece
class GamePiece {
// in logical coordinates, with the origin
// at the top-left corner of the screen
int row;
int col;
// whether this GamePiece is connected to the
// adjacent left, right, top, or bottom pieces
boolean left;
boolean right;
boolean top;
boolean bottom;
// whether the power station is on this piece
boolean powerStation;
boolean powered;
int distance;
GamePiece(int row, int col) {
this.row = row;
this.col = col;
this.left = false;
this.right = false;
this.top = false;
this.bottom = false;
this.powerStation = false;
this.powered = false;
this.distance = 0;
}
// overriding equals to check equality of GamePieces
public boolean equals(Object that) {
if (!(that instanceof GamePiece)) {
return false;
}
else {
GamePiece other = (GamePiece) that;
return this.row == other.row && this.col == other.col;
}
}
//overriding equals for GamePieces
public int hashCode() {
return this.col * 5000;
}
// Generate an image of this, the given GamePiece.
// - size: the size of the tile, in pixels
// - wireWidth: the width of wires, in pixels
// - wireColor: the Color to use for rendering wires on this
// - hasPowerStation: if true, draws a fancy star on this tile to represent the
// power station
WorldImage tileImage(int size, int wireWidth, Color wireColor, boolean hasPowerStation) {
// Start tile image off as a blue square with a wire-width square in the middle,
// to make image "cleaner" (will look strange if tile has no wire, but that
// can't be)
WorldImage image = new OverlayImage(
new RectangleImage(wireWidth, wireWidth, OutlineMode.SOLID, wireColor),
new RectangleImage(size, size, OutlineMode.SOLID, Color.DARK_GRAY));
WorldImage vWire = new RectangleImage(wireWidth, (size + 1) / 2, OutlineMode.SOLID, wireColor);
WorldImage hWire = new RectangleImage((size + 1) / 2, wireWidth, OutlineMode.SOLID, wireColor);
if (this.top) {
image = new OverlayOffsetAlign(AlignModeX.CENTER, AlignModeY.TOP, vWire, 0, 0, image);
}
if (this.right) {
image = new OverlayOffsetAlign(AlignModeX.RIGHT, AlignModeY.MIDDLE, hWire, 0, 0, image);
}
if (this.bottom) {
image = new OverlayOffsetAlign(AlignModeX.CENTER, AlignModeY.BOTTOM, vWire, 0, 0, image);
}
if (this.left) {
image = new OverlayOffsetAlign(AlignModeX.LEFT, AlignModeY.MIDDLE, hWire, 0, 0, image);
}
if (hasPowerStation) {
image = new OverlayImage(
new OverlayImage(new StarImage(size / 3, 7, OutlineMode.OUTLINE, new Color(255, 128, 0)),
new StarImage(size / 3, 7, OutlineMode.SOLID, new Color(0, 255, 255))),
image);
}
return image;
}
// to rotate this tiles wires
void rotate() {
boolean originalLeft = this.left;
this.left = this.bottom;
this.bottom = this.right;
this.right = this.top;
this.top = originalLeft;
}
// does this title have a bottom?
boolean hasBottom() {
return this.bottom;
}
//does this title have a top?
boolean hasTop() {
return this.top;
}
//does this title have a left?
boolean hasLeft() {
return this.left;
}
//does this title have a right?
boolean hasRight() {
return this.right;
}
// to place a powerStation at this tile
void placeStation() {
this.powerStation = true;
}
// to place remove the powerStation at this tile
void removeStation() {
this.powerStation = false;
}
// to power on this tile
void powerOn() {
this.powered = true;
}
//to power off this tile
void powerOff() {
this.powered = false;
}
}
// to represent an edge
class Edge {
GamePiece fromNode;
GamePiece toNode;
int weight;
Edge(GamePiece fromNode, GamePiece toNode, int weight) {
this.fromNode = fromNode;
this.toNode = toNode;
this.weight = weight;
}
}
//comparator for sorting edges
class EdgeSorting implements Comparator<Edge> {
// compares the weight of two edges
public int compare(Edge e1, Edge e2) {
return e1.weight - e2.weight;
}
}
// examples and tests for the world
class ExamplesLight {
// to run the game
void testBigBang(Tester t) {
LightEmAll world = new LightEmAll(7, 7);
int worldWidth = 70 * world.width;
int worldHeight = 70 * world.height + 80;
double tickRate = 1;
world.bigBang(worldWidth, worldHeight, tickRate);
}
// 2 x 2 board
LightEmAll l1;
GamePiece g0;
GamePiece g1;
GamePiece g2;
GamePiece g3;
Edge e0;
Edge e1;
Edge e2;
Edge e3;
Edge e4;
ArrayList<GamePiece> r0;
ArrayList<GamePiece> r1;
// 3 x 2 board
LightEmAll l2;
GamePiece g4;
GamePiece g5;
GamePiece g6;
GamePiece g7;
GamePiece g8;
GamePiece g9;
ArrayList<GamePiece> r2;
ArrayList<GamePiece> r3;
ArrayList<GamePiece> r4;
// Comparator Class
EdgeSorting edgeSort = new EdgeSorting();
// data for tests
void init() {
l1 = new LightEmAll(2, 2, new Random(5));
g0 = l1.board.get(0).get(0);
g1 = l1.board.get(0).get(1);
g2 = l1.board.get(1).get(0);
g3 = l1.board.get(1).get(1);
r0 = new ArrayList<GamePiece>(Arrays.asList(g0, g1));
r1 = new ArrayList<GamePiece>(Arrays.asList(g2, g3));
l2 = new LightEmAll(2, 3, new Random(6));
g4 = l2.board.get(0).get(0);
g5 = l2.board.get(0).get(1);
g6 = l2.board.get(1).get(0);
g7 = l2.board.get(1).get(1);
g8 = l2.board.get(2).get(0);
g9 = l2.board.get(2).get(1);
r2 = new ArrayList<GamePiece>(Arrays.asList(g4, g5));
r3 = new ArrayList<GamePiece>(Arrays.asList(g6, g7));
r4 = new ArrayList<GamePiece>(Arrays.asList(g8, g9));
e0 = l1.allEdges.get(0);
e1 = l1.allEdges.get(1);
e2 = l1.allEdges.get(1);
e3 = l1.allEdges.get(3);
}
//tests for the method compare in EdgeSorting class
boolean testEdgeComparator(Tester t) {
init();
return t.checkExpect(edgeSort.compare(e1, e3), -1)
&& t.checkExpect(edgeSort.compare(e3, e3), 0);
}
/* _____
.' `.
/ \
| |
'. +^^^+ .'
`. \./ .'
|_|_|
(___)
(___)
`---'
* LightEmAll Tests:
*/
// tests for the method randomStart
void testRandomStart(Tester t) {
init();
// tiles initially
t.checkExpect(g0.top, false);
t.checkExpect(g0.bottom, true);
t.checkExpect(g0.left, true);
t.checkExpect(g0.right, false);
t.checkExpect(g1.top, true);
t.checkExpect(g1.bottom, false);
t.checkExpect(g1.left, false);
t.checkExpect(g1.right, true);
// randomizing
l1.randomStart();
// tiles change
t.checkExpect(g0.top, true);
t.checkExpect(g0.bottom, false);
t.checkExpect(g0.left, true);
t.checkExpect(g0.right, false);
t.checkExpect(g1.top, true);
t.checkExpect(g1.bottom, false);
t.checkExpect(g1.left, true);
t.checkExpect(g1.right, false);
}
// tests for the method kruskal
void testKruskal(Tester t) {
init();
// mst is initialized with kruskal
// testing to see if mst size is n - 1
// and less than the original allEdges size
Edge e0 = new Edge(g0, g1, 0);
Edge e1 = new Edge(g1, g3, 0);
Edge e2 = new Edge(g2, g0, 1);
t.checkExpect(l1.allEdges.size(), 8);
t.checkExpect(l1.mst.size(), 3);
t.checkExpect(l1.mst, new ArrayList<Edge>(Arrays.asList(e0, e1, e2)));
Edge e3 = new Edge(g4, g5, 0);
Edge e4 = new Edge(g9, g7, 0);
Edge e5 = new Edge(g4, g6, 1);
Edge e6 = new Edge(g5, g7, 2);
Edge e7 = new Edge(g8, g9, 2);
t.checkExpect(l2.allEdges.size(), 14);
t.checkExpect(l2.mst.size(), 5);
t.checkExpect(l2.mst, new ArrayList<Edge>(Arrays.asList(e3, e4, e5, e6, e7)));
}
// tests for the method setReps
void testSetReps(Tester t) {
init();
// manually setting representatives
HashMap<GamePiece, GamePiece> l1Rep = new HashMap<GamePiece, GamePiece>();
l1Rep.put(g0, g1);
l1Rep.put(g1, g3);
l1Rep.put(g2, g3);
l1Rep.put(g3, g3);
HashMap<GamePiece, GamePiece> l2Rep = new HashMap<GamePiece, GamePiece>();
l2Rep.put(g4, g5);
l2Rep.put(g5, g6);
l2Rep.put(g6, g7);
l2Rep.put(g8, g7);
l2Rep.put(g9, g7);
l2Rep.put(g7, g7);
// representatives are initialized with setReps
t.checkExpect(l1.representatives, l1Rep);
t.checkExpect(l2.representatives, l2Rep);
}
// tests for the methods find and union
void testUnionFind(Tester t) {
init();
// representatives are intializes with the same parents
t.checkExpect(l2.find(g5), g7);
t.checkExpect(l2.find(g8), g7);
l2.union(g5, g8);
t.checkExpect(l2.find(g5), g7);
t.checkExpect(l2.find(g8), g7);
}
// tests for the method onTick
void testOnTick(Tester t) {
init();
l1.seconds = 57;
t.checkExpect(l1.seconds, 57);
l1.onTick();
t.checkExpect(l1.seconds, 58);
l1.onTick();
t.checkExpect(l1.seconds, 59);
l1.onTick();
t.checkExpect(l1.seconds, 0);
t.checkExpect(l1.minutes, 1);
}
// tests for the method assignEdges
void testAssignEdges(Tester t) {
init();
// manually creating edges that should be in list
Edge e0 = new Edge(g0, g1, 0);
Edge e1 = new Edge(g1, g3, 0);
Edge e2 = new Edge(g2, g0, 1);
Edge e3 = new Edge(g3, g1, 1);
Edge e4 = new Edge(g0, g2, 2);
Edge e5 = new Edge(g1, g0, 2);
Edge e6 = new Edge(g2, g3, 3);
Edge e7 = new Edge(g3, g2, 3);
// allEdges list is initialized with assignEdges
// testing to see if edges are assigned
t.checkExpect(l1.allEdges, new ArrayList<Edge>(Arrays.asList(e0, e1, e2, e3, e4, e5, e6, e7)));
}
// test for the method secondsAsString and minutesAsString
void testAsString(Tester t) {
init();
// 1 digit seconds
l1.seconds = 3;
t.checkExpect(l1.secondsAsString(), "03");