JAVA HELPPPPPPPPPPPPP!!!!!!!!! /** * Takes a random shot on the game board. The
ID: 3914422 • Letter: J
Question
JAVA HELPPPPPPPPPPPPP!!!!!!!!!
/**
* Takes a random shot on the game board. The random process works as follows:
* 1 - Pick a random valid x-coordinate
* 2 - Pick a random valid y-coordinate
* 3 - Check the shot, using the takeShot method.
* This process should repeat until the takeShot method returns 1 or 2, then update the cells
* in board with Config.HIT_CHAR ('*') or Config.MISS_CHAR ('@') accordingly.
*
* Note: Unlike the placeRandomShip method, this method continues until it is successful. This
* may seem risky, but in this case the random process will terminate (find an untargeted cell)
* fairly quickly. For more details, see the appendix of the Big Program 1 subject.
*
* @param rand The Random object.
* @param board The human player game board.
*/
public static void shootComputer(Random rand, char[][] board) {
//FIXME
}
Explanation / Answer
/** * Takes a random shot on the game board. The random process works as follows: * 1 - Pick a random valid x-coordinate * 2 - Pick a random valid y-coordinate * 3 - Check the shot, using the takeShot method. * This process should repeat until the takeShot method returns 1 or 2, then update the cells * in board with Config.HIT_CHAR ('*') or Config.MISS_CHAR ('@') accordingly. * * Note: Unlike the placeRandomShip method, this method continues until it is successful. This * may seem risky, but in this case the random process will terminate (find an untargeted cell) * fairly quickly. For more details, see the appendix of the Big Program 1 subject. * * @param rand The Random object. * @param board The human player game board. */ public static void shootComputer(Random rand, char[][] board) { boolean success = false; do { int randX = rand.nextInt(board.length); int randY = rand.nextInt(board[randX].length); int status = takeShot(board, randX, randY); if(status == 1) { board[randX][randY] = Config.HIT_CHAR; success = true; } else if(status == 2) { board[randX][randY] = Config.MISS_CHAR; success = true; } } while(!success); }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.