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

Write a c++ program with the following specifications: The program should simula

ID: 3712932 • Letter: W

Question

Write a c++ program with the following specifications:

The program should simulate 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 program generates the board 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 collection of ships will be stored with a linked list

Explanation / Answer

Let me know if you have any doubt.

#include <iostream>
#include <string>
using namespace std;

const int MAX_ROWS = 10;
const int MAX_COLS = 10;
const int MAX_SHIPS = 5;
const int MAX_SHIPSIZE = 5;


const char isWATER = 247;
const char isHIT = 'X';
const char isSHIP = 'S';
const char isMISS = '0';

struct POINT
{
  
   int X;
   int Y;
};

struct SHIP
     {
  
   string name;
   int size;
   POINT onGrid[5];
   bool status[MAX_SHIPSIZE];
   Orientation orientation;
}

struct PLAYER
{
   char grid[MAX_ROWS][MAX_COLS];
}player[3];

enum Orientation {HORIZONTAL,VERTICAL,NONE};
struct PLACESHIPS
{
  
   SHIP shipType;
};

bool gameRunning = false;


//Functions
void InitializeShips();
void InitializeBoard();
void DisplayBoard(int);
PLACESHIPS PlaceShip();
bool IsValid(int&,int&,int);
bool GameOverCheck(int);

int main()
{
   InitializeShips();
   InitializeBoard();

   for (int array=1; array<3; ++array)
   {
      
       for (int thisShip=0; thisShip<MAX_SHIPS; ++thisShip)
       {
          
           system("cls");
           DisplayBoard(array);
          
           cout << " ";
           cout << "INSTRUCTIONS (Player " << array << ") ";
           cout << "You are about to place your ships. Format should be: ";
           cout << "Facing (0:Horizontal,1:Vertical), X (top-row) coords, Y (left-side) coords ";
           cout << "Example: 0 7 2    This would place a ship beginning at X:7 Y:2 going horizontal ";
           cout << "Ship to place: " << ship[thisShip].name << " which has a size of " << ship[thisShip].size << " ";
           cout << "Where do you want it placed? ";
          
          
           PLACESHIPS aShip;
           aShip.shipType.onGrid[0].X = -1;
           while (aShip.shipType.onGrid[0].X == -1)
           {
               aShip = PlaceShip();
           }

          
           aShip.shipType.size = ship[thisShip].size;
           aShip.shipType.name = ship[thisShip].name;

          
           player[array].grid[aShip.shipType.onGrid[0].X][aShip.shipType.onGrid[0].Y] = isSHIP;

          
           for (int i=1; i<aShip.shipType.size; ++i)
           {
               if (aShip.orientation == HORIZONTAL){
                   aShip.shipType.onGrid[i].X = aShip.shipType.onGrid[i-1].X+1;
                   aShip.shipType.onGrid[i].Y = aShip.shipType.onGrid[i-1].Y; }
               if (aShip.orientation == VERTICAL){
                   aShip.shipType.onGrid[i].Y = aShip.shipType.onGrid[i-1].Y+1;
                   aShip.shipType.onGrid[i].X = aShip.shipType.onGrid[i-1].X; }

              
               player[array].grid[aShip.shipType.onGrid[i].X][aShip.shipType.onGrid[i].Y] = isSHIP;
           }
          
       }
      
   }


  
   gameRunning = true;
   int thisPlayer = 1;
   do {
      
       int enemyPlayer;
       if (thisPlayer == 1) enemyPlayer = 2;
       if (thisPlayer == 2) enemyPlayer = 1;
       system("cls");
       DisplayBoard(enemyPlayer);

      
       bool goodInput = false;
       int x,y;
       while (goodInput == false)
       {
           goodInput = IsValid(x,y,thisPlayer);
       }

       if (player[enemyPlayer].grid[x][y] == isSHIP) player[enemyPlayer].grid[x][y] = isHIT;
       if (player[enemyPlayer].grid[x][y] == isWATER) player[enemyPlayer].grid[x][y] = isMISS;

       int aWin = GameOverCheck(enemyPlayer);
       if (aWin != 0) {
           gameRunning = false;
           break;
       }
      
       thisPlayer = (thisPlayer == 1) ? 2 : 1;
   } while (gameRunning);

   system("cls");
   cout << " CONGRATULATIONS!!! PLAYER " << thisPlayer << " HAS WON THE GAME! ";

   system("pause");
   return 0;
}


bool GameOverCheck(int enemyPLAYER)
{
   bool winner = true;
  
   for (int w=0; w<MAX_ROWS; ++w){
           for (int h=0; h<MAX_COLS; ++h)
               {
              
               if (player[enemyPLAYER].grid[w][h] = isSHIP)
                   {
                       winner = false;
                       return winner;
                   }
       }}
  
   return winner;
}


bool IsValid(int& x, int& y, int theplayer)
{
   cout << " PLAYER " << theplayer << ", ENTER COORDINATES TO ATTACK: ";
   bool goodInput = false;
   cin >> x >> y;
   if (x<0 || x>=MAX_ROWS) return goodInput;
   if (y<0 || y>=MAX_COLS) return goodInput;
   goodInput = true;
   return goodInput;
}

PLACESHIPS PlaceShip()
{
   int d, x, y;
   PLACESHIPS tmp;
  
   tmp.shipType.onGrid[0].X = -1;
   cin >> d >> x >> y;
   if (d!=0 && d!=1) return tmp;
   if (x<0 || x>=MAX_ROWS) return tmp;
   if (y<0 || y>=MAX_COLS) return tmp;
  
   tmp.orientation = (Orientation)d;
   tmp.shipType.onGrid[0].X = x;
   tmp.shipType.onGrid[0].Y = y;
   return tmp;
}

void InitializeShips()
{
  
   ship[0].name = "Cruiser"; ship[0].size = 2;
   ship[1].name = "Carrier"; ship[1].size = 3;
   ship[2].name = "Patrol Boat"; ship[2].size = 3;
   ship[3].name = "Destroyer"; ship[3].size = 4;
   ship[4].name = "Battleship"; ship[4].size = 5;
  
  
}
void InitializeBoard()
{
   for (int plyr=1; plyr<3; ++plyr)
   {
        for (int w=0; w<MAX_ROWS; ++w){
           for (int h=0; h<MAX_COLS; ++h){
               player[plyr].grid[w][h] = isWATER;
       }}
      
   }
}

void DisplayBoard(int thisPlayer)
{
  
   cout << "PLAYER " << thisPlayer << "'s GAME BOARD ";
   cout << "   ";
   for (int w=0; w<MAX_ROWS; ++w)
   {
       if (w < 10)
            cout << w << " ";
       else if (w >= 10)
            cout << w << " ";
   }
   cout << " ";
   for (int h=0; h<MAX_COLS; ++h){
       for (int w=0; w<MAX_ROWS; ++w)
       {
            if (w==0) cout << h << " ";
           if (w<10 && w==0) cout << " ";
           if (gameRunning == false) cout << player[thisPlayer].grid[w][h] << " ";
           if (gameRunning == true && player[thisPlayer].grid[w][h] != isSHIP)
           {
           cout << player[thisPlayer].grid[w][h] << " ";
           }
           else if (gameRunning == true && player[thisPlayer].grid[w][h] == isSHIP)
           {cout << isWATER << " ";}  
      
           if (w == MAX_ROWS-1) cout << " ";
       }
   }
}

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