Repository navigation
Expand file tree
/
Copy pathChessPieceTester.java
More file actions
1561 lines (1439 loc) · 92.6 KB
/
Copy pathChessPieceTester.java
File metadata and controls
1561 lines (1439 loc) · 92.6 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 org.junit.Test;
import static org.junit.Assert.*;
/**
*
* A class which tests various methods from the ChessPiece hierarchy (note that this class tests all methods in Project 3
* and all methods from the ChessPiece hierarchy in Project 5)
* IMPORTANT NOTE TO THE READER: All direction/cardinality specifications (e.g. left, north-west, etc.) are from the perspective of a person
* looking at the board from the bottom (the last row, which would be the south side) to the top (the first row, which would be the north side)
* Secondary Note: Since the method moveDone() does nothing by design, it is not tested in this unit tester
* @author Peter Schlueter
*
*/
public class ChessPieceTester
{
//EuropeanChess gameRules stores the basic rules for this chess game (EuropeanChess)
EuropeanChess gameRules = new EuropeanChess();
//Xiangqi xiangqiGameRules stores the basic rules for a game of Xiangqi
Xiangqi xiangqiGameRules = new Xiangqi();
//EuropeanChessDisplay boardDisplay stores a chess board display instance
SwingEuropeanChessDisplay boardDisplay = new SwingEuropeanChessDisplay();
//SwingChessBoard board stores the chess game board
//Note: This is the typical size of a chess board, being 8 squares x 8 squares
SwingChessBoard board = new SwingChessBoard(boardDisplay, gameRules);
//SwingChessBoard xiangqiBoard stores a chess game board for Xiangqi
//Since this is used for the purpose of testing the pieces rather than the display, a SwingEuropeanChessDisplay is still used for the display of the board here
SwingChessBoard xiangqiBoard = new SwingChessBoard(boardDisplay, xiangqiGameRules);
/**
* Tests all getters and setters in ChessPiece, which are listed below
* 1.) setHasMoved(boolean hasMoved) and getHasMoved()
* 2.) setLocation(int row, int column), getRow(), and getColumn()
* 3.) setToBeRemoved(boolean toBeRemoved) and getToBeRemoved()
* 4.) setToBeUpgraded(boolean toBeUpgraded) and getToBeUpgraded()
* 5.) getChessBoard()
* 6.) getIcon()
* 7.) getLabel()
* 8.) getSide()
*/
@Test
public void testGettersAndSetters()
{
//ChessPiece testPiece stores a chess piece which is used for testing the getter/setter methods of chess piece
ChessPiece testPiece = new KnightPiece(ChessGame.Side.NORTH, 0, 0, board);
/* Setters are called in this section, tested via calling the getter methods which should return the values set by the setter methods here */
//Should set the value for whether or not the piece has moved this game
testPiece.setHasMoved(true);
//Should set the row and collumn of the piece to the specified value for each
testPiece.setLocation(1, 1);
//Should set the value for whether or not the piece should be removed
testPiece.setToBeRemoved(true);
//Should set the value for wheter or not the piece should be upgraded
testPiece.setToBeUpgraded(true);
/* Getters tested in this section */
//Should get the board the piece is on set by the contructor
assertEquals(board, testPiece.getChessBoard());
//Should get the column the piece was set to be on by setLocation()
assertEquals(1, testPiece.getColumn());
//Should get the value set by setHasMoved() for whether or not the piece has moved this game
assertEquals(true, testPiece.getHasMoved());
//Should get the icon of the piece set by the constructor
assertEquals(null, testPiece.getIcon());
//Should get the label of the piece set by the constructor
assertEquals("N", testPiece.getLabel());
//Should get the row the piece was set to be on by setLocation()
assertEquals(1, testPiece.getRow());
//Should get the side of the piece set by the contructor
assertEquals(ChessGame.Side.NORTH, testPiece.getSide());
//Sould get the value set by setToBeRemoved() for whether or not the piece should be removed
assertEquals(true, testPiece.getToBeRemoved());
//Should get the value set by setToBeUpgraded() for whether or not the piece should be upgraded
assertEquals(true, testPiece.getToBeUpgraded());
}
/**
* Tests the contructor for ChessPiece
*/
@Test
public void testChessPiece()
{
//ChessPiece testPiece stores a chess piece which is used for testing the ChessPiece constructor
//It is worth noting that since all classes extending ChessPiece call its constructor in their constructors,
//all subclasses of ChessPiece will properly test the ChessPiece constructor
ChessPiece testPiece = new KnightPiece(ChessGame.Side.SOUTH, 7, 1, board);
//Should get the piece's side, which is set to SOUTH for testPiece
assertEquals(ChessGame.Side.SOUTH, testPiece.getSide());
//Should get the piece's label, which is set to "N" for a KnightPiece instance of a ChessPiece
assertEquals("N", testPiece.getLabel());
//Should get the piece's icon, which is set to null for a KnightPiece instance of a ChessPiece
assertEquals(null, testPiece.getIcon());
//Should get the piece's row, which is set to 7 for testPiece
assertEquals(7, testPiece.getRow());
//Should get the piece's column, which is set to 1 for testPiece
assertEquals(1, testPiece.getColumn());
//Should get the board the piece is on, which is set to board for testPiece
assertEquals(board, testPiece.getChessBoard());
}
/**
* Tests the contructor for a KnightPiece
*/
@Test
public void testKnightPiece()
{
//ChessPiece testPiece stores a chess piece which is used for testing the KnightPiece constructor
ChessPiece testPiece = new KnightPiece(ChessGame.Side.SOUTH, 7, 1, board);
//Should get the piece's side, which is set to SOUTH for testPiece
assertEquals(ChessGame.Side.SOUTH, testPiece.getSide());
//Should get the piece's label, which is set to "N" for a KnightPiece instance of a ChessPiece
assertEquals("N", testPiece.getLabel());
//Should get the piece's icon, which is set to null for a KnightPiece instance of a ChessPiece
assertEquals(null, testPiece.getIcon());
//Should get the piece's row, which is set to 7 for testPiece
assertEquals(7, testPiece.getRow());
//Should get the piece's column, which is set to 1 for testPiece
assertEquals(1, testPiece.getColumn());
//Should get the board the piece is on, which is set to board for testPiece
assertEquals(board, testPiece.getChessBoard());
}
/**
* Tests the contructor for a RookPiece
*/
@Test
public void testRookPiece()
{
//ChessPiece testPiece stores a chess piece which is used for testing the RookPiece constructor
ChessPiece testPiece = new RookPiece(ChessGame.Side.SOUTH, 7, 1, board);
//Should get the piece's side, which is set to SOUTH for testPiece
assertEquals(ChessGame.Side.SOUTH, testPiece.getSide());
//Should get the piece's label, which is set to "R" for a RookPiece instance of a ChessPiece
assertEquals("R", testPiece.getLabel());
//Should get the piece's icon, which is set to null for a RookPiece instance of a ChessPiece
assertEquals(null, testPiece.getIcon());
//Should get the piece's row, which is set to 7 for testPiece
assertEquals(7, testPiece.getRow());
//Should get the piece's column, which is set to 1 for testPiece
assertEquals(1, testPiece.getColumn());
//Should get the board the piece is on, which is set to board for testPiece
assertEquals(board, testPiece.getChessBoard());
}
/**
* Tests the contructor for a BishopPiece
*/
@Test
public void testBishopPiece()
{
//ChessPiece testPiece stores a chess piece which is used for testing the BishopPiece constructor
ChessPiece testPiece = new BishopPiece(ChessGame.Side.SOUTH, 7, 1, board);
//Should get the piece's side, which is set to SOUTH for testPiece
assertEquals(ChessGame.Side.SOUTH, testPiece.getSide());
//Should get the piece's label, which is set to "B" for a BishopPiece instance of a ChessPiece
assertEquals("B", testPiece.getLabel());
//Should get the piece's icon, which is set to null for a BishopPiece instance of a ChessPiece
assertEquals(null, testPiece.getIcon());
//Should get the piece's row, which is set to 7 for testPiece
assertEquals(7, testPiece.getRow());
//Should get the piece's column, which is set to 1 for testPiece
assertEquals(1, testPiece.getColumn());
//Should get the board the piece is on, which is set to board for testPiece
assertEquals(board, testPiece.getChessBoard());
}
/**
* Tests the contructor for a QueenPiece
*/
@Test
public void testQueenPiece()
{
//ChessPiece testPiece stores a chess piece which is used for testing the QueenPiece constructor
ChessPiece testPiece = new QueenPiece(ChessGame.Side.SOUTH, 7, 1, board);
//Should get the piece's side, which is set to SOUTH for testPiece
assertEquals(ChessGame.Side.SOUTH, testPiece.getSide());
//Should get the piece's label, which is set to "Q" for a QueenPiece instance of a ChessPiece
assertEquals("Q", testPiece.getLabel());
//Should get the piece's icon, which is set to null for a QueenPiece instance of a ChessPiece
assertEquals(null, testPiece.getIcon());
//Should get the piece's row, which is set to 7 for testPiece
assertEquals(7, testPiece.getRow());
//Should get the piece's column, which is set to 1 for testPiece
assertEquals(1, testPiece.getColumn());
//Should get the board the piece is on, which is set to board for testPiece
assertEquals(board, testPiece.getChessBoard());
}
/**
* Tests the contructor for a KingPiece
*/
@Test
public void testKingPiece()
{
//ChessPiece testPiece stores a chess piece which is used for testing the KingPiece constructor
ChessPiece testPiece = new KingPiece(ChessGame.Side.SOUTH, 7, 1, board);
//Should get the piece's side, which is set to SOUTH for testPiece
assertEquals(ChessGame.Side.SOUTH, testPiece.getSide());
//Should get the piece's label, which is set to "K" for a KingPiece instance of a ChessPiece
assertEquals("K", testPiece.getLabel());
//Should get the piece's icon, which is set to null for a KingPiece instance of a ChessPiece
assertEquals(null, testPiece.getIcon());
//Should get the piece's row, which is set to 7 for testPiece
assertEquals(7, testPiece.getRow());
//Should get the piece's column, which is set to 1 for testPiece
assertEquals(1, testPiece.getColumn());
//Should get the board the piece is on, which is set to board for testPiece
assertEquals(board, testPiece.getChessBoard());
}
/**
* Tests the contructor for a PawnPiece
*/
@Test
public void testPawnPiece()
{
//ChessPiece testPiece stores a chess piece which is used for testing the PawnPiece constructor
ChessPiece testPiece = new PawnPiece(ChessGame.Side.SOUTH, 7, 1, board);
//Should get the piece's side, which is set to SOUTH for testPiece
assertEquals(ChessGame.Side.SOUTH, testPiece.getSide());
//Should get the piece's label, which is set to "P" for a PawnPiece instance of a ChessPiece
assertEquals("P", testPiece.getLabel());
//Should get the piece's icon, which is set to null for a PawnPiece instance of a ChessPiece
assertEquals(null, testPiece.getIcon());
//Should get the piece's row, which is set to 7 for testPiece
assertEquals(7, testPiece.getRow());
//Should get the piece's column, which is set to 1 for testPiece
assertEquals(1, testPiece.getColumn());
//Should get the board the piece is on, which is set to board for testPiece
assertEquals(board, testPiece.getChessBoard());
}
/**
* Tests isLMove(int toRow, int toColumn, ChessPiece piece) from the interface LMove
*/
@Test
public void testIsLMove()
{
//ChessPiece testPiece stores a chess piece used for testing this method
KnightPiece testPiece = new KnightPiece(ChessGame.Side.NORTH, 4, 4, board);
//The following tests should return true if the input location is up/down two spaces, over one space
assertTrue(testPiece.isLMove(6, 5, testPiece));
assertTrue(testPiece.isLMove(6, 3, testPiece));
assertTrue(testPiece.isLMove(2, 5, testPiece));
assertTrue(testPiece.isLMove(2, 3, testPiece));
//The following tests should return true if the input location is up/down one space, over two spaces
assertTrue(testPiece.isLMove(5, 6, testPiece));
assertTrue(testPiece.isLMove(5, 2, testPiece));
assertTrue(testPiece.isLMove(3, 6, testPiece));
assertTrue(testPiece.isLMove(3, 2, testPiece));
//This test should return false since the input location is not a valid L-Move
assertFalse(testPiece.isLMove(0, 0, testPiece));
}
/**
* Tests isVerticalMove(int toRow, toColumn, ChessPiece piece) from VerticalMove interface
*/
@Test
public void testIsVerticalMove()
{
//RookPiece testUpward stores a RookPiece used for testing general cases and cases where the piece attempts to move upward
RookPiece testUpward = new RookPiece(ChessGame.Side.NORTH, 4, 4, board);
//RookPiece testDownward stores a RookPiece used for testing cases where the piece attempts to move downward
RookPiece testDownward = new RookPiece(ChessGame.Side.NORTH, 3, 3, board);
//KnightPiece downHitDetection will store a KnightPiece used for testing that a vertical move is not legal if there is a piece between the input location and the current location
KnightPiece downHitDetection = new KnightPiece(ChessGame.Side.NORTH, 1, 4, board);
//Must add the piece to the board as otherwise all spaces on the board would be null
testUpward.getChessBoard().addPiece(downHitDetection, 1, 4);
//KnightPiece upHitDetection will store a KnightPiece used for testing that a vertical move is not legal if there is a piece between the input location and the current location
KnightPiece upHitDetection = new KnightPiece(ChessGame.Side.NORTH, 6, 3, board);
//Must add the piece to the board as otherwise all spaces on the board would be null
testDownward.getChessBoard().addPiece(upHitDetection, 6, 3);
//Is not a vertical move if the current column and input column do not match
assertFalse(testUpward.isVerticalMove(0, 0, testUpward));
//Is not a vertical move if the input position is the same as the current one
assertFalse(testUpward.isVerticalMove(4, 4, testUpward));
//Is a vertical move if the piece moves one space in the upward direction
assertTrue(testUpward.isVerticalMove(3, 4, testUpward));
//Is a vertical move if the piece moves multiple spaces in the upward direction
assertTrue(testUpward.isVerticalMove(2, 4, testUpward));
//Is not a vertical move if there is a piece between the inputted column and the one the piece is on
assertFalse(testUpward.isVerticalMove(0, 4, testUpward));
//Is a vertical move if the piece moves one space in the downward direction
assertTrue(testDownward.isVerticalMove(4, 3, testDownward));
//Is a vertical move if the piece moves multiple spaces in the downward direction
assertTrue(testDownward.isVerticalMove(5, 3, testDownward));
//Is not a vertical move if there is a piece between the inputted column and the one the piece is on
assertFalse(testDownward.isVerticalMove(7, 3, testDownward));
}
/**
* Tests isHorizontalMove(int toRow, toColumn, ChessPiece piece) from HorizontalMove interface
*/
@Test
public void testIsHorizontalMove()
{
//RookPiece testLeftward stores a RookPiece used for testing general cases and cases where the piece attempts to move leftward
RookPiece testLeftward = new RookPiece(ChessGame.Side.NORTH, 4, 4, board);
//RookPiece testRight stores a RookPiece used for testing cases where the piece attempts to move rightward
RookPiece testRightward = new RookPiece(ChessGame.Side.NORTH, 3, 3, board);
//KnightPiece leftHitDetection will store a KnightPiece used for testing that a horizontal move is not legal if there is a piece between the input location and the current location
KnightPiece leftHitDetection = new KnightPiece(ChessGame.Side.NORTH, 4, 1, board);
//Must add the piece to the board as otherwise all spaces on the board would be null
testLeftward.getChessBoard().addPiece(leftHitDetection, 4, 1);
//KnightPiece rightHitDetection will store a KnightPiece used for testing that a horizontal move is not legal if there is a piece between the input location and the current location
KnightPiece rightHitDetection = new KnightPiece(ChessGame.Side.NORTH, 3, 6, board);
//Must add the piece to the board as otherwise all spaces on the board would be null
testRightward.getChessBoard().addPiece(rightHitDetection, 3, 6);
//Is not a horizontal move if the current row and input row do not match
assertFalse(testLeftward.isHorizontalMove(0, 0, testLeftward));
//Is not a horizontal move if the input position is the same as the current one
assertFalse(testLeftward.isHorizontalMove(4, 4, testLeftward));
//Is a horizontal move if the piece moves one space in the leftward direction
assertTrue(testLeftward.isHorizontalMove(4, 3, testLeftward));
//Is a horizontal move if the piece moves multiple spaces in the leftward direction
assertTrue(testLeftward.isHorizontalMove(4, 2, testLeftward));
//Is not a horizontal move if there is a piece between the inputted row and the one the piece is on
assertFalse(testLeftward.isHorizontalMove(4, 0, testLeftward));
//Is a horizontal move if the piece moves one space in the rightward direction
assertTrue(testRightward.isHorizontalMove(3, 4, testRightward));
//Is a horizontal move if the piece moves multiple spaces in the rightward direction
assertTrue(testRightward.isHorizontalMove(3, 5, testRightward));
//Is not a horizontal move if there is a piece between the inputted row and the one the piece is on
assertFalse(testRightward.isHorizontalMove(3, 7, testRightward));
}
/**
* Tests isDiagonalMove(int toRow, toColumn, ChessPiece piece) from DiagonalMove interface
*/
@Test
public void testIsDiagonalMove() //This test will fail at current due to current constants
{
//ChessBoard diagonalBoard stores a 9 x 9 chess board which makes testing this method easier
SwingChessBoard diagonalBoard = new SwingChessBoard(boardDisplay, gameRules);
//BishopPiece testDiagonal stores a BishopPiece used for testing all cases in this method
BishopPiece testDiagonal = new BishopPiece(ChessGame.Side.NORTH, 4, 4, diagonalBoard);
//KnightPiece southEastHitDetection will store a KnightPiece used for testing that a diagonal move is not legal if there is a piece between the input location and the current location
KnightPiece southEastHitDetection = new KnightPiece(ChessGame.Side.NORTH, 7, 7, diagonalBoard);
//Must add the piece to the board as otherwise all spaces on the board would be null
testDiagonal.getChessBoard().addPiece(southEastHitDetection, 7, 7);
//KnightPiece northEastDetection will store a KnightPiece used for testing that a diagonal move is not legal if there is a piece between the input location and the current location
KnightPiece northEastHitDetection = new KnightPiece(ChessGame.Side.NORTH, 1, 7, diagonalBoard);
//Must add the piece to the board as otherwise all spaces on the board would be null
testDiagonal.getChessBoard().addPiece(northEastHitDetection, 1, 7);
//KnightPiece southWestHitDetection will store a KnightPiece used for testing that a diagonal move is not legal if there is a piece between the input location and the current location
KnightPiece southWestHitDetection = new KnightPiece(ChessGame.Side.NORTH, 7, 1, diagonalBoard);
//Must add the piece to the board as otherwise all spaces on the board would be null
testDiagonal.getChessBoard().addPiece(southWestHitDetection, 7, 1);
//KnightPiece northWestHitDetection will store a KnightPiece used for testing that a diagonal move is not legal if there is a piece between the input location and the current location
KnightPiece northWestHitDetection = new KnightPiece(ChessGame.Side.NORTH, 1, 1, diagonalBoard);
//Must add the piece to the baord as otherwise all spaces on the board would be null
testDiagonal.getChessBoard().addPiece(northWestHitDetection, 1, 1);
//First, test must be implimented checking if the inputted move is diagonal to the original position (in report: a test must be implimented checking if the absolute value...)
//Should return false for reasons stated in previous line comment
assertFalse(testDiagonal.isDiagonalMove(4, 0, testDiagonal));
//Second, test if the piece can move zero, one, or more than one spaces in the south east direction
assertFalse(testDiagonal.isDiagonalMove(4, 4, testDiagonal));
assertTrue(testDiagonal.isDiagonalMove(5, 5, testDiagonal));
assertTrue(testDiagonal.isDiagonalMove(6, 6, testDiagonal));
//Third, test that the piece cannot move south east if there is a piece between its destination and current location
assertFalse(testDiagonal.isDiagonalMove(8, 8, testDiagonal));
//Fourth, test if the piece can move zero, one or more than one spaces in the north east direction
assertFalse(testDiagonal.isDiagonalMove(4, 4, testDiagonal));
assertTrue(testDiagonal.isDiagonalMove(3, 5, testDiagonal));
assertTrue(testDiagonal.isDiagonalMove(2, 6, testDiagonal));
//Fifth, test that the piece cannot move north east if there is a piece between its destination and current location
assertFalse(testDiagonal.isDiagonalMove(0, 8, testDiagonal));
//Sixth, test if the piece can move zero, one, or more than one spaces in the south west direction
assertFalse(testDiagonal.isDiagonalMove(4, 4, testDiagonal));
assertTrue(testDiagonal.isDiagonalMove(5, 3, testDiagonal));
assertTrue(testDiagonal.isDiagonalMove(6, 2, testDiagonal));
//Seventh, test that the piece cannot move south west if there is a piece between its destination and current location
assertFalse(testDiagonal.isDiagonalMove(8, 0, testDiagonal));
//Eighth, test if the piece can move zero, one, or more than one spaces in the north west direction
assertFalse(testDiagonal.isDiagonalMove(4, 4, testDiagonal));
assertTrue(testDiagonal.isDiagonalMove(3, 3, testDiagonal));
assertTrue(testDiagonal.isDiagonalMove(2, 2, testDiagonal));
//Ninth and finally, test that the piece cannot move north west if there is a piece between its destination and current location
assertFalse(testDiagonal.isDiagonalMove(0, 0, testDiagonal));
}
/**
* Tests isOmniDirectionalOneSpaceMove(int toRow, int toColumn, ChessPiece piece)
*/
@Test
public void testIsOmniDirectionalOneSpaceMove()
{
//KingPiece testPiece stores a KingPiece used to test this method
KingPiece testPiece = new KingPiece(ChessGame.Side.NORTH, 4, 4, board);
//Should be a legal omni direcitonal one space move if the piece moves left one space
assertTrue(testPiece.isOmniDirectionalOneSpaceMove(3, 4, testPiece));
//Should be a legal omni direcitonal one space move if the piece moves right one space
assertTrue(testPiece.isOmniDirectionalOneSpaceMove(5, 4, testPiece));
//Should be a legal omni direcitonal one space move if the piece moves up one space
assertTrue(testPiece.isOmniDirectionalOneSpaceMove(4, 5, testPiece));
//Should be a legal omni direcitonal one space move if the piece moves down one space
assertTrue(testPiece.isOmniDirectionalOneSpaceMove(4, 3, testPiece));
//Should be a legal omni direcitonal one space move if the piece moves south east one space
assertTrue(testPiece.isOmniDirectionalOneSpaceMove(5, 5, testPiece));
//Should be a legal omni direcitonal one space move if the piece moves north east one space
assertTrue(testPiece.isOmniDirectionalOneSpaceMove(3, 5, testPiece));
//Should be a legal omni directional one space move if the piece moves south west one space
assertTrue(testPiece.isOmniDirectionalOneSpaceMove(5, 3, testPiece));
//Should be a legal omni direcitonal one space move if the piece moves north west one space
assertTrue(testPiece.isOmniDirectionalOneSpaceMove(3, 3, testPiece));
//Should not be an omni directional onespace move if the piece does not move one space in any direction
assertFalse(testPiece.isOmniDirectionalOneSpaceMove(0, 0, testPiece));
}
/**
* Tests isCastleMove(int toRow, intToColumn, ChessPiece piece)
* Also, SE = south east, NE = north east, SW = south west, and NW = north west
*/
@Test
public void testIsCastleMove()
{
//KingPiece testMovement stores a KingPiece testing if the king has moved this game
KingPiece testKingMovement = new KingPiece(ChessGame.Side.NORTH, 0, 4, board);
board.addPiece(testKingMovement, 0, 4);
testKingMovement.isLegalNonCaptureMove(1, 4);
//KingPiece testKingNorth stores a KingPiece testing castle move validity on the north side of the board
KingPiece testKingNorth = new KingPiece(ChessGame.Side.NORTH, 0, 4, board);
board.addPiece(testKingNorth, 0, 4);
//KingPiece testKingSouth stores a KingPiece testing castle move validity on the south side of the board
KingPiece testKingSouth = new KingPiece(ChessGame.Side.SOUTH, 7, 4, board);
board.addPiece(testKingSouth, 7, 4);
//All of the following are KnightPiece's which are used to test that a piece cannot castle if a piece other than a rook is in the corner of the board they are trying to move to
/* In all of the following pieces defined here, the row and column assigned to the piece does not matter since it will be added to the board at a defined point anyway */
//All of the following are KnightPiece's which are used to test that a piece cannot castle if a piece other than a rook is in the corner of the board they are trying to move to
KnightPiece testIsRookSE = new KnightPiece(ChessGame.Side.NORTH, 7, 7, board);
KnightPiece testIsRookNE = new KnightPiece(ChessGame.Side.NORTH, 0, 7, board);
KnightPiece testIsRookSW = new KnightPiece(ChessGame.Side.NORTH, 7, 0, board);
KnightPiece testIsRookNW = new KnightPiece(ChessGame.Side.NORTH, 0, 0, board);
board.addPiece(testIsRookSE, 7, 7);
board.addPiece(testIsRookNE, 0, 7);
board.addPiece(testIsRookSW, 7, 0);
board.addPiece(testIsRookNW, 0, 0);
//All of the following are RookPiece's which are used to test that a piece cannot castle if a piece at this corner of the board is of the opposing side
RookPiece testIsSameSideSE = new RookPiece(ChessGame.Side.NORTH, 7, 7, board);
RookPiece testIsSameSideNE = new RookPiece(ChessGame.Side.SOUTH, 0, 7, board);
RookPiece testIsSameSideSW = new RookPiece(ChessGame.Side.NORTH, 7, 0, board);
RookPiece testIsSameSideNW = new RookPiece(ChessGame.Side.SOUTH, 0, 0, board);
//All of the following are RookPiece's which are used to test that a piece cannot castle if the piece at this corner of the board has moved this game
RookPiece testRookMovementSE = new RookPiece(ChessGame.Side.NORTH, 7, 7, board);
RookPiece testRookMovementNE = new RookPiece(ChessGame.Side.NORTH, 0, 7, board);
RookPiece testRookMovementSW = new RookPiece(ChessGame.Side.NORTH, 7, 0, board);
RookPiece testRookMovementNW = new RookPiece(ChessGame.Side.NORTH, 0, 0, board);
//All of the following are KnightPiece's which are used to test that a piece cannot castle if there are many pieces between the king and the piece at this spot on the board
KnightPiece testManySE = new KnightPiece(ChessGame.Side.NORTH, 7, 7, board);
KnightPiece testManyNE = new KnightPiece(ChessGame.Side.NORTH, 0, 7, board);
KnightPiece testManySW = new KnightPiece(ChessGame.Side.NORTH, 7, 0, board);
KnightPiece testManyNW = new KnightPiece(ChessGame.Side.NORTH, 0, 0, board);
//All of the following are KnightPiece's which are used to test that a piece cannot castle if there is one piece between the king and the piece at this spot
KnightPiece testOneSE = new KnightPiece(ChessGame.Side.NORTH, 7, 7, board);
KnightPiece testOneNE = new KnightPiece(ChessGame.Side.NORTH, 0, 7, board);
KnightPiece testOneSW = new KnightPiece(ChessGame.Side.NORTH, 7, 0, board);
KnightPiece testOneNW = new KnightPiece(ChessGame.Side.NORTH, 0, 0, board);
//All of the following are KnightPiece's which are used to test that a piece cannot castle if many spaces between the king and the rook are threatened
QueenPiece testManyThreatenedSE = new QueenPiece(ChessGame.Side.NORTH, 7, 7, board);
QueenPiece testManyThreatenedNE = new QueenPiece(ChessGame.Side.SOUTH, 0, 7, board);
QueenPiece testManyThreatenedSW = new QueenPiece(ChessGame.Side.NORTH, 7, 0, board);
QueenPiece testManyThreatenedNW = new QueenPiece(ChessGame.Side.SOUTH, 0, 0, board);
//All of the following are KnightPiece's which are used to test that a piece cannot castle if one space between the king and the rook are threatened
RookPiece testOneThreatenedSE = new RookPiece(ChessGame.Side.NORTH, 7, 7, board);
RookPiece testOneThreatenedNE = new RookPiece(ChessGame.Side.SOUTH, 0, 7, board);
RookPiece testOneThreatenedSW = new RookPiece(ChessGame.Side.NORTH, 7, 0, board);
RookPiece testOneThreatenedNW = new RookPiece(ChessGame.Side.SOUTH, 0, 0, board);
//All of the following are KnightPiece's which are used to test that a piece can castle if there are zero pieces between the king and the piece at this spot
//and zero spaces between the king and this spot are threatened (this would mean that none of the conditions in the method return false for the specified direction
RookPiece testZeroSE = new RookPiece(ChessGame.Side.SOUTH, 7, 7, board);
RookPiece testZeroNE = new RookPiece(ChessGame.Side.NORTH, 0, 7, board);
RookPiece testZeroSW = new RookPiece(ChessGame.Side.SOUTH, 7, 0, board);
RookPiece testZeroNW = new RookPiece(ChessGame.Side.NORTH, 0, 0, board);
//First, test if that a castle move does not move two spaces to the right or left and/or the piece does not move horizontally across the same row
assertFalse(testKingNorth.isCastleMove(4, 4, testKingNorth));
//Before the rest of the tests, check if the king has moved at all this game
assertFalse(testKingMovement.isCastleMove(0, 2, testKingMovement));
board.removePiece(1, 4);
/* All of the following tests apply to a piece attempting to castle on the north east side */
//1.) Check if the piece at this corner of the board is a rook
assertFalse(testKingNorth.isCastleMove(0, 6, testKingNorth));
board.removePiece(0, 7);
//2.) Check if the piece at this corner of the board is on the opposing side
board.addPiece(testIsSameSideNE, 0, 7);
assertFalse(testKingNorth.isCastleMove(0, 6, testKingNorth));
board.removePiece(0, 7);
//3.) Check if the piece at this corner of the board has moved this game
board.addPiece(testRookMovementNE, 0, 7);
testRookMovementNE.isLegalNonCaptureMove(0, 6);
testRookMovementNE.isLegalNonCaptureMove(0, 7);
assertFalse(testKingNorth.isCastleMove(0, 6, testKingNorth));
board.removePiece(0, 7);
//4a.) Check if there are many pieces in between the piece in this corner of the board and the king
board.addPiece(testManyNE, 0, 7);
board.addPiece(testManyNE, 0, 6);
board.addPiece(testManyNE, 0, 5);
assertFalse(testKingNorth.isCastleMove(0, 6, testKingNorth));
board.removePiece(0, 7);
board.removePiece(0, 6);
//4b.) Check if there is one piece in between the piece in this corner of the board and the king
board.addPiece(testOneNE, 0, 7);
assertFalse(testKingNorth.isCastleMove(0, 6, testKingNorth));
board.removePiece(0, 7);
board.removePiece(0, 5);
//5a.) Check if there are many squares threatened between the piece in this corner of the board and the king
board.addPiece(testManyThreatenedNE, 0, 7);
board.addPiece(testManyThreatenedNE, 1, 6);
board.addPiece(testManyThreatenedNE, 1, 5);
assertFalse(testKingNorth.isCastleMove(0, 6, testKingNorth));
board.removePiece(0, 7);
board.removePiece(1, 6);
//5b.) Check if there is one square threatened between the piece in this corner of the board and the king
board.addPiece(testOneThreatenedNE, 0, 7);
assertFalse(testKingNorth.isCastleMove(0, 6, testKingNorth));
board.removePiece(0, 7);
board.removePiece(1, 5);
//4c, 5c., and by proxy 6.): Check if there are zero spaces threatened between the piece in this corner and the king
//and there there are zero pieces on the board between the this piece and the king
board.addPiece(testZeroNE, 0, 7);
assertTrue(testKingNorth.isCastleMove(0, 6, testKingNorth));
board.removePiece(0, 7);
/* All of the following tests apply to a piece attempting to castle on the north west side */
//1.) Check if the piece at this corner of the board is a rook
assertFalse(testKingNorth.isCastleMove(0, 2, testKingNorth));
board.removePiece(0, 0);
//2.) Check if the piece at this corner of the board is on the opposing side
board.addPiece(testIsSameSideNW, 0, 0);
assertFalse(testKingNorth.isCastleMove(0, 2, testKingNorth));
board.removePiece(0, 0);
//3.) Check if the piece at this corner of the board has moved this game
board.addPiece(testRookMovementNW, 0, 0);
testRookMovementNW.isLegalNonCaptureMove(0, 1);
testRookMovementNW.isLegalNonCaptureMove(0, 0);
assertFalse(testKingNorth.isCastleMove(0, 2, testKingNorth));
board.removePiece(0, 0);
//4a.) Check if there are many pieces in between the piece in this corner of the board and the king
board.addPiece(testManyNW, 0, 0);
board.addPiece(testManyNW, 0, 1);
board.addPiece(testManyNW, 0, 2);
assertFalse(testKingNorth.isCastleMove(0, 2, testKingNorth));
board.removePiece(0, 0);
board.removePiece(0, 1);
//4b.) Check if there is one piece in between the piece in this corner of the board and the king
board.addPiece(testOneNW, 0, 0);
assertFalse(testKingNorth.isCastleMove(0, 2, testKingNorth));
board.removePiece(0, 0);
board.removePiece(0, 2);
//5a.) Check if there are many squares threatened between the piece in this corner of the board and the king
board.addPiece(testManyThreatenedNW, 0, 0);
board.addPiece(testManyThreatenedNW, 1, 1);
board.addPiece(testManyThreatenedNW, 1, 2);
assertFalse(testKingNorth.isCastleMove(0, 2, testKingNorth));
board.removePiece(0, 0);
board.removePiece(1, 1);
//5b.) Check if there is one square threatened between the piece in this corner of the board and the king
board.addPiece(testOneThreatenedNW, 0, 0);
assertFalse(testKingNorth.isCastleMove(0, 2, testKingNorth));
board.removePiece(0, 0);
board.removePiece(1, 2);
//4c, 5c., and by proxy 6.): Check if there are zero spaces threatened between the piece in this corner and the king
//and there there are zero pieces on the board between the this piece and the king
board.addPiece(testZeroNW, 0, 0);
assertTrue(testKingNorth.isCastleMove(0, 2, testKingNorth));
board.removePiece(0, 0);
/* All of the following tests apply to a piece attempting to castle on the south east side */
//1.) Check if the piece at this corner of the board is a rook
assertFalse(testKingSouth.isCastleMove(7, 6, testKingSouth));
board.removePiece(7, 7);
//2.) Check if the piece at this corner of the board is on the opposing side
board.addPiece(testIsSameSideSE, 7, 7);
assertFalse(testKingSouth.isCastleMove(7, 6, testKingSouth));
board.removePiece(7, 7);
//3.) Check if the piece at this corner of the board has moved this game
board.addPiece(testRookMovementSE, 7, 7);
testRookMovementSE.isLegalNonCaptureMove(7, 6);
testRookMovementSE.isLegalNonCaptureMove(7, 7);
assertFalse(testKingSouth.isCastleMove(7, 6, testKingSouth));
board.removePiece(7, 7);
//4a.) Check if there are many pieces in between the piece in this corner of the board and the king
board.addPiece(testManySE, 7, 7);
board.addPiece(testManySE, 7, 6);
board.addPiece(testManySE, 7, 5);
assertFalse(testKingSouth.isCastleMove(7, 6, testKingSouth));
board.removePiece(7, 7);
board.removePiece(7, 6);
//4b.) Check if there is one piece in between the piece in this corner of the board and the king
board.addPiece(testOneSE, 7, 7);
assertFalse(testKingSouth.isCastleMove(7, 6, testKingSouth));
board.removePiece(7, 7);
board.removePiece(7, 5);
//5a.) Check if there are many squares threatened between the piece in this corner of the board and the king
board.addPiece(testManyThreatenedSE, 7, 7);
board.addPiece(testManyThreatenedSE, 6, 6);
board.addPiece(testManyThreatenedSE, 6, 5);
assertFalse(testKingSouth.isCastleMove(7, 6, testKingSouth));
board.removePiece(7, 7);
board.removePiece(6, 6);
//5b.) Check if there is one square threatened between the piece in this corner of the board and the king
board.addPiece(testOneThreatenedSE, 7, 7);
assertFalse(testKingSouth.isCastleMove(7, 6, testKingSouth));
board.removePiece(7, 7);
board.removePiece(6, 5);
//4c, 5c., and by proxy 6.): Check if there are zero spaces threatened between the piece in this corner and the king
//and there there are zero pieces on the board between the this piece and the king
board.addPiece(testZeroSE, 7, 7);
assertTrue(testKingSouth.isCastleMove(7, 6, testKingSouth));
board.removePiece(7, 7);
/* All of the following tests apply to a piece attempting to castle on the south west side */
//1.) Check if the piece at this corner of the board is a rook
assertFalse(testKingSouth.isCastleMove(7, 2, testKingSouth));
board.removePiece(7, 0);
//2.) Check if the piece at this corner of the board is on the opposing side
board.addPiece(testIsSameSideSW, 7, 0);
assertFalse(testKingSouth.isCastleMove(7, 2, testKingSouth));
board.removePiece(7, 0);
//3.) Check if the piece at this corner of the board has moved this game
board.addPiece(testRookMovementSW, 7, 0);
testRookMovementSW.isLegalNonCaptureMove(7, 1);
testRookMovementSW.isLegalNonCaptureMove(7, 0);
assertFalse(testKingSouth.isCastleMove(7, 2, testKingSouth));
board.removePiece(7, 0);
//4a.) Check if there are many pieces in between the piece in this corner of the board and the king
board.addPiece(testManySW, 7, 0);
board.addPiece(testManySW, 7, 1);
board.addPiece(testManySW, 7, 2);
assertFalse(testKingSouth.isCastleMove(7, 2, testKingSouth));
board.removePiece(7, 0);
board.removePiece(7, 1);
//4b.) Check if there is one piece in between the piece in this corner of the board and the king
board.addPiece(testOneSW, 7, 0);
assertFalse(testKingSouth.isCastleMove(7, 2, testKingSouth));
board.removePiece(7, 0);
board.removePiece(7, 2);
//5a.) Check if there are many squares threatened between the piece in this corner of the board and the king
board.addPiece(testManyThreatenedSW, 7, 0);
board.addPiece(testManyThreatenedSW, 6, 1);
board.addPiece(testManyThreatenedSW, 6, 2);
assertFalse(testKingSouth.isCastleMove(7, 2, testKingSouth));
board.removePiece(7, 0);
board.removePiece(6, 1);
//5b.) Check if there is one square threatened between the piece in this corner of the board and the king
board.addPiece(testOneThreatenedSW, 7, 0);
assertFalse(testKingSouth.isCastleMove(7, 2, testKingSouth));
board.removePiece(7, 0);
board.removePiece(6, 2);
//4c, 5c., and by proxy 6.): Check if there are zero spaces threatened between the piece in this corner and the king
//and there there are zero pieces on the board between the this piece and the king
board.addPiece(testZeroSW, 7, 0);
assertTrue(testKingSouth.isCastleMove(7, 2, testKingSouth));
board.removePiece(7, 0);
}
/**
* Tests isPawnMove(int toRow, intToColumn, ChessPiece piece)
* Also, SE = south east, NE = north east, SW = south west, and NW = north west
*/
@Test
public void testIsPawnMove()
{
//PawnPiece testNorth is used for testing the legality of one space pawn moves starting on the north side
PawnPiece testNorth = new PawnPiece(ChessGame.Side.NORTH, 1, 4, board);
//PawnPiece testSouth is used for testing the legality of one space pawn moves starting on the south side
PawnPiece testSouth = new PawnPiece(ChessGame.Side.SOUTH, 7, 7, board);
//PawnPiece testNorthFirstMove is used for testing the legality of two space pawn moves starting on the north side
PawnPiece testNorthFirstMove = new PawnPiece(ChessGame.Side.NORTH, 1, 6, board);
//PawnPiece testSouthFirstMove is used for testing the legality of two space pawn moves starting on the north side
PawnPiece testSouthFirstMove = new PawnPiece(ChessGame.Side.SOUTH, 7, 7, board);
//PawnPiece testNorthDiagonalMove is used for testing
//the legality of one space diagonal pawn moves starting on the north side
PawnPiece testNorthDiagonalMove = new PawnPiece(ChessGame.Side.NORTH, 3, 5, board);
//PawnPiece testSouthDiagonalMove is used for testing
//the legality of one space diagonal pawn moves starting on the north side
PawnPiece testSouthDiagonalMove = new PawnPiece(ChessGame.Side.SOUTH, 4, 5, board);
//PawnPiece testUpgrade is used for testing if a pawn is set to be upgraded when it reaches the opposing side
PawnPiece testUpgrade = new PawnPiece(ChessGame.Side.NORTH, 6, 7, board);
//This method call will return true, as shown in the unit tests below (moving piece one space forward is legal for a pawn)
testUpgrade.isPawnMove(7, 7, testUpgrade);
/* This section tests the legality of pawn moves made by pawns on the north side of the board */
//Refer to 4.) in testing report: All of these tests check that toBeUpgraded is set to true in the tests that return true
//Tests that if a pawn attempts to move one space forward, this is a pawn move
assertTrue(testNorth.isPawnMove(2, 4, testNorth));
assertTrue(testUpgrade.getToBeUpgraded());
//Tests that if a pawn attempts to move two spaces forward, this is a pawn move
assertTrue(testNorthFirstMove.isPawnMove(3, 6, testNorthFirstMove));
assertTrue(testUpgrade.getToBeUpgraded());
//Tests that if a pawn attempts to move one space diagonally, it can only do so if there is an opponent piece on that part of the board
testNorthDiagonalMove.getChessBoard().addPiece(testNorthDiagonalMove, 3, 5);
testNorthDiagonalMove.getChessBoard().addPiece(testSouth, 4, 4);
testNorthDiagonalMove.getChessBoard().addPiece(testSouth, 4, 6);
assertTrue(testNorthDiagonalMove.isPawnMove(4, 4, testNorthDiagonalMove));
assertTrue(testUpgrade.getToBeUpgraded());
testNorthDiagonalMove.getChessBoard().removePiece(4, 4);
testNorthDiagonalMove.getChessBoard().addPiece(testNorthDiagonalMove, 3, 5);
assertTrue(testNorthDiagonalMove.isPawnMove(4, 6, testNorthDiagonalMove));
assertTrue(testUpgrade.getToBeUpgraded());
testNorthDiagonalMove.getChessBoard().removePiece(4, 6);
testNorthDiagonalMove.getChessBoard().removePiece(4, 4);
testNorthDiagonalMove.getChessBoard().removePiece(3, 5);
testSouth.setLocation(7, 7);
/* This section tests the legality of pawn moves made by pawns on the south side of the board */
//Refer to 4.) in testing report: All of these tests check that toBeUpgraded is set to true in the tests that return true
//Tests that if a pawn attempts to move one space forward, this is a pawn move
assertTrue(testSouth.isPawnMove(6, 7, testSouth));
assertTrue(testUpgrade.getToBeUpgraded());
//Tests that if a pawn attempts to move two spaces forward, this is a pawn move
assertTrue(testSouthFirstMove.isPawnMove(5, 7, testSouthFirstMove));
assertTrue(testUpgrade.getToBeUpgraded());
//Tests that if a pawn attempts to move one space diagonally, it can only do so if there is an opponent piece on that part of the board
testSouthDiagonalMove.getChessBoard().addPiece(testSouthDiagonalMove, 4, 5);
testSouthDiagonalMove.getChessBoard().addPiece(testNorth, 3, 4);
testSouthDiagonalMove.getChessBoard().addPiece(testNorth, 3, 6);
assertTrue(testSouthDiagonalMove.isPawnMove(3, 4, testSouthDiagonalMove));
assertTrue(testUpgrade.getToBeUpgraded());
testSouthDiagonalMove.getChessBoard().removePiece(4, 5);
testSouthDiagonalMove.getChessBoard().addPiece(testSouthDiagonalMove, 4, 5);
assertTrue(testSouthDiagonalMove.isPawnMove(3, 6, testSouthDiagonalMove));
assertTrue(testUpgrade.getToBeUpgraded());
/* :Last two tests: Check to make sure that a move is not a legal pawn move if none of the above conditions are fulfilled
* or if the piece tries to move two spaces forward if it has already moved once this game */
assertFalse(testNorth.isPawnMove(0, 0, testNorth));
assertFalse(testSouthFirstMove.isPawnMove(3, 7, testSouthFirstMove));
}
/**
* Tests isLegalMove(int toRow, int toColumn) from the KnightPiece Class
*/
@Test
public void testKnightPieceIsLegalMove()
{
//ChessPiece testPiece stores a KnightPiece used for testing this method
KnightPiece testPiece = new KnightPiece(ChessGame.Side.NORTH, 0, 1, board);
board.addPiece(testPiece, 0, 1);
//Should return false if the player tries to move the piece out of bounds/off the board
assertFalse(testPiece.isLegalMove(900, 900));
//Should return false if the player tries to not move the piece at all
assertFalse(testPiece.isLegalMove(0, 1));
//Should return true if the player inputs a legal move for a knight (isLMove() returns true)
assertTrue(testPiece.isLegalMove(1, 3));
//Should return false if the player inputs an illegal move for a knight (isLMove() returns false)
assertFalse(testPiece.isLegalMove(6, 6));
}
/**
* Tests isLegalMove(int toRow, int toColumn) from the RookPiece Class
*/
@Test
public void testRookPieceIsLegalMove()
{
//ChessPiece testPiece stores a RookPiece used for testing this method
RookPiece testPiece = new RookPiece(ChessGame.Side.NORTH, 0, 0, board);
board.addPiece(testPiece, 0, 0);
//Should return false if the player tries to move the piece off the board/out of bounds
assertFalse(testPiece.isLegalMove(900, 900));
//Should return false if the player tries to not move the piece at all
assertFalse(testPiece.isLegalMove(0, 0));
//Should return true if the player inputs a legal vertical move (isVerticalMove() returns true)
assertTrue(testPiece.isLegalMove(0, 6));
//Should return true if the player inputs a legal horizontal move (isHorizontalMove() returns true)
assertTrue(testPiece.isLegalMove(6, 0));
//Should return false if, given the inputted destination is on the board and is not in the same spot as the piece currently is,
//the move is not legal for a rook
assertFalse(testPiece.isLegalMove(4, 4));
}
/**
* Tests isLegalMove(int toRow, int toColumn) from the BishopPiece Class
*/
@Test
public void testBishopPieceIsLegalMove()
{
//ChessPiece testPiece stores a BishopPiece used for testing this method
BishopPiece testPiece = new BishopPiece(ChessGame.Side.NORTH, 0, 0, board);
board.addPiece(testPiece, 4, 4);
//Should return false if the player tries to move the piece off the board/out of bounds
assertFalse(testPiece.isLegalMove(900, 900));
//Should return false if the player tries to not move the piece at all
assertFalse(testPiece.isLegalMove(4, 4));
//Should return true if the player inputs a legal diagonal move (isDiagonalMove() returns true)
assertTrue(testPiece.isLegalMove(1, 1));
//Should return false if, given the inputted destination is on the board and is not in the same spot as the piece currently is,
//the move is not legal for a bishop
assertFalse(testPiece.isLegalMove(1, 0));
}
/**
* Tests isLegalMove(int toRow, int toColumn) from the QueenPiece Class
*/
@Test
public void testQueenPieceIsLegalMove()
{
//QueenPiece testPiece stores a QueenPiece used for testing this method
QueenPiece testPiece = new QueenPiece(ChessGame.Side.NORTH, 0, 0, board);
board.addPiece(testPiece, 4, 4);
//Should return false if the player tries to move the piece off the board/out of bounds
assertFalse(testPiece.isLegalMove(900, 900));
//Should return false if the player tries to not move the piece at all
assertFalse(testPiece.isLegalMove(4, 4));
//Should return true if the player inputs a legal diagonal move (isDiagonalMove() returns true)
assertTrue(testPiece.isLegalMove(7, 7));
//Should return true if the player inputs a legal vertical move (isVerticalMove() returns true)
assertTrue(testPiece.isLegalMove(1, 4));
//Should return true if the player inputs a legal horizontal move (isHorizontalMove() returns true)
assertTrue(testPiece.isLegalMove(4, 7));
//Should return false if, given the inputted destination is on the board and is not in the same spot as the piece currently is,
//the move is not legal for a queen
assertFalse(testPiece.isLegalMove(1, 0));
}
/**
* Tests isLegalMove(int toRow, int toColumn) from the KingPiece Class
*/
@Test
public void testKingPieceIsLegalMove()
{
//KingPiece testPiece stores a KingPiece used for testing this method
KingPiece testPiece = new KingPiece(ChessGame.Side.NORTH, 0, 4, board);
board.addPiece(testPiece, 0, 4);
//RookPiece testCastle stores a RookPiece used for testing that a legal move for a king is a castle move
RookPiece testCastle = new RookPiece(ChessGame.Side.NORTH, 0, 0, board);
board.addPiece(testCastle, 0, 0);
//Should return false if the player tries to move the piece off the board/out of bounds
assertFalse(testPiece.isLegalMove(900, 900));
//Should return false if the player tries to not move the piece at all
assertFalse(testPiece.isLegalMove(0, 4));
//Should return true if the player inputs a legal castle move (isCastleMove() returns true)
assertTrue(testPiece.isLegalMove(0, 2));
//Should return true if the player inputs a legal omni directional one space move (isOmniDirectionalOneSpaceMove() returns true)
assertTrue(testPiece.isLegalMove(1, 4));
//Should return false if, given the inputted destination is on the board and is not in the same spot as the piece currently is,
//the move is not legal for a king
assertFalse(testPiece.isLegalMove(5, 0));
}
/**
* Tests isLegalMove(int toRow, int toColumn) from the PawnPiece Class
*/
@Test
public void testPawnPieceIsLegalMove()
{
//PawnPiece testPiece stores a PawnPiece used for testing this method
KingPiece testPiece = new KingPiece(ChessGame.Side.SOUTH, 7, 4, board);
board.addPiece(testPiece, 7, 4);
//Should return false if the player tries to move the piece off the board/out of bounds
assertFalse(testPiece.isLegalMove(900, 900));
//Should return false if the player tries to not move the piece at all
assertFalse(testPiece.isLegalMove(7, 4));
//Should return true if the player inputs a legal pawn move (isPawnMove() returns true)
assertTrue(testPiece.isLegalMove(6, 4));
//Should return false if, given the inputted destination is on the board and is not in the same spot as the piece currently is,
//the move is not legal for a pawn
assertFalse(testPiece.isLegalMove(7, 7));
}
/**
* Tests isLegalNonCaptureMove(int row, int column)
*/
@Test
public void testIsLegalNonCaptureMove()
{
//QueenPiece testPiece stores a QueenPiece used for testing this method
QueenPiece testPiece = new QueenPiece(ChessGame.Side.NORTH, 4, 4, board);
board.addPiece(testPiece, 4, 4);
//PawnPiece testEmpty stores a PawnPiece used for testing if testPiece is trying to move to an empty square or not
PawnPiece testEmpty = new PawnPiece(ChessGame.Side.NORTH, 1, 1, board);
board.addPiece(testEmpty, 1, 1);
//KingPiece testCastleLeft stores a KingPiece used for testing that a rook is moved properly if a castle move is made
//with the king castling to the left
KingPiece testCastleLeft = new KingPiece(ChessGame.Side.NORTH, 0, 4, board);
board.addPiece(testCastleLeft, 0, 4);
//RookPiece testLeftCastlingRookMoves stores a RookPiece used for testing that a rook is moved properly if the king makes a castle move
//with the king castling to the left
RookPiece testLeftCastlingRookMoves = new RookPiece(ChessGame.Side.NORTH, 0, 0, board);
board.addPiece(testLeftCastlingRookMoves, 0, 0);
//Tests that if the input position is not a legal move for this type of piece, this is not a legal move
//(In other words, isLegalMove() for this piece returns false)
assertFalse(testPiece.isLegalNonCaptureMove(2, 3));
//Tests that if the square the player is trying to move to is occupied, this piece should not be moved
assertFalse(testPiece.isLegalNonCaptureMove(1, 1));
//Tests that if the square the player is trying to move to is a legal move for this kind of piece
//and is empty, then the piece should be moved to new input location
assertTrue(testPiece.isLegalNonCaptureMove(6, 6));
//Tests that if the piece attempting to be moved is a king and a castle move is being made,
//then if the king is castling to the right corner, move the rook in that corner to be one space to the right of the king's new position
assertTrue(testCastleLeft.isLegalNonCaptureMove(0, 2));
board.removePiece(0, 2);
//Testing Rook's new position: If the rook did not move, then trying to remove the piece at this position should throw a NullPointerException during runtime
board.removePiece(0, 3);
//KingPiece testCastleRight stores a KingPiece used for testing that a rook is moved properly if a castle move is made
//with the king castling to the right
KingPiece testCastleRight = new KingPiece(ChessGame.Side.NORTH, 0, 4, board);
board.addPiece(testCastleRight, 0, 4);
//RookPiece testRightCastlingRookMoves stores a RookPiece used for testing that a rook is moved properly if the king makes a castle move
//with the king castling to the right
RookPiece testRightCastlingRookMoves = new RookPiece(ChessGame.Side.NORTH, 0, 7, board);
board.addPiece(testRightCastlingRookMoves, 0, 7);
//Tests that if the piece attempting to be moved is a king and a castle move is being made,
//then if the king is castling to the right corner, move the rook in that corner to be one space to the left of the king
assertTrue(testCastleRight.isLegalNonCaptureMove(0, 6));
//Testing Rook's new position: If the rook did not move, then trying to remove the piece at this position should throw a NullPointerException during runtime
board.removePiece(0, 5);
}
/**
* Tests isLegalCaptureMove(int row, int column)
*/
@Test
public void testIsLegalCaptureMove()
{
//QueenPiece testPiece stores a QueenPiece used for testing this method
QueenPiece testPiece = new QueenPiece(ChessGame.Side.NORTH, 4, 4, board);
board.addPiece(testPiece, 4, 4);
//PawnPiece testEmpty stores a PawnPiece used for testing if testPiece is trying to move to a spot with a piece on it
PawnPiece testEmpty = new PawnPiece(ChessGame.Side.SOUTH, 1, 1, board);
board.addPiece(testEmpty, 1, 1);
//PawnPiece testSameSide stores a PawnPiece used for testing if testPiece is trying to move to a spot containing a piece of the same side
PawnPiece testSameSide = new PawnPiece(ChessGame.Side.NORTH, 1, 6, board);
board.addPiece(testSameSide, 1, 6);
//Tests if this piece is making a legal move, is moving to a space with a piece, and the piece
//in the destination square is not on the same side as the piece being moved, this is a legal capture move for the piece being moved
assertTrue(testPiece.isLegalCaptureMove(1, 1)); //This will set the location of testPiece to row 1, column 1
//Tests if this piece is not making a legal move for its type, then this is not a legal capture move
assertFalse(testPiece.isLegalCaptureMove(3, 2));
//Tests if this piece is making a legal move for its type but the space it is trying to move to is empty,
//this is not a legal capture move
assertFalse(testPiece.isLegalCaptureMove(3, 3));
//Tests if this piece is making a legal move for its type and is moving to an unempty space containing a piece of the same side,
//this is not a legal capture move
assertFalse(testPiece.isLegalCaptureMove(1, 6));
}
/**
* Tests legalPieceToPlay(ChessPiece piece, int row, int column) from EuropeanChess class
* Note that, by default, the starting player is always the one of the north side
*/
@Test
public void testLegalPieceToPlay()
{
//PawnPiece testLegalToPlay stores a PawnPiece used to test this method (north side trying to use north side piece)
PawnPiece testLegalToPlay = new PawnPiece(ChessGame.Side.NORTH, 4, 4, board);
//PawnPiece testPieceIllegalToPlay stores a PawnPiece used to test this method (north side trying to use south side piece)
PawnPiece testIllegalToPlay = new PawnPiece(ChessGame.Side.SOUTH, 4, 4, board);
//Tests if the player on the north side is trying to use a piece of the north side that this is a legal piece to play
assertTrue(gameRules.legalPieceToPlay(testLegalToPlay, 4, 4));
//Tests if the player on the north side is trying to use a piece of the south side that this is not a legal piece to play
assertFalse(gameRules.legalPieceToPlay(testIllegalToPlay, 4, 4));
}
/**
* Tests legalPieceToPlay(ChessPiece piece, int row, int column) from EuropeanChess class
* Again, it is worth noting that, by default, the starting player is always the one on the north side
*/
@Test
public void testMakeMove()
{