Programming Assignment using Java………………………….

//////////////////// ALL ASSIGNMENTS INCLUDE THIS SECTION ///////////////////// // // Title: Battleship.java // Files: Battleship.java, Config.java, TestBattleship.java // Course: CS 200- 2018 // // Author: Emily Stummer // Email: emstummer@wisc.edu // Lecturer's Name: Jim Williams // ///////////////////////////// CREDIT OUTSIDE HELP ///////////////////////////// // // Students who get help from sources other than their partner must fully // acknowledge and credit those sources of help here. Instructors and TAs do // not need to be credited here, but tutors, friends, relatives, room mates // strangers, etc do. If you received no outside help from either type of // source, then please explicitly indicate NONE. // // Persons: (identify each person and describe their help in detail) // Online Sources: (identify each URL and describe their assistance in detail) // /////////////////////////////// 80 COLUMNS WIDE /////////////////////////////// import java.util.Scanner; import java.util.Random; public class Battleship { /** * This method converts a String representing a base (or radix) 26 number into a decimal (or * base 10) number. The String representation of the base 26 number uses the letters of the * Latin alphabet to represent the 26 digits. That is, A represents 0, B represents 1, C * represents 2, …, Y represents 24, and Z represents 25. * * A couple of examples: BAAA = 1 * 26^3 + 0 * 26^2 + 0 * 26^1 + 0 * 26^0 = 17576 ZERTY = 25 * * 26^4 + 4 * 26^3 + 17 * 26^2 + 19 * 26^1 + 24 * 26^0 = 11506714 * * For this method: – use Math.pow to calculate the powers of 26. – don't assume that the input * is in any particular case; use toUpperCase(). – don't check that the input is only 'A' to * 'Z'. – calculate the value of each digit relative to 'A'. – start from either the first or * last character, and calculate the exponent based on the index of each character. * * @param coord The coordinate value in base 26 as described above. * @return The numeric representation of the coordinate. */ public static int coordAlphaToNum(String coord) { int ans = 0; for (int i = 0; i < coord.length(); i++) { int e = Character.toUpperCase(coord.charAt(i)) – 'A'; ans = ans + e * (int) Math.pow(26, coord.length() – i – 1); } return ans; } /** * 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 r = coord % 26; char c = (char) (r + 'A'); result = c + 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) { int num; while (sc.hasNextInt()) { System.out.print("Enter the " + valName + " (" + min + " to " + max + "): "); num = sc.nextInt(); if (num <= max && num >= min) { return num; } else { System.out.println("Invalid value."); } } return -1; } /** * 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) { String str; while (sc.hasNextInt()) { System.out.print("Enter the " + valName + " (" + min + " to " + max + "): "); str = sc.nextLine(); str = str.trim(); if (str.compareTo(max) < 0 && str.compareTo(min) > 0) { return str; } else { System.out.println("Invalid value."); } } return ""; } /** * Prompts the user for an char value. The prompt displayed is the contents of the String * referenced by the prompt parameter. Note: There should not be a new line terminating the * prompt. * * After prompting the user, the method will read an entire line of input and return the first * non-whitespace character in lower case. * * @param sc The Scanner instance to read from System.in * @param prompt The user prompt. * @return Returns the first non-whitespace character (in lower case) read from the user. If * there are no non-whitespace characters read, the null character is returned. */ public static char promptChar(Scanner sc, String prompt) { System.out.println(prompt); String input = sc.next(); input = input.trim(); input = input.toLowerCase(); return ''; } /** * Initializes a game board so that all the entries are Config.WATER_CHAR. * * @param board The game board to initialize. */ public static void initBoard(char board[][]) { for ( int i = 0; i < board.length; i++) { for ( int j = 0; j < board[ i ].length; j++) { board[ i ][ j ] = Config.WATER_CHAR; } } } /** * Prints the game boards as viewed by the user. This method is used to print the game boards as * the user is placing their ships and during the game play. * * Some notes on the display: – Each column printed will have a width of Config.MAX_COL_WIDTH. – * Each row is followed by an empty line. – The values in the headers and cells are to be right * justified. * * @param board The board to print. * @param caption The board caption. */ public static void printBoard(char board[][], String caption) { for ( int i = 0; i < board.length; i++ ) { for (int j = 0; j < board[ i ].length; j++) { System.out.printf("%" + Config.MAX_COL_WIDTH + "cnn", board[i][j]); } } System.out.println(caption); } /** * 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 xcoordBR = dir ? xcoord : xcoord + len – 1; int ycoordBR = dir ? ycoord + len -1 : ycoord; if (xcoord < 0 || ycoord < 0 || xcoordBR >= board[0].length || ycoordBR >= board.length) { return -2; } int val = 1; if (dir) { for (int y = ycoord; y

 

Looking for a Similar Assignment? Hire our Top Uk Tutors while you enjoy your free time! All papers are written from scratch and are 100% Original. Try us today! Active Discount Code FREE15