Reputation: 1683
I am creating simple chess board game in java, and they are running smoothly, however i am getting 2 failures while testing and i don't understand the logic behind them.
failure #1: player 1 has cloned his king and dominated expected <8> but it was <1>
failure #2: Symbols should be treated as an empty cells. expected <1> but it was <0>
that's my code:
import java.io.ObjectInputStream.GetField;
public class ChessBoard {
public static boolean belongsToPlayer(final char piece, final int player) { // method to check a piece is related to player 1 or 2
if (player == 0 && Character.isUpperCase(piece)) {
return true;
}
if (player == 1 && Character.isLowerCase(piece)) {
return true;
}
return false;
}
public static int getCount(final String[] board, final int player) { // method to return the number of pieces belonging to each player
int count = 0;
if (board.length == 0) {
return count = 0;
}
else if (board.length == 1) {
return count = 1;
}
else {
int size = board.length;
for (int i = 1; i < size; i++) {
for (int j = 0; j < board[i].length(); j++) {
if (belongsToPlayer(board[i].charAt(j), player)) {
count++;
}
}
}
}
return count;
}
}
and these are the test methods:
test #1
@Test(timeout = 1000)
public void test8KingsPlayer1() {
String[] board = {
"kkkkkkkk"
};
assertEquals("Player 1 has cloned his king and has dominated",
8, instance.getCount(board, PLAYER1));
}
test #2
@Test(timeout = 1000)
public void testAPieceOfCode() {
String[] board = {
"int factorIal(int n) {",
" int n = 8, r = 1; ",
" while (n-- > 1) ",
" r *= n; ",
" return r; ",
"} "
};
assertEquals("Symbols should be treated as empty cells.",
1, instance.getCount(board, PLAYER0));
}
anyone knows how to solve the problem?
Upvotes: 1
Views: 299
Reputation: 7760
Failure 1:
else if (board.length == 1) {
return count = 1;
}
It returns 1 right away because you passed it a String[] with 1 element.
Failure 2:
for (int i = 1; i < size; i++) {
...
if (belongsToPlayer(board[i] ...
Array indexing starts at 0. You started with 1, which skips the first string in the array. Since the capital I was in the first row, it wasn't counted.
Upvotes: 2