JAVA HELPPPPPPPPPPP /** * Interacts with the user to take a shot. The procedure
ID: 3914418 • Letter: J
Question
JAVA HELPPPPPPPPPPP
/**
* Interacts with the user to take a shot. The procedure is as follows:
* 1 - Using the promptStr method, prompt the user for the "x-coord shot". The maximum value
* should be based on the width of the board. You will need to use the coordAlphaToNum
* and coordNumToAlpha methods to covert between int and String values of coordinates.
* 2 - Using the promptInt method, prompt the user for the "y-coord shot". The maximum value
* should be calculated based on the width of the board.
* 3 - Check the shot, using the takeShot method. If it returns:
* -1: Print out an error message "Coordinates out-of-bounds!", terminated by a new line.
* 3: Print out an error message "Shot location previously targeted!", terminated by a
* new line.
* 1 or 2: Update the cells in board and boardTrack with Config.HIT_CHAR('*') or
* Config.MISS_CHAR ('@')accordingly.
* This process should repeat until the takeShot method returns 1 or 2.
*
* @param sc The Scanner instance to read from System.in.
* @param board The computer opponent board (containing the ship placements).
* @param boardTrack The human player tracking board.
*/
public static void shootPlayer(Scanner sc, char[][] board, char[][] boardTrack) {
//FIXME
}
these are the codes that I have so far that can be used for above codes!!!
/**
* Prompts the user for an String value, displaying the following:
* "Enter the valName (min to max): "
* Note: There should not be a new line terminating the prompt. valName should contain the
* contents of the String referenced by the parameter valName. min and max should be the values
* passed in the respective parameters.
*
* After prompting the user, the method will read an entire line of input, trimming any trailing
* or leading whitespace. If the value read is (lexicographically ignoring case) between min and
* max (inclusive), that value is returned. Otherwise, "Invalid value." terminated by a new line
* is output and the user is prompted again.
*
* @param sc The Scanner instance to read from System.in.
* @param valName The name of the value for which the user is prompted.
* @param min The minimum acceptable String value (inclusive).
* @param min The maximum acceptable String value (inclusive).
* @return Returns the value read from the user.
*/
public static String promptStr(Scanner sc, String valName, String min, String max) {
System.out.println("Enter the " +valName+ "(" + min +" to "+ max + "): ");
String line = sc.nextLine();
line = line.trim();
String min2 = min.toUpperCase();
String max2 = max.toUpperCase();
line = line.toUpperCase();
if (min2.compareTo(line)<=0 && line.compareTo(max2)<=0) {
return line;
}
else {
System.out.println("Invalid value.");
return promptStr(sc,valName,min,max);
}
}
public static int coordAlphaToNum(String coord) {
int toNum = 0;
coord = coord.toUpperCase();
for(int i=0; i<coord.length(); i++) {
int eachChar = (coord.charAt(i)) - (int)'A';
toNum = toNum + eachChar*(int)Math.pow(26, coord.length()-i-1);
}
return toNum;
}
/**
* This method converts an int value into a base (or radix) 26 number, where the digits are
* represented by the 26 letters of the Latin alphabet. That is, A represents 0, B represents 1,
* C represents 2, ..., Y represents 24, and Z represents 25.
* A couple of examples: 17576 is BAAA, 11506714 is ZERTY.
*
* The algorithm to convert an int to a String representing these base 26 numbers is as follows:
* - Initialize res to the input integer
* - The next digit is determined by calculating the remainder of res with respect to 26
* - Convert this next digit to a letter based on 'A'
* - Set res to the integer division of res and 26
* - Repeat until res is 0
*
* @param coord The integer value to covert into an alpha coordinate.
* @return The alpha coordinate in base 26 as described above. If coord is negative, an empty
* string is returned.
*/
public static String coordNumToAlpha(int coord) {
String result = "";
while(coord>0) {
int res = coord % 26;
char character = (char)(res + 'A');
result = character + result;
coord = coord/26;
}
return result;
}
/**
* Prompts the user for an integer value, displaying the following:
* "Enter the valName (min to max): "
* Note: There should not be a new line terminating the prompt. valName should contain the
* contents of the String referenced by the parameter valName. min and max should be the values
* passed in the respective parameters.
*
* After prompting the user, the method will read an int from the console and consume an entire
* line of input. If the value read is between min and max (inclusive), that value is returned.
* Otherwise, "Invalid value." terminated by a new line is output and the user is prompted
* again.
*
* @param sc The Scanner instance to read from System.in.
* @param valName The name of the value for which the user is prompted.
* @param min The minimum acceptable int value (inclusive).
* @param min The maximum acceptable int value (inclusive).
* @return Returns the value read from the user.
*/
public static int promptInt(Scanner sc, String valName, int min, int max) {
System.out.print("Enter the "+valName+" (" + min + " to " + max +"): ");
int line = sc.nextInt();
if(min <= line && line <= max)
return line;
else{
System.out.print("Invalid value.");
return promptInt(sc, valName, min, max);
}
}
/**
* 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.
* NOT SO SURE IF I WROTE THIS RIGHT EITHER...
*/
public static int takeShot(char[][] board, int x, int y) {
int outcome = -2;
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;
}
}
}
return outcome;
}
/**
* Character values for displaying the different statuses of the game board cells.
*/
I HAVE SEPARATE CLASS "Config" FOR THIS NO WORRIES!
public static final char WATER_CHAR = '~'; // Water character (not yet targeted)
public static final char HIT_CHAR = '*'; // Hit character
public static final char MISS_CHAR = '@'; // Miss character
Explanation / Answer
public static void shootPlayer(Scanner sc, char[][] board, char[][] boardTrack) { String min = coordNumToAlpha(1).toUpperCase(); String max = coordNumToAlpha(board.length).toUpperCase(); boolean success = false; do { String xcoord = promptStr(sc, "x-coord shot", min, max); int xcoordVal = coordAlphaToNum(xcoord); int ycoordVal = promptInt(sc, "ycoord", 1, board[0].length); int status = takeShot(board, xcoordVal, ycoordVal); if(status == 1) { board[randX][randY] = Config.HIT_CHAR; success = true; } else if(status == 2) { board[randX][randY] = Config.MISS_CHAR; success = true; } else if(status == -1) { System.out.println("Coordinates out-of-bounds!"); } else if(status == 3) { System.out.println("Shot location previously targeted!"); } } while(!success); }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.