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

The Battleship Game Simulation class does the following: Instantiates the Battle

ID: 3747966 • Letter: T

Question

The Battleship Game Simulation class does the following:

Instantiates the Battleship Game Board with the 2-D array

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

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

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.

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

"You won" or "You Lost".

6.) Start all over again with another Battleship Game, reset all the counters, and keep track of all the games won and lost. (Hint: will need more counters, for games won and lost).

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();
// 1) 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
/*
Loop while you still haven't won and you haven't lost
1.) Ask user to guess a location for ship position
2.) Call the getter of the Battleship GameBoard to see what is in that position
3a.) if that position has a '1', score in the hitsCounter, and move an 'X' to that postion.
3b.) if that position has a '0', add 1 to the missesCounter, and move a "-" to that position
4.) display ONLY the HITS and the MISSES on the GameBoard - show all the other locations with a blank [ ]
  
After loop is done, determine if the user got all the ships, or not. Display the message "You won" or "You Lost".

*/
}
  
}

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;
}
  
  
  
  
  
  
}

Explanation / Answer

//Hi student get code and feel free to reach me if you want more clarification or at any point feeling any //dout

import java.util.Random;

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;
}
}

/*Tester class*/

import java.util.Scanner;

//Driver class to test functionality
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();
// 1) Start all over again with another Battleship Game,
//and keep track of all the games won and lost.

}
public static void playGame()
{
int win = 0;
int loss = 0;
  
BattleshipGameBoard bgm= new BattleshipGameBoard();
bgm.generateRandomShips();
/*for (int row = 0; row < 5; row++)
{
for (int col = 0; col < 5; col++)
{
myGameBoard[row][col] = ' ';
}
}*/
char[][] myGameBoard1 = new char[5][5];
for (int row = 0; row < 5; row++)
{
for (int col = 0; col < 5; col++)
{
myGameBoard1[row][col] = ' ';
}
}
System.out.println(myGame);
Scanner in = new Scanner(System.in);
// Loop while you still haven't won and you haven't lost
System.out.println("Enter row and column value from 0 to 4");
while(win<9&&loss<12){
// Ask user to guess a location for ship position
System.out.println("Enter row number");
int a=in.nextInt();
System.out.println("Enter column number");
int b=in.nextInt();
//Call the getter of the Battleship GameBoard to see what is in that position
char cint =bgm.getBattleshipGameBoard(a,b);
if(cint!= '0')
{
System.out.println("Ship Destroyed having position "+a+" "+b);
win++;
/*if that position has a '1', score in the hitsCounter, and move an 'X' to that postion.
//display ONLY the HITS and the MISSES on the GameBoard - show all the other locations with a blank [ ]
*/
myGameBoard1[a][b] = '*';
}
else
{
loss++;
System.out.println("Ship Missed position "+a+" "+b);
/*if that position has a '0', add 1 to the missesCounter,
and move a "-" to that position
//display ONLY the HITS and the MISSES on the GameBoard
- show all the other locations with a blank [ ]
*/
myGameBoard1[a][b] = '-';
}
}
//put logic for rest of program here
/*
4.) display ONLY the HITS and the MISSES on the GameBoard - show all the other locations with a blank [ ]

After loop is done, determine if the user got all the ships, or not. Display the message "You won" or "You Lost".

*/
if(win==8)
System.out.println("You Win");
else
System.out.println("You Loose");
System.out.println("Actual Position of Ship is :");
System.out.println(bgm.actualGameBoardDisplay());
}
  
  
  
}