Question

I need assistance with this method. Don't change the method type, decoding UTF8 import java.util.Scanner; /**...

I need assistance with this method. Don't change the method type, decoding UTF8

import java.util.Scanner;

    /**
     * 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) {
        //FIXME
        return 0;
    }

    /**
     * Checks the cells of the game board to determine if all the ships have been sunk.
     *
     * @param board The game board to check.
     * @return true if all the ships have been sunk, false otherwise.
     */
    public static boolean checkLost(char board[][]) {
        //FIXME
        return false;
    }

    /**
     * Places a ship into a game board. The coordinate passed in the parameters xcoord and ycoord
     * represent the top-left coordinate of the ship. The ship is represented on the game board by
     * the Character representation of the ship id. (For this method, you can assume that the id
     * parameter will only be values 1 through 9.)
     *
     * @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.
     * @param id The ship id, assumed to be 1 to 9.
     * @return false if the ship goes out-of-bounds of the board, true otherwise.
     */
    public static boolean placeShip(char board[][], int xcoord, int ycoord, int len, boolean dir,
                                    int id) {
        //FIXME
        return false;
    }

    /**
     * Randomly attempts to place a ship into a game board. The random process is as follows:
     *   1 - Pick a random boolean, using rand. True represents vertical, false horizontal.
     *   2 - Pick a random integer, using rand, for the x-coordinate of the top-left cell of the 
     *       ship. The number of integers to choose from should be calculated based on the width of
     *       the board and length of the ship such that the placement of the ship won't be 
     *       out-of-bounds.
     *   3 - Pick a random integer, using rand, for the y-coordinate of the top-left cell of the 
     *       ship. The number of integers to choose from should be calculated based on the height of
     *       the board and length of the ship such that the placement of the ship won't be 
     *       out-of-bounds.
     *   4 - Verify that this random location can fit the ship without intersecting another ship 
     *       (checkWater method). If so, place the ship with the placeShip method.
     *
     * It is possible for the configuration of a board to be such that a ship of a given length may
     * not fit. So, the random process will be attempted at most Config.RAND_SHIP_TRIES times.
     * 
     * @param board The game board to search.
     * @param len The length of the ship.
     * @param id The ship id, assumed to be 1 to 9..
     * @param rand The Random object.
     * @return true if the ship is placed successfully, false otherwise.
     */
    public static boolean placeRandomShip(char board[][], int len, int id, Random rand) {
        //FIXME
        return false;
    }

    /**
     * This method interacts with the user to place a ship on the game board of the human player and
     * the computer opponent. The process is as follows:
     *   1 - Print the user primary board, using the printBoard.
     *   2 - Using the promptChar method, prompt the user with "Vertical or horizontal? (v/h) ".
     *       A response of v is interpreted as vertical. Anything else is assumed to be horizontal.
     *   3 - Using the promptInt method, prompt the user for an integer representing the 
     *       "ship length", where the minimum ship length is Config.MIN_SHIP_LEN and the maximum 
     *       ship length is width or height of the game board, depending on the input of the user 
     *       from step 1.
     *   4 - Using the promptStr method, prompt the user for the "x-coord". The maximum value
     *       should be calculated based on the width of the board and the length of the ship. You 
     *       will need to use the coordAlphaToNum and coordNumToAlpha methods to covert between int
     *       and String values of coordinates.
     *   5 - Using the promptInt method, prompt the user for the "y-coord". The maximum value
     *       should be calculated based on the width of the board and the length of the ship.
     *   6 - Check if there is space on the board to place the ship. 
     *     6a - If so:
     *             - Place the ship on the board using placeShip. 
     *             - Then, call placeRandomShip to place the opponents ships of the same length.
     *             - If placeRandomShip fails, print out the error message (terminated by a new 
     *               line): "Unable to place opponent ship: id", where id is the ship id, and 
     *               return false.
     *     6b - If not:
     *             - Using promptChar, prompt the user with "No room for ship. Try again? (y/n): "
     *             - If the user enters a 'y', restart the process at Step 1. 
     *             - Otherwise, return false.
     *
     * @param sc The Scanner instance to read from System.in.
     * @param boardPrime The human player board.
     * @param boardOpp The opponent board.
     * @param id The ship id, assumed to be 1 to 9.
     * @param rand The Random object.
     * @return true if ship placed successfully by player and computer opponent, false otherwise.
     */
    public static boolean addShip(Scanner sc, char boardPrime[][], char boardOpp[][], int id,
                                  Random rand) {
        return false;
    }


 
