-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRookTest.java
173 lines (151 loc) · 5.94 KB
/
RookTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package com.dalton.ChessEngine;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Comparator;
import static org.junit.Assert.*;
import static com.dalton.ChessEngine.Types.*;
import static com.dalton.ChessEngine.UtilsForTests.*;
/**
* Tests for the Rook piece
* @author Dalton Herrewynen
* @version 2
*/
public class RookTest{
Board board;
Engine engine;
ArrayList<Integer> gotMoves, filteredMoves;
ArrayList<Coord> gotCoords, expectedCoords;
@Before
public void setup(){
board=new Board(Board.CLEAR);
gotMoves=new ArrayList<>();
filteredMoves=new ArrayList<>();
gotCoords=new ArrayList<>();
expectedCoords=new ArrayList<>();
engine=new Engine(1,2);
}
@After
public void tearDown(){
board=null;
gotCoords=null;
expectedCoords=null;
gotMoves=null;
filteredMoves=null;
engine=null;
}
/** Test basic movement for WHITE Rook, do not test capture */
@Test
public void testBasicMoveWHITE(){
testBasicMoveCommon(WHITE);
}
/** Test basic movement for BLACK Rook, do not test capture */
@Test
public void testBasicMoveBLACK(){
testBasicMoveCommon(BLACK);
}
/**
* Tests each color's basic moves to avoid code re-use
* @param team Which team to check? WHITE or BLACK
*/
public void testBasicMoveCommon(boolean team){
Coord piecePos;
Rook piece=new Rook(team);
for(int i=0; i<TOTAL_SQUARES; ++i){//test on all tiles
piecePos=new Coord(i);
board.setSquare(piece.pieceCode,piecePos.getIndex());
gotMoves=Engine.getLegalMoves(board,piece.pieceCode);
gotCoords=getDestCoords(gotMoves);
expectedCoords=new ArrayList<>();
expectedCoords.addAll(getLineOfCoords(piecePos,0,1));//4 cardinal directions
expectedCoords.addAll(getLineOfCoords(piecePos,0,-1));
expectedCoords.addAll(getLineOfCoords(piecePos,1,0));
expectedCoords.addAll(getLineOfCoords(piecePos,-1,0));
gotCoords.sort(Comparator.comparingInt(Coord::getIndex));//sort the coord, so they are in same order and comparisons will work
expectedCoords.sort(Comparator.comparingInt(Coord::getIndex));
for(int move: gotMoves){//test correct piece code
assertEquals("Piece code should match the Rook",piece.pieceCode,Move.getPieceCode(move));
}
assertEquals("Square "+piecePos+": Must have same number of Coord in the got and expected list",
expectedCoords.size(),gotCoords.size());
for(int j=0; j<expectedCoords.size(); ++j){
assertEquals("Square "+piecePos+": Move "+j+" destinations don't match",
expectedCoords.get(j).toString(),gotCoords.get(j).toString());
}
board.setSquare(PieceCode.Blank,piecePos.getIndex());//remove this piece and start again
}
}
/** Test the Move structure for WHITE */
@Test
public void testMoveAnatomyWHITE(){
testMoveAnatomyCommon(WHITE);
}
/** Test the Move structure for BLACK */
@Test
public void testMoveAnatomyBLACK(){
testMoveAnatomyCommon(BLACK);
}
/**
* Test move anatomy for either team
* @param team WHITE or BLACK
*/
public void testMoveAnatomyCommon(boolean team){
Coord piecePos=new Coord(4,4);
Rook piece=new Rook(team);
board.setSquare(piece.pieceCode,piecePos.getIndex());
gotMoves=Engine.getLegalMoves(board,piece.pieceCode);
assertFalse("There should be encoded move integers here",gotMoves.isEmpty());
for(int move: gotMoves){
assertEquals("Moves should have starting position correct",piecePos.toString(),Coord.orderedPair(Move.getStartIndex(move)));
assertEquals("Move should be of Normal type (0)",Move.normalMove,Move.getSpecialCode(move));
assertEquals("Piece code, converted to pretty printed name",PieceCode.decodePieceName(piece.pieceCode),Move.getPieceName(move));
}
}
/** Call the capture move test for BLACK Rook */
@Test
public void testBLACKCapture(){
testCaptureCommon(BLACK);
}
/** Call the capture move test for WHITE Rook */
@Test
public void testWHITECapture(){
testCaptureCommon(WHITE);
}
/**
* Tests capture moves for either team
* @param team WHITE or BLACK
*/
public void testCaptureCommon(boolean team){
Piece piece=new Rook(team), friendly=new Queen(team), enemy=new Queen(!team);
Coord piecePos=new Coord();
ArrayList<Coord> reachableSquares=new ArrayList<>();
for(int i=0; i<TOTAL_SQUARES; ++i){//testing all squares
piecePos.setIndex(i);
board.setSquare(piece.pieceCode,piecePos.getIndex());
reachableSquares.clear();//clear and find all squares in range
reachableSquares.addAll(getLineOfCoords(piecePos,0,1));//4 cardinal directions
reachableSquares.addAll(getLineOfCoords(piecePos,0,-1));
reachableSquares.addAll(getLineOfCoords(piecePos,1,0));
reachableSquares.addAll(getLineOfCoords(piecePos,-1,0));
for(Coord enemyPos: reachableSquares){//Check ALL squares reachable by the Rook, one by one
board.setSquare(enemy.pieceCode,enemyPos.getIndex());//Test capture
gotMoves=Engine.getLegalMoves(board,piece.pieceCode);
filteredMoves=findCaptures(gotMoves,board);
assertEquals("Tile: "+piecePos+": Should not have exactly one capture",1,filteredMoves.size());
assertEquals("Tile: "+piecePos+": capture move should end on the enemy piece",
enemyPos.toString(),Coord.orderedPair(Move.getEndIndex(filteredMoves.get(0))));
assertTrue("Tile: "+piecePos+": capture move should flag capture",Move.isCapture(filteredMoves.get(0)));
assertEquals("Tile: "+piecePos+": capture move should have this Rook's code set in its pieceCode field",
piece.pieceCode,Move.getPieceCode(filteredMoves.get(0)));
board.setSquare(friendly.pieceCode,enemyPos.getIndex());//Don't capture friendlies
gotMoves=Engine.getLegalMoves(board,piece.pieceCode);
filteredMoves=findCaptures(gotMoves,board);
assertEquals("Tile: "+piecePos+": Should not capture friendlies at "+enemyPos+" Size of list should be 0"
,0,filteredMoves.size());
board.setSquare(PieceCode.Blank,enemyPos.getIndex());//Clean up for next run
}
board.setSquare(PieceCode.Blank,piecePos.getIndex());//remove the piece I placed for next square
}
}
}