Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need help with the BGS class, I\'m providing the driver class BGB along with par

ID: 3745637 • Letter: N

Question

Need help with the BGS class, I'm providing the driver class BGB along with part of the BattleshipGameSimulation class.

Please comment every section to understand the code.

The Battleship Game Simulation class does the following:

1.Instantiates the Battleship Game Board with the 2-D array

2.Calls a method in the Battleship Game Board that generates 8 random locations in the 2-D array

3.Loop while you still haven't hit all 8 battleships, and while you have not reached the maximum
number of misses = 12:

0.) Create a hitsCounter (for number of battleships hit), a missesCounter (for number of shots missing a battleship), a maxMisses (a constant), and a maxHits (a constant). Initialize hits & misses counter to 0, the maxMisses to 12, and the maxHits to 8.

                         1.) Ask user to guess a location for ship position (a row and a column), and validate that it is
                               a valid location in the 5 x 5
gameboard.
                         2.) Call the getter of the Battleship GameBoard to see what is in that position
                         3a.) If that position has a '1', add 1 to the hitsCounter, and move an 'X' to that position,
                                 and give another chance (by adding 1 to the
chanceCounter);

                         3b.) If that position has a ‘0’, move a “-“ to that position, add 1 to the missesCounter.

                         3c.) If the position already has an “X” or a “-“ in it, give a message to the player to be more
                                careful, since he/she has already made that guess. Do not penalize them for being careless.
                         4a.) When the hitsCounter reaches the maxHits, game is over, and player wins.

                         4b.) When the missesCounter reaches the maxMisses, game is over, and player loses.
                         5.) display only the HITS and the Misses on the GameBoard , and show all the other locations
                             with a blank [ ] (hint: toString of GameBoard class). Also, display the value inside the hits
                              and misses counters.

4. After loop is done, determine if the player got all the ships, or not. Display the message:

"You won" or "You Lost".

5. Start all over again with another Battleship Game,

5a. prompt the user if they want to play another game

5b. reset all the counters, and

5c) keep track of all the games won and lost -every game played when user finishes playing completely. ( will need more counters, for games won and lost.

HINT: *arraylist).

--------------------------------------------------------------------------

package battleshipgamesimulation;

import java.util.Random;

/**

*

* @author

*/

public class BattleshipGameBoard

{

private char[][] myGameBoard = new char[5][5];

  

public BattleshipGameBoard()

{

for (int row = 0; row < 5; row++)

{

for (int col = 0; col < 5; col++)

{

myGameBoard[row][col] = '0';

}

}

}

  

public void generateRandomShips()

{

int row, col;

Random myRan = new Random();

int counter = 0;

  

while (counter < 8)

{

row = myRan.nextInt(5);

col = myRan.nextInt(5);

  

if (myGameBoard[row][col] == '1')

{

continue;

}

else

{

myGameBoard[row][col] = '1';

counter++;

}

}

}

  

public char getBattleshipGameBoard(int row, int col)

{

return myGameBoard[row][col];

}

  

public void setBattleshipGameBoard(int row, int col, char X)

{

myGameBoard[row][col] = X;

}

  

public String toString()

{

String gameBoardContent = "";

for (int row = 0; row < 5; row++)

{

for (int col = 0; col < 5; col++)

{

if (myGameBoard[row][col] != '0' && myGameBoard[row][col] != '1')

gameBoardContent += "[" + myGameBoard[row][col] + "] ";

else

gameBoardContent += "[" + " " + "] ";  

}

gameBoardContent += " ";

}

return gameBoardContent;

}

  

public String actualGameBoardDisplay()

{

String gameBoardContent = "";

for (int row = 0; row < 5; row++)

{

for (int col = 0; col < 5; col++)

{

gameBoardContent += "[" + myGameBoard[row][col] + "] ";

}

gameBoardContent += " ";

}

return gameBoardContent;

}

  

}

-------------------------------------------------------------

package battleshipgamesimulation;

/**

*

* @author

*/

public class BattleshipGameSimulation {

public static BattleshipGameBoard myGame;

/**

* @param args the command line arguments

*/

public static void main(String[] args) {

myGame = new BattleshipGameBoard();

myGame.generateRandomShips();

playGame();

// Start all over again with another Battleship Game, and keep track of all the games won and lost.

  

}

public static void playGame()

{

System.out.println(myGame);

//put logic for rest of program here

  

  

}

  

}

Explanation / Answer

package battleshipgamesimulation;

import java.io.*;

import java.util.Scanner;

/**

*

* @author

*/

public class BattleshipGameSimulation {

public static BattleshipGameBoard myGame;

public static final int maxMisses = 12;

public static final int maxHits = 8;

public static int won = 0;

public static int lost = 0;

/**

* @param args the command line arguments

*/

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

myGame = new BattleshipGameBoard();

// For First time Only

playGame();

int userResponse = 0;

// For User Menu Selection

while(userResponse!=4){

// Allow the user what he wants to do when he ends up with game

System.out.println("Please Choose one of the Options : ");

System.out.println("1. Do you want to play another Game?");

System.out.println("2. Reset All Counters");

System.out.println("3. Score");

System.out.println("4. Exit");

// Store the response

userResponse = in.nextInt();

switch(userResponse){

case 1 : {

playGame();

break;

}

case 2 : {

// Re-setting Counters

won=0;

lost = 0;

break;

}

case 3 : {

// Summary of Lost/Won Games

System.out.printf("Won : %d Lost : %d ", won, lost);

break;

}

case 4 : break;

default : {

System.out.println("Wrong Choice Re-enter");

}

}

}

}

public static void playGame()

{

Scanner in = new Scanner(System.in);

int hitsCounter=0, missesCounter=0, chanceCounter=1;

myGame.generateRandomShips();

while( chanceCounter==1 && hitsCounter<=maxHits && missesCounter<=maxMisses)

{

System.out.printf("Guess the Location of the Ship (row and Column) : ");

int m = in.nextInt();

int n = in.nextInt();

// Checking the Valid Position

if(!((m>=0 && m<5) && (n>=0 && n<5)))

{

System.out.println("InValid Location");

continue;

}

char X = myGame.getBattleshipGameBoard(m,n);

if(X=='1')

{

hitsCounter++;

chanceCounter++;

myGame.setBattleshipGameBoard(m,n,X);

continue;

}

if(X=='0')

{

missesCounter++;

myGame.setBattleshipGameBoard(m,n,'-');

}

if(X=='-')

{

missesCounter++;

chanceCounter--;

System.out.println("Be more careful, since you already made that guess.");

}

}

System.out.printf(" Hits : %d Misses : %d", hitsCounter, missesCounter);

if(maxHits==hitsCounter && missesCounter<=maxMisses){

System.out.println("You Won!!!!!!!");

won++;

} else {

System.out.println("You Lost!!!!!!!");

lost++;

}

// System.out.println(myGame);

}

}

Above answer maybe incorrect as i have doubt about X. What is 'X'? Is it position or value that [row][col].

Enjoy!!!!!!!!!!.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote