JJJJJJJJJJJJAAAAAAAAAAAAAVVVVVVVVVVVVVAAAAAAAAAAAA HEEEEEEEEEEEEELLLLLLLLLLLLLPP
ID: 3916736 • Letter: J
Question
JJJJJJJJJJJJAAAAAAAAAAAAAVVVVVVVVVVVVVAAAAAAAAAAAA
HEEEEEEEEEEEEELLLLLLLLLLLLLPPPPPPPPPPPPPPPPPP!!!!!!!!!!!!!
/**
* Determines if a sequence of cells of length len in a game board is clear or not. This is used
* to determine if a ship will fit on a given game board. The x and y coordinates passed in as
* parameters represent the top-left cell of the ship when considering the grid.
*
* @param board The game board to search.
* @param xcoord The x-coordinate of the top-left cell of the ship.
* @param ycoord The y-coordinate of the top-left cell of the ship.
* @param len The length of the ship.
* @param dir true if the ship will be vertical, otherwise horizontal
* @return 1 if the cells to be occupied by the ship are all Config.WATER_CHAR, -1 if the cells
* to be occupied are not Config.WATER_CHAR, and -2 if the ship would go out-of-bounds
* of the board.
*/
public static int checkWater(char board[][], int xcoord, int ycoord, int len, boolean dir) {
int outcome = -2;
if(dir) {
for(int i = 0; i < board.length; ++i){
int count1 = 0;
int count2 = 0;
for(int j = 0; j < board.length; ++j){
if(board[j][i] == Config.WATER_CHAR){
++count1;
}
if(board[j][i] != Config.WATER_CHAR){
++count2;
}
}
if(count1 == board.length && count1 >= len){
outcome = 1;
}
else if(count2 == board.length && count2 >= len){
outcome = -1;
}
}
}
else
{
for(int i = 0; i < board.length; ++i){
int count1 = 0;
int count2 = 0;
for(int j = 0; j < board[i].length; ++j) {
if(board[i][j] == Config.WATER_CHAR){
++count1;
}
if(board[i][j] != Config.WATER_CHAR) {
++count2;
}
}
if(count1 == board[i].length && count1 >= len){
outcome = 1;
}
else if(count2 == board[i].length && count2 >= len){
outcome = -1;
}
}
}
return outcome;
}
WHY WOULD THIS CAUSE AN ERROR?
Failed to handle the out-of-bounds case in checkWater()
THIS IS WHAT THE ZYBOOK SAYS..
HOW DO I FIX IT?????
Explanation / Answer
public static int checkWater(char board[][], int xcoord, int ycoord, int len, boolean dir) {
int outcome = -2;
if(dir) {
for(int i = 0; i < board.length; ++i){
int count1 = 0;
int count2 = 0;
for(int j = 0; j < board[i].length; ++j){
if(board[j][i] == Config.WATER_CHAR){
++count1;
}
if(board[j][i] != Config.WATER_CHAR){
++count2;
}
}
if(count1 == board.length && count1 >= len){
outcome = 1;
}
else if(count2 == board.length && count2 >= len){
outcome = -1;
}
}
}
else
{
for(int i = 0; i < board.length; ++i){
int count1 = 0;
int count2 = 0;
for(int j = 0; j < board[i].length; ++j) {
if(board[i][j] == Config.WATER_CHAR){
++count1;
}
if(board[i][j] != Config.WATER_CHAR) {
++count2;
}
}
if(count1 == board[i].length && count1 >= len){
outcome = 1;
}
else if(count2 == board[i].length && count2 >= len){
outcome = -1;
}
}
}
return outcome;
}
Note:
changed board.length to board[i[.length
for(int j = 0; j < board[i].length; ++j){
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.