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
  • import java.util.Arrays; import java.util.Random; import java.util.Scanner; /** * TODO Write a summary of the role of...

    import java.util.Arrays; import java.util.Random; import java.util.Scanner; /** * TODO Write a summary of the role of this class in the * MasterMind program. * * @author TODO add your name here */ public class MasterMind { /** * Prompts the user for a value by displaying prompt. * Note: This method should not add a new line to the output of prompt. * * After prompting the user, the method will consume an entire * line of input while reading...

  • 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;       ...

  • Programming Language : JAVA Write a class named Ship. Its purpose is to model a ship...

    Programming Language : JAVA Write a class named Ship. Its purpose is to model a ship in the BattleShip game and its placement on the Battleship board. Ships exist on a 10 x 10 battleship board with ten rows labeled A through J and columns labeled 1 through 9 and the final column labeled 0 to indicate the tenth column. We will refer to the Ship placed on the board at the origin which the pair (r,c) where in the...

  • 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...

  • 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...

  • Task 1 of 3 Game Rules Presented here, is an outline of the rules of the...

    Task 1 of 3 Game Rules Presented here, is an outline of the rules of the game around which this assignment is centred. This is not meant to be a completely exhaustive explanation, as you should defer to the specics provided in the Task descriptions for more detail, but rather this is to esh out the design and intention behind the game. 2.1.1 Game Board The game is played on a square board made of NxN characters where N is...

  • Assignment overview In this assignment, you will implement a simple game of Battleship. If you are...

    Assignment overview In this assignment, you will implement a simple game of Battleship. If you are unfamiliar with the game Battleship, there are tutorials online describing the game. In short, there are two players that each have a 10 by 10 grid where ships are placed. The players take turns taking shots at each other’s ships. A player wins when they have shot at all spaces on their opponent’s grid that are occupied by ship. To sink a ship, you...

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