JAVA - Can somebody help me write a following testcode private static void testT
ID: 3918057 • Letter: J
Question
JAVA - Can somebody help me write a following testcode
private static void testTakeShot() {
//FIXME
}
This is my codes..
/**
* Checks the state of a targeted cell on the game board. This method does not change the
* contents of the game board.
*
* @return 3 if the cell was previously targeted. 2 if the shot would be a miss. 1 if the shot
* would be a hit. -1 if the shot is out-of-bounds.
*/
public static int takeShot(char[][] board, int x, int y) {
if (board[y][x] == Config.HIT_CHAR || board[y][x] == Config.MISS_CHAR) {
return 3;
}
if (board[y][x] == Config.WATER_CHAR) {
return 2;
}
if (board[y][x] != Config.WATER_CHAR && board[y][x] != Config.HIT_CHAR
&& board[y][x] != Config.MISS_CHAR) {
return 1;
}
if (x < 0 || x > board[y].length || y < 0 || y > board.length) {
return -1;
}
return 0;
}
Explanation / Answer
public static int takeShot(char[][] board, int x, int y) {
if (x < 0 || x > board[y].length || y < 0 || y > board.length) {
return -1;
}
if (board[y][x] == Config.HIT_CHAR) {
return 1;
}
if (board[y][x] == Config.WATER_CHAR) {
return 3;
}
if (board[y][x] == Config.MISS_CHAR) {
return 2;
}
return 0;
}
The above code is assuming that board[y][x] is the correct way of accessing the board contents and not board[x][y].
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.