-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBishopTest.java
285 lines (223 loc) · 6.89 KB
/
BishopTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package a1;
import static org.junit.Assert.*;
import org.junit.Test;
public class BishopTest {
/*
* Tests if Bishop can moves to all the legal moves
*/
@Test
public void testLegalMoves() {
ChessBoard board = new ChessBoard();
board.initialize();
try {
arrangeBoard(board);
ChessPiece p1 = board.getPiece("c1");
ChessPiece p2 = board.getPiece("f1");
ChessPiece p3 = board.getPiece("c8");
ChessPiece p4 = board.getPiece("f8");
firstWhiteBishop(board, p1);
secondWhiteBishop(board, p2);
firstBlackBishop(board, p3);
secondBlackBishop(board, p4);
testLegalMovesException1();
testLegalMovesException2();
testLegalMovesException3();
} catch(Exception e) {
System.out.println("Exception Occurred");
}
}
/*
* Tests if 2nd black bishop moves correctly
* Possible moves for 2nd black bishop are : e7, d6, c5, g7, h6
*/
private void secondBlackBishop(ChessBoard board, ChessPiece p4)
throws IllegalMoveException, IllegalPositionException {
board.move("f8", "e7");
assertTrue( p4 == board.getPiece("e7"));
board.move("e7", "f8");
board.move("f8", "d6");
assertTrue( p4 == board.getPiece("d6"));
board.move("d6", "f8");
board.move("f8", "c5");
assertTrue( p4 == board.getPiece("c5"));
board.move("c5", "f8");
board.move("f8", "g7");
assertTrue( p4 == board.getPiece("g7"));
board.move("g7", "f8");
board.move("f8", "h6");
assertTrue( p4 == board.getPiece("h6"));
board.move("h6", "f8");
}
/*
* Tests if 1st black bishop moves correctly
* Possible moves for 1st black bishop are : b7, a6, d7, e6, f5
*/
private void firstBlackBishop(ChessBoard board, ChessPiece p3)
throws IllegalMoveException, IllegalPositionException {
board.move("c8", "b7");
assertTrue( p3 == board.getPiece("b7"));
board.move("b7", "c8");
board.move("c8", "a6");
assertTrue( p3 == board.getPiece("a6"));
board.move("a6", "c8");
board.move("c8", "d7");
assertTrue( p3 == board.getPiece("d7"));
board.move("d7", "c8");
board.move("c8", "e6");
assertTrue( p3 == board.getPiece("e6"));
board.move("e6", "c8");
board.move("c8", "f5");
assertTrue( p3 == board.getPiece("f5"));
board.move("f5", "c8");
}
/*
* Tests if 2nd white bishop moves correctly
* Possible moves for 2nd white bishop are : g2, h3, e2, d3, c4
*/
private void secondWhiteBishop(ChessBoard board, ChessPiece p2)
throws IllegalMoveException, IllegalPositionException {
board.move("f1", "g2");
assertTrue( p2 == board.getPiece("g2"));
board.move("g2", "f1");
board.move("f1", "h3");
assertTrue( p2 == board.getPiece("h3"));
board.move("h3", "f1");
board.move("f1", "e2");
assertTrue( p2 == board.getPiece("e2"));
board.move("e2", "f1");
board.move("f1", "d3");
assertTrue( p2 == board.getPiece("d3"));
board.move("d3", "f1");
board.move("f1", "c4");
assertTrue( p2 == board.getPiece("c4"));
board.move("c4", "f1");
}
/*
* Tests if 1st white bishop moves correctly
* Possible moves for 1st white bishop are : d2, e3, f4, b2, a3
*/
private void firstWhiteBishop(ChessBoard board, ChessPiece p1)
throws IllegalMoveException, IllegalPositionException {
board.move("c1", "d2");
assertTrue( p1 == board.getPiece("d2"));
board.move("d2", "c1");
board.move("c1", "e3");
assertTrue( p1 == board.getPiece("e3"));
board.move("e3", "c1");
board.move("c1", "f4");
assertTrue( p1 == board.getPiece("f4"));
board.move("f4", "c1");
board.move("c1", "b2");
assertTrue( p1 == board.getPiece("b2"));
board.move("b2", "c1");
board.move("c1", "a3");
assertTrue( p1 == board.getPiece("a3"));
board.move("a3", "c1");
}
/*
* Arranges the board by moving white and black pawns to clear the way to test Bishop
* Kills the pawns to clear the board.
*/
private void arrangeBoard(ChessBoard board) throws IllegalMoveException {
arrangeBoard2(board);
//kill the pawns
board.move("a5", "b4");
board.move("b5", "c4");
board.move("c5", "d4");
board.move("d5", "e4");
board.move("e5", "f4");
board.move("f5", "g4");
board.move("g5", "h4");
board.move("a4", "a5");
}
/*
* Verifies the bishop doesn't move on it's own team members
*/
@Test
public void testLegalMovesException1() {
ChessBoard board = new ChessBoard();
board.initialize();
try {
//CHeck if bishop does not move on it's own team
board.move("c1", "c2");
board.move("f1", "f2");
board.move("c8", "c7");
board.move("f8", "f7");
} catch(IllegalMoveException e) {
return;
}
fail("IllegalMoveException expected");
}
/*
* This test verifies that Bishop does not move horizontally left or right
*/
@Test
public void testLegalMovesException2() {
ChessBoard board = new ChessBoard();
board.initialize();
try {
arrangeBoard2(board);
//move the bishop where they can move horizontally left and right
board.move("c1", "d2");
board.move("f1", "g2");
board.move("c8", "b7");
board.move("f8", "e7");
//Verify that bishop does not move horizontally left and right
board.move("d2", "b2");
board.move("d2", "c2");
board.move("g2", "h2");
board.move("g2", "f2");
board.move("b7", "c7");
board.move("b7", "a7");
board.move("e7", "d7");
board.move("e7", "f7");
} catch(IllegalMoveException e) {
return;
}
fail("IllegalMoveException expected");
}
private void arrangeBoard2(ChessBoard board) throws IllegalMoveException {
// move the black pawns
board.move("h7", "h5");
board.move("g7", "g5");
board.move("f7", "f5");
board.move("e7", "e5");
board.move("d7", "d5");
board.move("c7", "c5");
board.move("b7", "b5");
board.move("a7", "a5");
//move the white pawns
board.move("a2", "a4");
board.move("b2", "b4");
board.move("c2", "c4");
board.move("d2", "d4");
board.move("e2", "e4");
board.move("f2", "f4");
board.move("g2", "g4");
board.move("h2", "h4");
}
/*
* This test verifies that the bishop does not move vertically left or right
*/
@Test
public void testLegalMovesException3() {
ChessBoard board = new ChessBoard();
board.initialize();
try {
arrangeBoard2(board);
//move the bishop where they can move horizontally left and right
board.move("c1", "d2");
board.move("f1", "g2");
board.move("c8", "b7");
board.move("f8", "e7");
//Verify that bishop does not move vertically
board.move("d2", "d3");
board.move("g2", "g3");
board.move("b7", "b6");
board.move("e7", "e6");
} catch(IllegalMoveException e) {
return;
}
fail("IllegalMoveException expected");
}
}