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

Write a C++ program that simulates a one-player game of battleship. The user sho

ID: 3766524 • Letter: W

Question

Write a C++ program that simulates a one-player game of battleship. The user should be able to specify the board size and the number of ships that will be placed on the board. (Ships should be of varied sizes.) Once the board is generated with random ship placement, the user should be able to specify coordinates and have the game show them whether a "hit" or "miss" has occurred and display the resulting screen. Additional rules may be specified as needed. Program structural requirements:

The board will be constructed with a dynamic array

The ships will be constructed with a linked list.

Explanation / Answer

Answer :

#include <cstdlib>
#include <iostream>
#include <iomanip>

using namespace std;

void showBoard(char userGrid[][4]);

void checkInt(int &val)
{
   cin >> val;
  
   if (cin.fail())
   {
cin.clear();
cin.ignore(100,' ');
}
}

void showBoard(char userGrid[][4])
{
   cout << endl;
   cout << " Columns " << endl << endl;
     
for (int i = 0; i < 6; i++)
{
cout << "Row " << i+1 << " ";
for (int q = 0; q < 4; q++)
{
cout << userGrid[i][q];
if (q == 3)
{
    cout << endl;
}
}
}
}

int main ()
{

cout << endl << endl << " Battleship Game" << endl << endl;
cout << "There are 3 ships. One is 2 spaces, one is 3 spaces, one is 4 spaces. "
<< endl << endl;
cout << "Can you handle this?! ";
  
  


int row, col, connectA=0, connectB=0, value, connectC=0, tries=0, dip=0;


char userGrid[6][4] = {{'O','O','O','O'},
{'O','O','O','O'},
{'O','O','O','O'},
{'O','O','O','O'},
{'O','O','O','O'},
{'O','O','O','O'}};


int board[6][4] = {{1,2,1,1},
{1,2,1,4},
{1,2,1,4},
{1,1,1,4},
{3,3,1,4},
{1,1,1,1}};

do
{


cout << endl << endl << "This is what your board looks like. ";
cout << endl;
  
showBoard(userGrid);
  
  
cout << endl << endl << "Please choose a row (1-6): ";
checkInt(row);
  
  
cout << "Please choose a column (1-4): " ;
checkInt(col);
  
cout << endl;
  
if (col > 0 && col < 5)
{
if (row > 0 && row < 7)
{   
value = board[row-1][col-1];


  

  
  
if (value == 1)
{
cout << "The coordinate you picked was a miss. Please try again. ";
tries++;
}
  

else if (value == 2)
{
cout << ">>You have hit ship 1. ";
  
tries++;
connectA++;
  
userGrid[row-1][col-1] = 'X';
board[row-1][col-1] = 5;
  
if (connectA == 3)
{
cout << ">>You have dip ship 1!<<";
connectA = 0;
dip++;
}
  
else
{
cout << 3 - connectA << " more hit(s) and you sink it.<< ";
  
  
}
}
  
else if (value == 3)
{
cout << ">>You have hit ship 2. ";
  
tries++;
connectB++;
  
userGrid[row-1][col-1] = 'X';
board[row-1][col-1] = 5;
  
if (connectB == 2)
{
cout << ">>You have dip ship 2!<<" << endl;
connectB = 0;
dip++;
}
  
else
{
cout << 2 - connectB << " more hit(s) and you sink it.<< ";
  
  
}
}
  
else if (value == 4)
{
cout << ">>You have hit ship 3. ";
  
tries++;
connectC++;
  
userGrid[row-1][col-1] = 'X';
board[row-1][col-1] = 5;
  
if (connectC == 4)
{
cout << ">>You have dip ship 3!<<" << endl;
connectC = 0;
dip++;
}
  
else
{
cout << 4 - connectC << " more hit(s) and you sink it.<< ";
  
}
}
  
else if (value == 5)
{
cout << endl << "You already hit that space!!" << endl;

}
}

}

else
{
cout << endl << "Please enter a valid row and column number." << endl << endl;
}
  
  
if (dip == 3)
{
cout << endl;
  
showBoard(userGrid);
  
cout << endl << endl << "Congratulations! You have dip all 3 ships. You win the game! "
<< endl << endl << "It only took you " << tries << " tries!" << endl << endl;
  
}
}
  
  
  
while (dip < 3);
  

  
system("PAUSE");
return EXIT_SUCCESS;
}

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