-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBridgit.java
More file actions
1099 lines (861 loc) · 29.9 KB
/
Bridgit.java
File metadata and controls
1099 lines (861 loc) · 29.9 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 java.util.ArrayList;
import tester.*;
import javalib.impworld.*;
import java.awt.Color;
import javalib.worldimages.*;
//Represents a tile on the game board
interface Tile {
// EFFECT: Link this Tile to the Tile above
public void linkUp(Tile t);
// EFFECT: Link this Tile to the Tile below
public void linkDown(Tile t);
// EFFECT: Link this Tile to the Tile to the left
public void linkLeft(Tile t);
// EFFECT: Link this Tile to the Tile to the right
public void linkRight(Tile t);
// EFFECT: add the links of this Tile into this links
public ArrayList<Tile> addLinks();
// EFFECT: set this Tile as the Target to win the game
public void makeTarget();
// is this Tile the target tile?
public boolean isTarget();
// is this Tile traversable from this given player's turn?
public boolean path(int turn);
}
//Represents an empty piece that is outside the board
class EmptyCell implements Tile {
// nothing to link in the EmptyCell
public void linkUp(Tile t) {
// do nothing
}
// nothing to link in the EmptyCell
public void linkDown(Tile t) {
// do nothing
}
// nothing to link in the EmptyCell
public void linkLeft(Tile t) {
// do nothing
}
// nothing to link in the EmptyCell
public void linkRight(Tile t) {
// do nothing
}
// nothing to link in the EmptyCell
public ArrayList<Tile> addLinks() {
throw new RuntimeException("EmptyCell has no links");
}
// nothing to do in the EmptyCell
public void makeTarget() {
// do nothing
}
// this is not the target
public boolean isTarget() {
return false;
}
// this is not a valid path
public boolean path(int turn) {
return false;
}
}
//represents a Cell on the game board
abstract class Cell implements Tile {
Tile up;
Tile down;
Tile left;
Tile right;
Boolean target;
Cell() {
this.up = null;
this.down = null;
this.left = null;
this.right = null;
this.target = false;
}
// EFFECT: Link this Cell to the Cell above
public void linkUp(Tile t) {
this.up = t;
}
// EFFECT: Link this Cell to the Cell below
public void linkDown(Tile t) {
this.down = t;
}
// EFFECT: Link this Cell to the Cell to the left
public void linkLeft(Tile t) {
this.left = t;
}
// EFFECT: Link this Cell to the Cell to the right
public void linkRight(Tile t) {
this.right = t;
}
// Draws this Cell onto the background
public abstract WorldImage drawAt(int col, int row, int length, WorldImage background);
// Draws this Cell onto the background with its color
public WorldImage drawAtHelper(int col, int row, int length, WorldImage background, Color color) {
Double size = background.getWidth();
WorldImage draw = new RectangleImage(size.intValue() / length, size.intValue() / length,
"solid", color);
return new OverlayOffsetAlign(AlignModeX.LEFT, AlignModeY.TOP, draw, -col * draw.getWidth(),
-row * draw.getHeight(), background);
}
// return the turn of the turn of the current player
public int click(int turn, int row, int column, ArrayList<ArrayList<Cell>> arr) {
return turn;
}
// EFFECT: add the links of this Cell into this links
public ArrayList<Tile> addLinks() {
ArrayList<Tile> arr = new ArrayList<Tile>();
arr.add(this.up);
arr.add(this.down);
arr.add(this.left);
arr.add(this.right);
return arr;
}
// EFFECT: set this Cell as the target
public void makeTarget() {
this.target = true;
}
// this is not the target
public boolean isTarget() {
return this.target;
}
// is this Cell a valid path?
public abstract boolean path(int turn);
}
//Represents an WCell
class WCell extends Cell {
Color col = Color.white;
WCell() {
super();
}
// Draws this WCell onto the background
public WorldImage drawAt(int col, int row, int length, WorldImage background) {
return this.drawAtHelper(col, row, length, background, this.col);
}
// EFFECT: remove the Cell in the given row and column and replace it with
// a RCell or a BCell depending on the player's turn
// return the turn of the next player
public int click(int turn, int row, int column, ArrayList<ArrayList<Cell>> cells) {
Cell currTile = cells.get(row).remove(column);
if (turn == 1) {
Cell newTile = new RCell();
newTile.linkUp(currTile.up);
currTile.up.linkDown(newTile);
newTile.linkDown(currTile.down);
currTile.down.linkUp(newTile);
newTile.linkLeft(currTile.left);
currTile.left.linkRight(newTile);
newTile.linkRight(currTile.right);
currTile.right.linkLeft(newTile);
cells.get(row).add(column, newTile);
return 2;
}
else {
Cell newTile = new BCell();
newTile.linkUp(currTile.up);
currTile.up.linkDown(newTile);
newTile.linkDown(currTile.down);
currTile.down.linkUp(newTile);
newTile.linkLeft(currTile.left);
currTile.left.linkRight(newTile);
newTile.linkRight(currTile.right);
currTile.right.linkLeft(newTile);
cells.get(row).add(column, newTile);
return 1;
}
}
// a WCell is not a valid path
public boolean path(int turn) {
return false;
}
}
//Represents a BCell
class BCell extends Cell {
Color col = Color.blue;
BCell() {
super();
}
// Draws this WCell onto the background
public WorldImage drawAt(int col, int row, int length, WorldImage background) {
return this.drawAtHelper(col, row, length, background, this.col);
}
// is it player's 2 turn?
public boolean path(int turn) {
return turn == 2;
}
}
//Represents a RCell
class RCell extends Cell {
Color col = Color.red;
RCell() {
super();
}
// Draws this WCell onto the background
public WorldImage drawAt(int col, int row, int length, WorldImage background) {
return this.drawAtHelper(col, row, length, background, this.col);
}
// is it player's 2 turn?
public boolean path(int turn) {
return turn == 1;
}
}
//Represents the Bridgit Game
class Bridgit extends World {
int grid;
ArrayList<ArrayList<Cell>> cells;
int turn;
// original constructor
Bridgit(int grid) {
this.grid = grid;
this.cells = new ArrayList<ArrayList<Cell>>();
this.turn = 1;
if (this.grid % 2 == 0 || this.grid < 3) {
throw new IllegalArgumentException("the number needs to be odd and greater than 3");
}
this.makeGame(this.grid);
}
// for testing
Bridgit(int grid, ArrayList<ArrayList<Cell>> cells) {
this.grid = grid;
this.cells = cells;
}
// EFFECT: make the Cells and link them together and the Cells to that
// needs to be linked to EmptyCell
public void makeGame(int grid) {
this.cells = new ArrayList<ArrayList<Cell>>();
Tile empty = new EmptyCell();
for (int i = 0; i < grid; i++) {
this.cells.add(new ArrayList<Cell>());
for (int j = 0; j < grid; j++) {
// add Cells to the even row
if (i % 2 == 0) {
// add WCells on every even column
if (j % 2 == 0) {
this.cells.get(i).add(new WCell());
}
// add RCells on every odd column
else {
this.cells.get(i).add(new RCell());
}
}
// add Cells to the odd rows
else {
// add RCells on every even column
if (j % 2 == 0) {
this.cells.get(i).add(new BCell());
}
// add WCells on every odd column
else {
this.cells.get(i).add(new WCell());
}
}
Tile currentTile = this.cells.get(i).get(j);
// link the Cells on the top row to the Empty Cell above
if (i == 0) {
currentTile.linkUp(empty);
}
// link the cells up and down
else {
Tile up = this.cells.get(i - 1).get(j);
currentTile.linkUp(up);
up.linkDown(currentTile);
}
// link the Cells on the last row to the Empty Cell below
if (i == this.grid - 1) {
currentTile.linkDown(empty);
// make every odd cell, i.e RCell, a target
if (j % 2 == 1) {
currentTile.makeTarget();
}
}
// link the Cells on the first column to the Empty Cell on the left
if (j == 0) {
currentTile.linkLeft(empty);
}
// link the Cells left and right
else {
Tile left = this.cells.get(i).get(j - 1);
currentTile.linkLeft(left);
left.linkRight(currentTile);
}
// link the Cells on the last column to the Empty Cell on the right
if (j == this.grid - 1) {
currentTile.linkRight(empty);
// make every odd cell, i.e BCell, a target
if (i % 2 == 1) {
currentTile.makeTarget();
}
}
}
}
}
// draws the game and tells which player's turn it is
public WorldScene makeScene() {
WorldScene scene = this.getEmptyScene();
int edge = scene.height / this.grid;
int edge2 = edge / 2;
int edge4 = edge / 4;
WorldImage board = new RectangleImage(scene.height, scene.width, "solid", Color.white);
for (int i = 0; i < this.grid; i++) {
for (int j = 0; j < this.grid; j++) {
board = this.cells.get(i).get(j).drawAt(j, i, this.grid, board);
}
}
if (this.turn == 1) {
board = new OverlayOffsetAlign(AlignModeX.LEFT, AlignModeY.TOP,
new TextImage("P1", edge2, Color.red), -edge4, -edge4, board);
}
else {
board = new OverlayOffsetAlign(AlignModeX.LEFT, AlignModeY.TOP,
new TextImage("P2", edge2, Color.blue), -edge4, -edge4, board);
}
scene.placeImageXY(board, scene.width / 2, scene.height / 2);
return scene;
}
// draws the game with a given canvas width and height and tell which player's
// turn it is
// I'm doing this because makeScene() is dependent on the canvas size that
// bigbang provides so if we don't initialize bigbang
public WorldScene makeSceneTest(int width, int height) {
WorldScene scene = new WorldScene(width, height);
int edge = scene.height / this.grid;
int edge2 = edge / 2;
int edge4 = edge / 4;
WorldImage board = new RectangleImage(scene.height, scene.width, "solid", Color.white);
for (int i = 0; i < this.cells.size(); i++) {
for (int j = 0; j < this.cells.get(0).size(); j++) {
board = this.cells.get(i).get(j).drawAt(j, i, this.grid, board);
}
}
if (this.turn == 1) {
board = new OverlayOffsetAlign(AlignModeX.LEFT, AlignModeY.TOP,
new TextImage("P1", edge2, Color.red), -edge4, -edge4, board);
}
else {
board = new OverlayOffsetAlign(AlignModeX.LEFT, AlignModeY.TOP,
new TextImage("P2", edge2, Color.blue), -edge4, -edge4, board);
}
scene.placeImageXY(board, scene.width / 2, scene.height / 2);
return scene;
}
// EFFECT: if a valid Cell is clicked, make the WCell become a BCell or
// RCell depending on the player's turn, update the turn, and end the game if
// a player has won
public void onMouseClicked(Posn pos) {
WorldScene scene = this.getEmptyScene();
int edge = scene.height / this.grid;
int edgeRow = pos.y / edge;
int edgeColumn = pos.x / edge;
int newTurn = this.turn;
// checks for valid clicks
if (edgeColumn > 0 && edgeRow > 0 && edgeColumn < this.grid - 1 && edgeRow < this.grid - 1) {
Cell clicked = this.cells.get(edgeRow).get(edgeColumn);
newTurn = clicked.click(this.turn, edgeRow, edgeColumn, this.cells);
ArrayList<Tile> worklist = new ArrayList<Tile>();
// player's 1 turn
if (this.turn == 0) {
for (int i = 1; i < this.grid; i += 2) {
Tile addTile = this.cells.get(0).get(i);
worklist.add(addTile);
}
}
// player's 2 turn
else {
for (int i = 1; i < this.grid; i += 2) {
Tile addTile = this.cells.get(i).get(0);
worklist.add(addTile);
}
}
if (this.checkWin(worklist, this.turn)) {
this.endOfWorld("Player " + this.turn + " has won");
}
}
this.turn = newTurn;
}
// EFFECT: if a valid Cell is clicked, make the WCell become a BCell or
// RCell depending on the player's turn, update the turn, and end the game if
// a player has won
// This is made for testing since onMouseClicked the height and width of the
// board is dependent on big bang
public void onMouseClickedTest(Posn pos, int height, int column) {
WorldScene scene = new WorldScene(height, column);
int edge = scene.height / this.grid;
int edgeRow = pos.y / edge;
int edgeColumn = pos.x / edge;
int newTurn = this.turn;
// checks for valid clicks
if (edgeColumn > 0 && edgeRow > 0 && edgeColumn < this.grid - 1 && edgeRow < this.grid - 1) {
Cell clicked = this.cells.get(edgeRow).get(edgeColumn);
newTurn = clicked.click(this.turn, edgeRow, edgeColumn, this.cells);
ArrayList<Tile> worklist = new ArrayList<Tile>();
// player's 1 turn
if (this.turn == 1) {
for (int i = 1; i < this.grid; i += 2) {
Tile addTile = this.cells.get(0).get(i);
worklist.add(addTile);
}
}
// player's 2 turn
else {
for (int i = 1; i < this.grid; i += 2) {
Tile addTile = this.cells.get(i).get(0);
worklist.add(addTile);
}
}
if (this.checkWin(worklist, this.turn)) {
this.endOfWorld("Player " + this.turn + " has won");
}
}
this.turn = newTurn;
}
// determines if a player has won by checking if there is a path
// from any source in the given worklist to the target using
// depth first search
public boolean checkWin(ArrayList<Tile> worklist, int turn) {
ArrayList<Tile> seenList = new ArrayList<Tile>();
while (worklist.size() > 0) {
Tile next = worklist.remove(0);
ArrayList<Tile> nextLinks = next.addLinks();
if (next.isTarget()) {
return true;
}
else if (seenList.contains(next)) {
// do nothing
}
else {
for (Tile t : nextLinks) {
if (t.path(turn)) {
worklist.add(0, t);
}
}
seenList.add(next);
}
}
return false;
}
// EFFECT: reset the cells to its original state and make the turn 1, i.e,
// resetting the game
public void onKeyEvent(String s) {
if (s.equals("r")) {
this.makeGame(this.grid);
this.turn = 1;
}
}
// produce the lastScene of the game with a given message
public WorldScene lastScene(String s) {
WorldScene scene = this.makeScene();
scene.placeImageXY(new TextImage(s, 30, Color.BLACK), scene.height / 2, scene.width / 2);
return scene;
}
}
class ExamplesBridgit {
Tile emptyCell;
Cell wCell;
Cell rCell;
Cell bCell;
Cell wCell2;
Cell wCell3;
Cell wCell4;
Cell wCell5;
Cell rCell2;
Cell rCell3;
Cell rCell4;
Cell rCell5;
Cell bCell2;
Cell bCell3;
Cell bCell4;
Cell bCell5;
ArrayList<ArrayList<Cell>> cells;
Bridgit b1;
Bridgit b2;
void init() {
this.emptyCell = new EmptyCell();
// for 3 by 3
this.wCell = new WCell();
this.rCell = new RCell();
this.bCell = new BCell();
this.wCell2 = new WCell();
this.wCell3 = new WCell();
this.wCell4 = new WCell();
this.wCell5 = new WCell();
this.rCell2 = new RCell();
this.rCell3 = new RCell();
this.rCell4 = new RCell();
this.rCell5 = new RCell();
this.bCell2 = new BCell();
this.bCell3 = new BCell();
this.bCell4 = new BCell();
this.bCell5 = new BCell();
ArrayList<Cell> row1 = new ArrayList<Cell>();
row1.add(this.wCell);
row1.add(this.rCell);
row1.add(this.wCell2);
ArrayList<Cell> row2 = new ArrayList<Cell>();
row2.add(this.bCell);
row2.add(this.wCell3);
row2.add(this.bCell2);
ArrayList<Cell> row3 = new ArrayList<Cell>();
row3.add(this.wCell4);
row3.add(this.rCell2);
row3.add(this.wCell5);
this.cells = new ArrayList<ArrayList<Cell>>();
cells.add(row1);
cells.add(row2);
cells.add(row3);
this.bCell2.makeTarget();
this.rCell2.makeTarget();
this.wCell.linkUp(this.emptyCell);
this.wCell.linkDown(this.bCell);
this.wCell.linkLeft(this.emptyCell);
this.wCell.linkRight(this.rCell);
this.rCell.linkUp(this.emptyCell);
this.rCell.linkDown(this.wCell3);
this.rCell.linkLeft(this.wCell);
this.rCell.linkRight(this.wCell2);
this.wCell2.linkUp(this.emptyCell);
this.wCell2.linkDown(this.bCell2);
this.wCell2.linkLeft(this.rCell);
this.wCell2.linkRight(this.emptyCell);
this.bCell.linkUp(this.wCell);
this.bCell.linkDown(this.wCell4);
this.bCell.linkLeft(this.emptyCell);
this.bCell.linkRight(this.wCell3);
this.wCell3.linkUp(this.rCell);
this.wCell3.linkDown(this.rCell2);
this.wCell3.linkLeft(this.bCell);
this.wCell3.linkRight(this.bCell2);
this.bCell2.linkUp(this.wCell2);
this.bCell2.linkDown(this.wCell5);
this.bCell2.linkLeft(this.wCell3);
this.bCell2.linkRight(this.emptyCell);
this.wCell4.linkUp(this.bCell);
this.wCell4.linkDown(this.emptyCell);
this.wCell4.linkLeft(this.emptyCell);
this.wCell4.linkRight(this.rCell2);
this.rCell2.linkUp(this.wCell3);
this.rCell2.linkDown(this.emptyCell);
this.rCell2.linkLeft(this.wCell4);
this.rCell2.linkRight(this.wCell5);
this.wCell5.linkUp(this.bCell2);
this.wCell5.linkDown(this.emptyCell);
this.wCell5.linkLeft(this.rCell2);
this.wCell5.linkRight(this.emptyCell);
this.b1 = new Bridgit(3);
this.b2 = new Bridgit(11);
}
// testing constructor exceptions
void testConstructor(Tester t) {
t.checkConstructorException(
new IllegalArgumentException("the number needs to be odd and greater than 3"), "Bridgit",
1);
t.checkConstructorException(
new IllegalArgumentException("the number needs to be odd and greater than 3"), "Bridgit",
2);
t.checkConstructorException(
new IllegalArgumentException("the number needs to be odd and greater than 3"), "Bridgit",
4);
}
void testGame(Tester t) {
this.init();
Bridgit g = this.b2;
g.bigBang(500, 500);
}
// testing linkUp method
void testLinkUp(Tester t) {
this.init();
// testing the emptyCell
this.emptyCell.linkUp(emptyCell);
t.checkExpect(this.emptyCell, new EmptyCell());
// testing the wCell
this.wCell.linkUp(emptyCell);
t.checkExpect(this.wCell.up, new EmptyCell());
this.wCell.linkUp(this.rCell);
t.checkExpect(this.wCell.up, this.rCell);
// testing the rCell
this.rCell.linkUp(this.rCell);
t.checkExpect(this.rCell.up, this.rCell);
this.rCell.linkUp(emptyCell);
t.checkExpect(this.rCell.up, emptyCell);
// testing the bCell
this.bCell.linkUp(emptyCell);
t.checkExpect(this.bCell.up, new EmptyCell());
this.bCell.linkUp(this.bCell);
t.checkExpect(this.bCell.up, this.bCell);
}
// testing linkDown method
void testLinkDown(Tester t) {
this.init();
// testing the emptyCell
this.emptyCell.linkDown(this.rCell);
t.checkExpect(this.emptyCell, new EmptyCell());
// testing the wCell
this.wCell.linkDown(emptyCell);
t.checkExpect(this.wCell.down, new EmptyCell());
this.wCell.linkDown(this.rCell);
t.checkExpect(this.wCell.down, this.rCell);
// testing the rCell
this.rCell.linkDown(this.rCell);
t.checkExpect(this.rCell.down, this.rCell);
this.rCell.linkDown(emptyCell);
t.checkExpect(this.rCell.down, emptyCell);
// testing the bCell
this.bCell.linkDown(emptyCell);
t.checkExpect(this.bCell.down, new EmptyCell());
this.bCell.linkDown(this.bCell);
t.checkExpect(this.bCell.down, this.bCell);
}
// testing linkLeft method
void testLinkLeft(Tester t) {
this.init();
// testing the emptyCell
this.emptyCell.linkUp(this.wCell);
t.checkExpect(this.emptyCell, new EmptyCell());
// testing the wCell
this.wCell.linkLeft(emptyCell);
t.checkExpect(this.wCell.left, new EmptyCell());
this.wCell.linkLeft(this.rCell);
t.checkExpect(this.wCell.left, this.rCell);
// testing the rCell
this.rCell.linkLeft(this.rCell);
t.checkExpect(this.rCell.left, this.rCell);
this.rCell.linkLeft(emptyCell);
t.checkExpect(this.rCell.left, emptyCell);
// testing the bCell
this.bCell.linkLeft(emptyCell);
t.checkExpect(this.bCell.left, new EmptyCell());
this.bCell.linkLeft(this.bCell);
t.checkExpect(this.bCell.left, this.bCell);
}
// testing linkRight method
void testLinkRight(Tester t) {
this.init();
// testing the emptyCell
this.emptyCell.linkRight(this.bCell);
t.checkExpect(this.emptyCell, new EmptyCell());
// testing the wCell
this.wCell.linkRight(emptyCell);
t.checkExpect(this.wCell.right, new EmptyCell());
this.wCell.linkRight(this.rCell);
t.checkExpect(this.wCell.right, this.rCell);
// testing the rCell
this.rCell.linkRight(this.rCell);
t.checkExpect(this.rCell.right, this.rCell);
this.rCell.linkRight(emptyCell);
t.checkExpect(this.rCell.right, emptyCell);
// testing the bCell
this.bCell.linkRight(emptyCell);
t.checkExpect(this.bCell.right, new EmptyCell());
this.bCell.linkRight(this.bCell);
t.checkExpect(this.bCell.right, this.bCell);
}
// testing drawAt method
void testDrawAt(Tester t) {
this.init();
WorldImage board = new RectangleImage(100, 100, "solid", Color.white);
t.checkExpect(this.wCell.drawAt(0, 0, 4, board), new OverlayOffsetAlign(AlignModeX.LEFT,
AlignModeY.TOP, new RectangleImage(25, 25, "solid", Color.white), 0, 0, board));
t.checkExpect(this.rCell.drawAt(0, 0, 4, board), new OverlayOffsetAlign(AlignModeX.LEFT,
AlignModeY.TOP, new RectangleImage(25, 25, "solid", Color.red), 0, 0, board));
t.checkExpect(this.bCell.drawAt(0, 0, 4, board), new OverlayOffsetAlign(AlignModeX.LEFT,
AlignModeY.TOP, new RectangleImage(25, 25, "solid", Color.blue), 0, 0, board));
}
// testing drawAtHelper method
void testDrawAtHelper(Tester t) {
this.init();
WorldImage board = new RectangleImage(100, 100, "solid", Color.white);
t.checkExpect(this.wCell.drawAtHelper(0, 0, 4, board, Color.white),
new OverlayOffsetAlign(AlignModeX.LEFT, AlignModeY.TOP,
new RectangleImage(25, 25, "solid", Color.white), 0, 0, board));
t.checkExpect(this.rCell.drawAtHelper(0, 0, 4, board, Color.red),
new OverlayOffsetAlign(AlignModeX.LEFT, AlignModeY.TOP,
new RectangleImage(25, 25, "solid", Color.red), 0, 0, board));
t.checkExpect(this.bCell.drawAtHelper(0, 0, 4, board, Color.blue),
new OverlayOffsetAlign(AlignModeX.LEFT, AlignModeY.TOP,
new RectangleImage(25, 25, "solid", Color.blue), 0, 0, board));
}
// testing makeGame method
void testMakeGame(Tester t) {
this.init();
t.checkExpect(this.b1.cells, cells);
}
// testing makeSceneTest method
void testMakeSceneTest(Tester t) {
this.init();
ArrayList<Cell> row1 = new ArrayList<Cell>();
row1.add(this.wCell);
row1.add(this.rCell);
row1.add(this.bCell);
ArrayList<ArrayList<Cell>> cells = new ArrayList<ArrayList<Cell>>();
cells.add(row1);
Bridgit game1 = new Bridgit(3, cells);
WorldScene scene = new WorldScene(400, 400);
WorldImage board = new RectangleImage(scene.height, scene.width, "solid", Color.white);
int edge = scene.height / game1.grid;
int edge2 = edge / 2;
int edge4 = edge / 4;
WorldImage rCell = new RectangleImage(133, 133, "solid", Color.red);
WorldImage bCell = new RectangleImage(133, 133, "solid", Color.blue);
WorldImage wCell = new RectangleImage(133, 133, "solid", Color.white);
board = new OverlayOffsetAlign(AlignModeX.LEFT, AlignModeY.TOP, wCell, 0 * wCell.getWidth(),
0 * wCell.getHeight(), board);
board = new OverlayOffsetAlign(AlignModeX.LEFT, AlignModeY.TOP, rCell, -1 * rCell.getWidth(),
0 * rCell.getHeight(), board);
board = new OverlayOffsetAlign(AlignModeX.LEFT, AlignModeY.TOP, bCell, -2 * bCell.getWidth(),
0 * bCell.getHeight(), board);
board = new OverlayOffsetAlign(AlignModeX.LEFT, AlignModeY.TOP,
new TextImage("P2", edge2, Color.blue), -edge4, -edge4, board);
scene.placeImageXY(board, 200, 200);
t.checkExpect(game1.makeSceneTest(400, 400), scene);
}
// testing onMouseClickTest method
void testOnMouseClickTest(Tester t) {
this.init();
// clicking on the edges doesn't do anything
this.b1.onMouseClickedTest(new Posn(5, 5), 30, 30);
this.b1.onMouseClickedTest(new Posn(5, 15), 30, 30);
this.b1.onMouseClickedTest(new Posn(5, 25), 30, 30);
this.b1.onMouseClickedTest(new Posn(15, 5), 30, 30);
this.b1.onMouseClickedTest(new Posn(25, 5), 30, 30);
this.b1.onMouseClickedTest(new Posn(25, 15), 30, 30);
this.b1.onMouseClickedTest(new Posn(25, 25), 30, 30);
this.b1.onMouseClickedTest(new Posn(15, 25), 30, 30);
t.checkExpect(this.b1.cells, this.cells);
// the middle tile is a WCell so clicking on it will change it to
// a RCell since it's player 1's turn
this.init();
this.b1.onMouseClickedTest(new Posn(15, 15), 30, 30);
Cell changeTile = this.cells.get(1).remove(1);
RCell newRCell = new RCell();
newRCell.linkDown(changeTile.down);
changeTile.down.linkUp(newRCell);
newRCell.linkUp(changeTile.up);
changeTile.up.linkDown(newRCell);
newRCell.linkLeft(changeTile.left);
changeTile.left.linkRight(newRCell);
newRCell.linkRight(changeTile.right);
changeTile.right.linkLeft(newRCell);
this.cells.get(1).add(1, newRCell);
t.checkExpect(this.b1.cells, this.cells);
}
// testing onKeyEvent method
void testOnKeyEvent(Tester t) {
this.init();
// clicking on a random key does nothing
this.b1.onKeyEvent("a");
t.checkExpect(this.b1.cells, this.cells);
// clicking on a random key does nothing
this.b1.onKeyEvent("b");
t.checkExpect(this.b1.cells, this.cells);
// resets the game
this.b1.cells.get(1).get(1).click(0, 1, 1, this.b1.cells);
this.b1.onKeyEvent("r");
t.checkExpect(this.b1.cells, this.cells);
}
// testing click method
void testClick(Tester t) {
this.init();
t.checkExpect(this.wCell.click(0, 1, 1, this.cells), 1);
t.checkExpect(this.rCell.click(0, 1, 1, this.cells), 0);
t.checkExpect(this.bCell.click(1, 1, 1, this.cells), 1);
}
// testing addLinks method
void testAddLinks(Tester t) {
this.init();
t.checkException(new RuntimeException("EmptyCell has no links"), this.emptyCell, "addLinks");
ArrayList<Tile> arr = new ArrayList<Tile>();
arr.add(this.wCell.up);
arr.add(this.wCell.down);
arr.add(this.wCell.left);
arr.add(this.wCell.right);
t.checkExpect(this.wCell.addLinks(), arr);
ArrayList<Tile> arr2 = new ArrayList<Tile>();
arr2.add(this.bCell.up);
arr2.add(this.bCell.down);
arr2.add(this.bCell.left);
arr2.add(this.bCell.right);
t.checkExpect(this.bCell.addLinks(), arr2);
ArrayList<Tile> arr3 = new ArrayList<Tile>();
arr3.add(this.rCell.up);
arr3.add(this.rCell.down);
arr3.add(this.rCell.left);
arr3.add(this.rCell.right);
t.checkExpect(this.rCell.addLinks(), arr3);
}
// testing checkWin method
void testCheckWin(Tester t) {
this.init();
ArrayList<Tile> arr = new ArrayList<Tile>();
arr.add(this.rCell);