-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLightEmAll.java
More file actions
773 lines (678 loc) · 23.3 KB
/
Copy pathLightEmAll.java
File metadata and controls
773 lines (678 loc) · 23.3 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
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Random;
import tester.*;
import javalib.impworld.*;
import java.awt.Color;
import javalib.worldimages.*;
/*
EXTRA CREDIT
- click r to restart the game at any point.
- score is displayed after a users win the game.
*/
// to represent a LightEmAll game
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 nodes
ArrayList<GamePiece> nodes;
// a list of edges of the minimum spanning tree
ArrayList<Edge> mst;
// the width and height of the board
int width;
int height;
int tileSize = 40;
// the current location of the power station,
// as well as its effective radius
int powerRow;
int powerCol;
int radius;
int userScore;
LightEmAll(int width, int height) {
this.width = width;
this.height = height;
this.powerRow = 0;
this.powerCol = 0;
this.userScore = 0;
this.generateBoard();
this.randomizeBoard();
this.updatePower();
}
//randomize the tiles in the board
void randomizeBoard() {
Random rand = new Random();
for (ArrayList<GamePiece> col : board) {
for (GamePiece gp : col) {
int spins = rand.nextInt(4);
for (int i = 0; i < spins; i++) {
gp.rotate();
}
}
}
}
//randomize the tiles in the board (SEEDED FOR TESTING)
void randomizeBoardSeeded() {
Random rand = new Random(4);
for (ArrayList<GamePiece> col : board) {
for (GamePiece gp : col) {
int spins = rand.nextInt(4);
for (int i = 0; i < spins; i++) {
gp.rotate();
}
}
}
}
// return a new ArrayList with all the tile that is connected to the
// given GamePiece
ArrayList<GamePiece> getConnectedTile(GamePiece gp) {
ArrayList<GamePiece> connected = new ArrayList<>();
if (gp.top && gp.row > 0) {
GamePiece aboveGP = board.get(gp.col).get(gp.row - 1);
if (aboveGP.bottom) {
connected.add(aboveGP);
}
}
if (gp.bottom && gp.row < height - 1) {
GamePiece belowGP = board.get(gp.col).get(gp.row + 1);
if (belowGP.top) {
connected.add(belowGP);
}
}
if (gp.left && gp.col > 0) {
GamePiece leftGP = board.get(gp.col - 1).get(gp.row);
if (leftGP.right) {
connected.add(leftGP);
}
}
if (gp.right && gp.col < width - 1) {
GamePiece rightGp = board.get(gp.col + 1).get(gp.row);
if (rightGp.left) {
connected.add(rightGp);
}
}
return connected;
}
// update the power status of the tiles
void updatePower() {
for (GamePiece gp : nodes) {
gp.powered = false;
}
ArrayList<GamePiece> waitList = new ArrayList<>();
ArrayList<GamePiece> seen = new ArrayList<>();
GamePiece powerStation = board.get(powerCol).get(powerRow);
powerStation.powered = true;
waitList.add(powerStation);
while (!waitList.isEmpty()) {
GamePiece current = waitList.remove(0);
ArrayList<GamePiece> connected = getConnectedTile(current);
seen.add(current);
for (GamePiece neighbor : connected) {
neighbor.powered = true;
if (!seen.contains(neighbor)) {
waitList.add(neighbor);
}
}
}
}
// are all the wires powered?
boolean allPowered() {
for (GamePiece gp : nodes) {
if (!gp.powered) {
return false;
}
}
return true;
}
// make edges for every GamePiece
ArrayList<Edge> makeEdges() {
ArrayList<Edge> edges = new ArrayList<>();
Random rand = new Random();
for (GamePiece gp : nodes) {
if (gp.col < width - 1) {
edges.add(new Edge(gp, board.get(gp.col + 1).get(gp.row), rand.nextInt(100)));
}
if (gp.row < height - 1) {
edges.add(new Edge(gp, board.get(gp.col).get(gp.row + 1), rand.nextInt(100)));
}
}
return edges;
}
// create the board using Kruskal's algo
public void generateBoard() {
this.nodes = new ArrayList<>();
this.board = new ArrayList<>();
for (int col = 0; col < this.width; col++) {
ArrayList<GamePiece> column = new ArrayList<>();
for (int row = 0; row < this.height; row++) {
GamePiece gp = new GamePiece(row, col, false, false, false, false);
column.add(gp);
this.nodes.add(gp);
}
this.board.add(column);
}
ArrayList<Edge> edges = this.makeEdges();
// get mst
TreeUtils util = new TreeUtils();
this.mst = util.getEdges(edges, this.nodes);
for (Edge edge : mst) {
GamePiece from = edge.fromNode;
GamePiece to = edge.toNode;
if (from.row == to.row) {
if (from.col < to.col) {
from.right = true;
to.left = true;
} else {
from.left = true;
to.right = true;
}
} else {
if (from.row < to.row) {
from.bottom = true;
to.top = true;
} else {
from.top = true;
to.bottom = true;
}
}
}
board.get(0).get(0).addPowerStation();
updatePower();
}
// rotate the GamePiece
public void onMouseClicked(Posn pos) {
int col = pos.x / tileSize;
int row = pos.y / tileSize;
if (row >= 0 && row < height && col >= 0 && col < width) {
GamePiece clicked = board.get(col).get(row);
clicked.rotate();
updatePower();
userScore++;
}
}
// override onKeyEvent so that when the arrow keys are pressed,
// the powerStation is moved, if r is clicked, restart the game with a new board
// EFFECT : changes the value of powerRow and powerCol
public void onKeyEvent(String key) {
int index = 0;
boolean moved = false;
if (key.equals("r")) {
this.generateBoard();
this.randomizeBoard();
this.updatePower();
userScore = 0;
} else {
while (index < nodes.size()) {
GamePiece gp = nodes.get(index);
if (!moved && gp.powered && gp.powerStation) {
gp.movePowerStation(key, this);
this.updatePower();
userScore = userScore + 1;
moved = true;
}
index++;
}
}
}
// make the WorldScene
public WorldScene makeScene() {
WorldScene scene = new WorldScene(width * tileSize, height * tileSize);
for (int col = 0; col < board.size(); col++) {
ArrayList<GamePiece> column = board.get(col);
for (int row = 0; row < column.size(); row++) {
GamePiece gp = column.get(row);
Color wireColor = Color.GRAY;
if (gp.powered) {
wireColor = Color.YELLOW;
}
WorldImage tile = gp.tileImage(tileSize, tileSize / 10, wireColor, gp.powerStation);
scene.placeImageXY(tile, col * tileSize + tileSize / 2,
row * tileSize + tileSize / 2);
}
}
if (this.allPowered()) {
WorldImage winText = new TextImage("You Win!", 40, Color.GREEN);
WorldImage scoreText = new TextImage("Score : " + userScore, 20, Color.GREEN);
WorldImage endText = new AboveImage(winText, scoreText);
scene.placeImageXY(endText, width * tileSize / 2, height * tileSize / 2);
}
return scene;
}
// returns the game piece with the row and column matching the given
// row and column
public GamePiece findMatchingNode(int targetRow, int targetCol) {
boolean found = false;
int index = 0;
GamePiece currentNode = new GamePiece(0, 0, false, false, false, false);
while (!found && index < nodes.size()) {
currentNode = nodes.get(index);
if (currentNode.row == targetRow && currentNode.col == targetCol) {
found = true;
} else {
index++;
}
}
return currentNode;
}
}
// 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;
GamePiece(int row, int col, boolean left, boolean right, boolean top, boolean bottom) {
this.row = row;
this.col = col;
this.left = left;
this.right = right;
this.top = top;
this.bottom = bottom;
this.powerStation = false;
this.powered = false;
}
// rotates the tile clockwise
// EFFECT : replaces the value of each side of the tile to the value of the side to its left
void rotate() {
boolean prevTop = this.top;
boolean prevRight = this.right;
boolean prevBottom = this.bottom;
boolean prevLeft = this.left;
this.top = prevLeft;
this.right = prevTop;
this.bottom = prevRight;
this.left = prevBottom;
}
// moves the power station in the direction of the arrow key pressed
// if a key other than the arrow key is pressed or there is no tile in that
// direction, do nothing
// EFFECT : remove the power station from this tile
void movePowerStation(String key, LightEmAll world) {
int targetRow = world.powerRow;
int targetCol = world.powerCol;
if (key.equals("up") && this.top && (this.row != 0)) {
// move the power station up
targetRow = this.row - 1;
targetCol = this.col;
if (world.findMatchingNode(targetRow, targetCol).bottom) {
this.powerStation = false;
world.findMatchingNode(targetRow, targetCol).addPowerStation();
world.powerCol = targetCol;
world.powerRow = targetRow;
}
} else if (key.equals("left") && this.left && this.col != 0) {
// move the power station to the left
targetRow = this.row;
targetCol = this.col - 1;
if (world.findMatchingNode(targetRow, targetCol).right) {
this.powerStation = false;
world.findMatchingNode(targetRow, targetCol).addPowerStation();
world.powerCol = targetCol;
world.powerRow = targetRow;
}
} else if (key.equals("right") && this.right && this.col != world.board.size()) {
// move the power station to the right
targetRow = this.row;
targetCol = this.col + 1;
if (world.findMatchingNode(targetRow, targetCol).left) {
this.powerStation = false;
world.findMatchingNode(targetRow, targetCol).addPowerStation();
world.powerCol = targetCol;
world.powerRow = targetRow;
}
} else if (key.equals("down") && this.bottom && this.row != world.board.get(0).size()) {
// move the power station down
targetRow = this.row + 1;
targetCol = this.col;
if (world.findMatchingNode(targetRow, targetCol).top) {
this.powerStation = false;
world.findMatchingNode(targetRow, targetCol).addPowerStation();
world.powerCol = targetCol;
world.powerRow = targetRow;
}
}
}
// creates an id for the hashmap using the row and column values
String getID() {
return "r" + this.row + "c" + this.col;
}
// adds the power station to this game piece
// EFFECT : sets the value of powerStation to true
void addPowerStation() {
this.powerStation = true;
}
// returns the image of the tile, with the wire images overlayed
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));
// a vertical wire piece
WorldImage vWire = new RectangleImage(wireWidth, (size + 1) / 2, OutlineMode.SOLID, wireColor);
// a horizontal wire piece
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 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;
}
}
// Utils class in order to run Kruskal's
class TreeUtils {
ArrayList<Edge> getEdges(ArrayList<Edge> workList, ArrayList<GamePiece> nodes) {
workList.sort((edge1, edge2) -> edge1.weight - edge2.weight);
HashMap<String, String> representatives = new HashMap<String, String>();
ArrayList<Edge> edgesInTree = new ArrayList<Edge>();
for (GamePiece node : nodes) {
representatives.put(node.getID(), node.getID());
}
for (Edge edge : workList) {
String from = find(representatives, edge.fromNode.getID());
String to = find(representatives, edge.toNode.getID());
// basically unioning
if (!from.equals(to)) {
edgesInTree.add(edge);
representatives.put(from, to);
}
}
return edgesInTree;
}
// Find the representative
String find(HashMap<String, String> representatives, String id) {
if (representatives.get(id).equals(id)) {
return id;
}
String parent = find(representatives, representatives.get(id));
representatives.put(id, parent);
return parent;
}
}
// examples class
class ExamplesLightEmAll {
LightEmAll game;
// examples
GamePiece row0col0;
GamePiece row0col1;
GamePiece row0col2;
GamePiece row0col3;
GamePiece row0col4;
GamePiece row1col0;
GamePiece row1col1;
GamePiece row1col2;
GamePiece row1col3;
GamePiece row1col4;
GamePiece row2col0;
GamePiece row2col1;
GamePiece row2col2;
GamePiece row2col3;
GamePiece row2col4;
GamePiece row3col0;
GamePiece row3col1;
GamePiece row3col2;
GamePiece row3col3;
GamePiece row3col4;
GamePiece row4col0;
GamePiece row4col1;
GamePiece row4col2;
GamePiece row4col3;
GamePiece row4col4;
// columns
ArrayList<GamePiece> col0;
ArrayList<GamePiece> col1;
ArrayList<GamePiece> col2;
ArrayList<GamePiece> col3;
ArrayList<GamePiece> col4;
// board and nodes
ArrayList<ArrayList<GamePiece>> board;
ArrayList<GamePiece> nodes;
LightEmAll world;
// initConditions
void initConditions() {
world = new LightEmAll(5, 5);
row0col0 = new GamePiece(0, 0, false, false, false, true);
row0col1 = new GamePiece(0, 1, false, false, false, true);
row0col2 = new GamePiece(0, 2, false, false, false, true);
row0col3 = new GamePiece(0, 3, false, false, false, true);
row0col4 = new GamePiece(0, 4, false, false, false, true);
row1col0 = new GamePiece(1, 0, false, false, true, true);
row1col1 = new GamePiece(1, 1, false, false, true, true);
row1col2 = new GamePiece(1, 2, false, false, true, true);
row1col3 = new GamePiece(1, 3, false, false, true, true);
row1col4 = new GamePiece(1, 4, false, false, true, true);
row2col0 = new GamePiece(2, 0, false, true, true, true);
row2col1 = new GamePiece(2, 1, true, true, true, true);
row2col2 = new GamePiece(2, 2, true, true, true, true);
row2col2.addPowerStation();
row2col3 = new GamePiece(2, 3, true, true, true, true);
row2col4 = new GamePiece(2, 4, true, false, true, true);
row3col0 = new GamePiece(3, 0, false, false, true, true);
row3col1 = new GamePiece(3, 1, false, false, true, true);
row3col2 = new GamePiece(3, 2, false, false, true, true);
row3col3 = new GamePiece(3, 3, false, false, true, true);
row3col4 = new GamePiece(3, 4, false, false, true, true);
row4col0 = new GamePiece(4, 0, false, false, true, false);
row4col1 = new GamePiece(4, 1, false, false, true, false);
row4col2 = new GamePiece(4, 2, false, false, true, false);
row4col3 = new GamePiece(4, 3, false, false, true, false);
row4col4 = new GamePiece(4, 4, false, false, true, false);
// columns
col0 = new ArrayList<GamePiece>(Arrays.asList(
row0col0, row1col0, row2col0, row3col0, row4col0));
col1 = new ArrayList<GamePiece>(Arrays.asList(
row0col1, row1col1, row2col1, row3col1, row4col1));
col2 = new ArrayList<GamePiece>(Arrays.asList(
row0col2, row1col2, row2col2, row3col2, row4col2));
col3 = new ArrayList<GamePiece>(Arrays.asList(
row0col3, row1col3, row2col3, row3col3, row4col3));
col4 = new ArrayList<GamePiece>(Arrays.asList(
row0col4, row1col4, row2col4, row3col4, row4col4));
this.board = new ArrayList<ArrayList<GamePiece>>(Arrays.asList(col0, col1, col2, col3, col4));
this.nodes = new ArrayList<GamePiece>(col0);
nodes.addAll(col1);
nodes.addAll(col2);
nodes.addAll(col3);
nodes.addAll(col4);
world.board = board;
world.nodes = nodes;
world.powerRow = 2;
world.powerCol = 2;
world.updatePower();
}
// test getConnectedTiles
void testGetConnectedTiles(Tester t) {
initConditions();
GamePiece center = world.board.get(2).get(2);
ArrayList<GamePiece> connected = world.getConnectedTile(center);
t.checkExpect(connected.size(), 4);
t.checkExpect(connected.contains(world.board.get(2).get(1)), true);
t.checkExpect(connected.contains(world.board.get(2).get(3)), true);
t.checkExpect(connected.contains(world.board.get(1).get(2)), true);
t.checkExpect(connected.contains(world.board.get(3).get(2)), true);
}
// test updatePower
void testUpdatePower(Tester t) {
initConditions();
t.checkExpect(world.board.get(2).get(0).powered, true);
t.checkExpect(world.board.get(2).get(4).powered, true);
t.checkExpect(world.board.get(0).get(2).powered, true);
t.checkExpect(world.board.get(4).get(2).powered, true);
}
// test allPowered
void testAllPowered(Tester t) {
initConditions();
t.checkExpect(world.allPowered(), true);
}
// test onMouseClicked
void testOnMouseClicked(Tester t) {
initConditions();
t.checkExpect(this.world.userScore, 0);
GamePiece original = world.board.get(0).get(0);
boolean origTop = original.top;
boolean origRight = original.right;
boolean origBottom = original.bottom;
boolean origLeft = original.left;
Posn click = new Posn(5, 5);
world.onMouseClicked(click);
GamePiece newPiece = world.board.get(0).get(0);
t.checkExpect(newPiece.top, origLeft);
t.checkExpect(newPiece.right, origTop);
t.checkExpect(newPiece.bottom, origRight);
t.checkExpect(newPiece.left, origBottom);
t.checkExpect(this.world.userScore, 1);
}
// test onKeyEvent
void testonKeyEvent(Tester t) {
initConditions();
// nothing changes when key other than arrows is clicked
t.checkExpect(world.userScore, 0);
t.checkExpect(world.powerRow, 2);
t.checkExpect(world.powerCol, 2);
t.checkExpect(row2col2.powerStation, true);
this.world.onKeyEvent("t");
t.checkExpect(world.userScore, 1);
t.checkExpect(world.powerRow, 2);
t.checkExpect(world.powerCol, 2);
t.checkExpect(row2col2.powerStation, true);
// position of power station changes when arrows are clicked
t.checkExpect(world.powerRow, 2);
t.checkExpect(world.powerCol, 2);
t.checkExpect(row2col2.powerStation, true);
this.world.onKeyEvent("up");
t.checkExpect(world.userScore, 2);
t.checkExpect(world.powerRow, 1);
t.checkExpect(world.powerCol, 2);
t.checkExpect(row2col2.powerStation, false);
t.checkExpect(row1col2.powerStation, true);
// test for restart (cannot test because of randomization
// if the board were all powered, and r was clicked it would no longer be
// t.checkExpect(world.allPowered(), true);
// this.world.onKeyEvent("r");
// t.checkExpect(world.allPowered(), false);
// t.checkExpect(world.userScore, 0);
}
// test makeScene
void testMakeScene(Tester t) {
initConditions();
WorldScene scene = world.makeScene();
t.checkExpect(scene.width, 200);
t.checkExpect(scene.height, 200);
}
// test findMatchingNodes
void testFindMatchingNodes(Tester t) {
initConditions();
t.checkExpect(world.findMatchingNode(0, 0), this.row0col0);
t.checkExpect(world.findMatchingNode(4, 3), this.row4col3);
t.checkExpect(world.findMatchingNode(2,3), this.row2col3);
}
// test Rotate
void testRotate(Tester t) {
GamePiece gp = new GamePiece(0, 0, true, false, false, false);
gp.rotate();
t.checkExpect(gp.top, true);
t.checkExpect(gp.right, false);
t.checkExpect(gp.bottom, false);
t.checkExpect(gp.left, false);
}
// test movePowerStation
void testMovePowerStation(Tester t) {
initConditions();
// test init
t.checkExpect(world.powerRow, 2);
t.checkExpect(world.powerCol, 2);
t.checkExpect(this.row2col2.powerStation, true);
row2col2.movePowerStation("up", world);
// test when able to move
t.checkExpect(this.row2col2.powerStation, false);
t.checkExpect(this.row1col2.powerStation, true);
t.checkExpect(world.powerRow, 1);
t.checkExpect(world.powerCol, 2);
row1col2.movePowerStation("left", world);
// test when unable to move
t.checkExpect(this.row1col1.powerStation, false);
t.checkExpect(this.row1col2.powerStation, true);
t.checkExpect(world.powerRow, 1);
t.checkExpect(world.powerCol, 2);
}
// test addPowerStation
void testAddPowerStation(Tester t) {
initConditions();
// test for already powered init
t.checkExpect(this.row2col2.powerStation, true);
this.row2col2.addPowerStation();
// mutated
t.checkExpect(this.row2col2.powerStation, true);
// test for not powered init
t.checkExpect(this.row4col0.powerStation, false);
this.row4col0.addPowerStation();
// mutated
t.checkExpect(this.row4col0.powerStation, true);
}
// test makeEdges
void testMakeEdges(Tester t) {
LightEmAll le = new LightEmAll(3, 3);
ArrayList<Edge> edges = le.makeEdges();
t.checkExpect(edges.size(), 12);
}
// test getID
void testGetID(Tester t) {
GamePiece gp = new GamePiece(1, 1, false, false, false, false);
t.checkExpect(gp.getID(), "r1c1");
GamePiece gp0 = new GamePiece(0, 0, false, false, false, false);
t.checkExpect(gp0.getID(), "r0c0");
}
// test find
void testFind(Tester t) {
TreeUtils util = new TreeUtils();
HashMap<String, String> hm = new HashMap<>();
hm.put("a", "a");
hm.put("b", "c");
hm.put("c", "c");
String result = util.find(hm, "a");
t.checkExpect(result, "a");
String result1 = util.find(hm, "c");
t.checkExpect(result1, "c");
}
//Run game
void testRunGame(Tester t) {
LightEmAll l = new LightEmAll(6,6);
l.bigBang(300, 300, 0.1);
}
}