0 0
Add a comment Improve this question Transcribed image text
Answer #1

The following information has been provided for the question above:

  • "Screen shot of method definitions of Java code" for understanding of code indentation.
  • "Text format code" to copy and paste in your IDE.

NOTE: Given question does not contains ‘main’ method description for execution of the program. So, I provided the method definitions in the answer as per the question requirements.

Screen shot of method definitions of Java code:

/import java.util.Scanner; *Determines if a sequence of cells of length len in a game board is clear * or not. This 13 used t

else for (int i=0; ǐ < board. length; ++1) int count! 0; int count2 = 0; boolean water-true for (int j 0; j < board [i]. leng

*Places a ship into a game board. The coordinate passed in the *parameters xcoord and ycoord represent the top-left coordinat

*Randomly attempts to place a ship into a game board. The random * process is as follows: 1 Pick a random boolean, using rand

*This method interacts with the user to place a ship on the game board * or the human player and the computer opponent. The p

String minStr: int user Length char continue-n printBoard (boardPrime, My Ships) choice= promptChar(sc, Vertical or horiz

Text format code:

import java.util.Scanner;
/**
* 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;
     boolean water = true;
     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;
     boolean water = true;
     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;

  }

  /**
   * Checks the cells of the game board to determine if all the ships have
   * been sunk.
   *
   * @param board
   *            The game board to check.
   * @return true if all the ships have been sunk, false otherwise.
   */
  public static boolean checkLost(char board[][])
  {
   for(int i = 0; i < board.length; i++)
    for(int j = 0; j < board[i].length; j++)
     if(board[i][j] >= 1 && board[i][j] <= 9)
      return false;
   return true;

  }

  /**
   * Places a ship into a game board. The coordinate passed in the
       * parameters xcoord and ycoord represent the top-left coordinate of the
       * ship. The ship is represented on the game board by the Character
   * representation of the ship id. (For this method, you can assume that
       * the id parameter will only be values 1 through 9.)
   *
   * @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.
   * @param id
   *            The ship id, assumed to be 1 to 9.
   * @return false if the ship goes out-of-bounds of the board, true
   *         otherwise.
   */
  public static boolean placeShip(char board[][], int xcoord, int ycoord, int len, boolean dir, int id)
  {
   if(dir == true)
   {
    if(ycoord + len >= Config.MAX_HEIGHT)
    {
     return false;
    }
    for(int i = ycoord; i < ycoord + len; ++i)
    {
     board[i][xcoord] = (char) ('0' + id);
    }
    return true;
   }
   else
   {
    if(xcoord + len >= Config.MAX_WIDTH)
    {
     return false;
    }
    for(int i = xcoord; i < xcoord + len; ++i)
    {
     board[ycoord][i] = (char) ('0' + id);
    }
    return true;
   }

  }

  /**
   * Randomly attempts to place a ship into a game board. The random
       * process is as follows: 1 - Pick a random boolean, using rand. True
       * represents
   * vertical, false horizontal. 2 - Pick a random integer, using rand,for
   * the x-coordinate of the top-left cell of the ship. The number of
       * integers to choose from should be calculated based on the width of
       * the board and length of the ship such that the placement of the ship
       * won't be out-of-bounds. 3 - Pick a random integer, using rand, for
       * the y-coordinate of the top-left cell of the ship. The number of
       * integers to choose from should be calculated based on the height of
       * the board and length of the ship such that the placement of the ship
       * won't be out-of-bounds. 4 - Verify that this random location can fit
       * the ship without intersecting another ship (checkWater method). If
       * so, place the ship with the placeShip method.
   * It is possible for the configuration of a board to be such that a
       * ship of a given length may not fit. So, the random process will be
       * attempted at most Config.RAND_SHIP_TRIES times.
   *
   * @param board
   *            The game board to search.
   * @param len
   *            The length of the ship.
   * @param id
   *            The ship id, assumed to be 1 to 9..
   * @param rand
   *            The Random object.
   * @return true if the ship is placed successfully, false otherwise.
   */
  public static boolean placeRandomShip(char board[][], int len, int id,Random rand)
  {
   boolean dir = rand.nextBoolean();
   for(int i = 0; i < Config.RAND_SHIP_TRIES; ++i)
   {
    int x = rand.nextInt(Config.MAX_WIDTH - len);
    int y = rand.nextInt(Config.MAX_HEIGHT - len);
    if(checkWater(board, x, y, len, dir) == 1)
    {
     boolean canPlace = placeShip(board, x, y, len, dir, id);
     if(canPlace == true)
     {
      return true;
     }
    }
   }
   return false;

  }

  /**
   * This method interacts with the user to place a ship on the game board
* of the human player and the computer opponent. The process is as
* follows: 1
   * - Print the user primary board, using the printBoard. 2 - Using the
   * promptChar method, prompt the user with "Vertical or horizontal?(v/h)
   * ". A response of v is interpreted as vertical. Anything else is
       * assumed to be horizontal. 3 - Using the promptInt method, prompt the
       * user for an integer representing the "ship length", where the minimum
       * ship length is Config.MIN_SHIP_LEN and the maximum ship length is
       * width or height of the game board, depending on the input of the user
       * from step 1. 4 - Using the promptStr method, prompt the user for the
       * "x-coord". The maximum value should be calculated based on the width
       * of the board and the length of the ship. You will need to use the
       * coordAlphaToNum and coordNumToAlpha methods to covert between int and
       * String values of coordinates. 5 – Using the promptInt method, prompt
       * the user for the "y-coord". The maximum value should be calculated
       * based on the width of the board and the length
   * of the ship. 6 - Check if there is space on the board to place the
       * ship. 6a - If so: - Place the ship on the board using placeShip. –
       * Then, call placeRandomShip to place the opponents ships of the same
       * length. – If placeRandomShip fails, print out the error message
       * (terminated by a new line): "Unable to place opponent ship: id",
       * where id is the ship id, and return false. 6b - If not: - Using
       * promptChar, prompt the user with "No room for ship. Try again?
       * (y/n): " - If the user enters a 'y',
   * restart the process at Step 1. - Otherwise, return false.
   *
   * @param sc
   *            The Scanner instance to read from System.in.
   * @param boardPrime
   *            The human player board.
   * @param boardOpp
   *            The opponent board.
   * @param id
   *            The ship id, assumed to be 1 to 9.
   * @param rand
   *            The Random object.
   * @return true if ship placed successfully by player and computer  
       * opponent, false otherwise.
   */
  public static boolean addShip(Scanner sc, char boardPrime[][], char boardOpp[][], int id, Random rand)
  {
   char choice;
   int numRows = boardPrime.length;
   int numCols = boardPrime[0].length;
   boolean shipPlace = false;
   boolean dir = false;
   int minValue;
   int maxValue;
   String minStr;
   int userLength;
   char continue = 'n';

   do
   {
    printBoard(boardPrime, "My Ships");
    choice = promptChar(sc, "Vertical or horizontal? (v/h): ");
    if(choice == 'v')
    {
     dir = false;
     minValue = Config.MIN_HEIGHT;
     maxValue = numRows;
     minStr = coordNumToAlpha(Config.MIN_HEIGHT - 1);
    }
    else
    {
     dir = true;
     minValue = Config.MIN_WIDTH;
     maxValue = numCols;
     minStr = coordNumToAlpha(Config.MIN_WIDTH - 1);
    }
    userLength = promptInt(sc, "ship length", Config.MIN_SHIP_LEN, maxValue);
    String userStrMax = coordNumToAlpha(maxValue - 1);
    String xcoordString = promptStr(sc, "x-coord", minStr, userStrMax);
    int xcoord = coordAlphaToNum(xcoordString);
    int ycoord = promptInt(sc, "y-coord", minValue, maxValue) - 1;
    if(checkWater(boardPrime, xcoord, ycoord, userLength,dir)== 1)
    {
     placeShip(boardPrime, xcoord, ycoord,userLength,dir, id);
     if(placeRandomShip(boardOpp, userLength, id, rand))
     {
      shipPlace = true;
     }
     else
     {
     System.out.println("Unable to place opponent ship:"+ id);
      shipPlace = false;
     }
    }
    else
    {
        continue=promptChar(sc,"No room for ship. Try again? (y/n):");
    }
   } while(continue == 'y');
   return shipPlace;

  }

Add a comment
Know the answer?
Add Answer to:
I need assistance with this method. Don't change the method type, decoding UTF8 import java.util.Scanner; /**...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This is my code for a TicTacToe game. However, I don't know how to write JUnit...

    This is my code for a TicTacToe game. However, I don't know how to write JUnit tests. Please help me with my JUnit Tests. I will post below what I have so far. package cs145TicTacToe; import java.util.Scanner; /** * A multiplayer Tic Tac Toe game */ public class TicTacToe {    private char currentPlayer;    private char[][] board;    private char winner;       /**    * Default constructor    * Initializes the board to be 3 by 3 and...

  • Here is the indexOf method that I wrote: public static int indexOf(char[] arr, char ch) {...

    Here is the indexOf method that I wrote: public static int indexOf(char[] arr, char ch) {            if(arr == null || arr.length == 0) {                return -1;            }            for (int i = 0; i < arr.length; i++) {                if(arr[i] == ch) {                    return i;                }            }        return -1;       ...

  • Make a FLOWCHART for the following JAVA Prime Number Guessing Game. import java.util.Random; import java.util.Scanner; public...

    Make a FLOWCHART for the following JAVA Prime Number Guessing Game. import java.util.Random; import java.util.Scanner; public class Project2 { //Creating an random class object static Random r = new Random(); public static void main(String[] args) {    char compAns,userAns,ans; int cntUser=0,cntComp=0; /* * Creating an Scanner class object which is used to get the inputs * entered by the user */ Scanner sc = new Scanner(System.in);       System.out.println("*************************************"); System.out.println("Prime Number Guessing Game"); System.out.println("Y = Yes , N = No...

  • This is my code for my game called Reversi, I need to you to make the...

    This is my code for my game called Reversi, I need to you to make the Tester program that will run and complete the game. Below is my code, please add comments and Javadoc. Thank you. public class Cell { // Displays 'B' for the black disk player. public static final char BLACK = 'B'; // Displays 'W' for the white disk player. public static final char WHITE = 'W'; // Displays '*' for the possible moves available. public static...

  • I am unsure how to add the following methods onto this code?? please help - rowValuesIncrease(int[][]...

    I am unsure how to add the following methods onto this code?? please help - rowValuesIncrease(int[][] t) A method that returns true if from left to right in any row, the integers are increasing, otherwise false. - columnValuesIncrease(int[][] t) A method that returns true if from top to bottom in any column, the integers are increasing, otherwise false. - isSetOf1toN(int[][] t) A method that returns true if the set of integers used is {1, 2, . . . , n}...

  • Complete the code: package hw4; import java.io.File; import java.io.IOException; import java.util.LinkedList; import java.util.Scanner; /* * This...

    Complete the code: package hw4; import java.io.File; import java.io.IOException; import java.util.LinkedList; import java.util.Scanner; /* * This class is used by: * 1. FindSpacing.java * 2. FindSpacingDriver.java * 3. WordGame.java * 4. WordGameDriver.java */ public class WordGameHelperClass { /* * Returns true if an only the string s * is equal to one of the strings in dict. * Assumes dict is in alphabetical order. */ public static boolean inDictionary(String [] dict, String s) { // TODO Implement using binary search...

  • In C++ please.. I only need the game.cpp. thanks. Game. Create a project titled Lab8_Game. Use...

    In C++ please.. I only need the game.cpp. thanks. Game. Create a project titled Lab8_Game. Use battleship.h, battleship.cpp that mentioned below; add game.cpp that contains main() , invokes the game functions declared in battleship.h and implements the Battleship game as described in the introduction. ——————————————- //battleship.h #pragma once // structure definitions and function prototypes // for the battleship assignment // 3/20/2019 #include #include #ifndef BATTLESHIP_H_ #define BATTLESHIP_H_ // // data structures definitions // const int fleetSize = 6; // number...

  • Assignment - Battleship In 1967, Hasbro toys introduced a childrens game named “Battleship”. In the next...

    Assignment - Battleship In 1967, Hasbro toys introduced a childrens game named “Battleship”. In the next two assignments you will be creating a one-player version of the game. The game is extremely simple. Each player arranges a fleet of ships in a grid. The grid is hidden from the opponent. Here is an ASCII representation of a 10x10 grid. The ‘X’s represent ships, the ‘~’s represent empty water. There are three ships in the picture: A vertical ship with a...

  • I need help with this. I need to create the hangman game using char arrays. I've...

    I need help with this. I need to create the hangman game using char arrays. I've been provided with the three following classes to complete it. I have no idea where to start. HELP!! 1. /** * This class contains all of the logic of the hangman game. * Carefully review the comments to see where you must insert * code. * */ public class HangmanGame { private final Integer MAX_GUESSES = 8; private static HangmanLexicon lexicon = new HangmanLexicon();...

  • I have already finished most of this assignment. I just need some help with the canMove...

    I have already finished most of this assignment. I just need some help with the canMove and main methods; pseudocode is also acceptable. Also, the programming language is java. Crickets and Grasshoppers is a simple two player game played on a strip of n spaces. Each space can be empty or have a piece. The first player plays as the cricket pieces and the other plays as grasshoppers and the turns alternate. We’ll represent crickets as C and grasshoppers as...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT