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

Goal Develop and implement a game of Battleship that pits a human player against

ID: 3762270 • Letter: G

Question

Goal

Develop and implement a game of Battleship that pits a human player against a

computer opponent.

Details

You may work in pairs on this project if you wish. Both people in each pair must email

me to confirm that you are working together.

The program should display the following items:

• The program name

• The number of boats remaining for both players

• The number of hits remaining for each of the human's boats. Small extra credit if

you limit the display to just boats that are still afloat.

• The human player's own board (the one on which the player places their boats)

• The human player's opponent board (the one on which the player tracks their

shots on the opponent)

The program should begin by initializing (of course) and then reading the positions of

the boats. Use arrow keys to position a boat, press Enter to lock in the boat's position,

then arrow keys again to choose a direction and finally Enter to finalize the boat's

location. Do not allow boats to hang over the edge of the board, and do not allow

boats to overlap other boats.

Once the boats are placed, place the computer boats randomly, obeying the two

restrictions given in the previous paragraph.

Choose who starts at random.

For the user's turn, have them choose a cell by coordinates A-J, 0-9. Upper or lower

case should be treated the same. Give the user the appropriate result:

• Miss

• Hit

• Hit and sunk (you don't need to tell the user which boat was sunk)

The computer should choose randomly and automatically process its choice.

Do not let either player select the same cell twice.

End the game if the user chooses 'q' (or 'Q') at any time.

If either player's fleet is sunk, end the game with an appropriate message.

What to turn in

Turn in a copy of your source code.

Explanation / Answer

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

const int BOARD_WIDTH = 15;
const int BOARD_HEIGHT = 10;
const int SHIP_TYPES = 5;

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

struct POINT {
     
   int Y;
};

struct SHIP {
     
   string name;
     
   int distance;
     
   POINT onGrid[5];
     
   bool hitFlag[5];
}ship[SHIP_TYPES];

struct PLAYER {
   char grid[BOARD_WIDTH][BOARD_HEIGHT];
}player[3];

enum DIRECTION {
HORIZONTAL,VERTICAL
};
struct PLACESHIPS
{
   DIRECTION direction;
   SHIP shipType;
};

bool gameRunning = false;


void LoadShips();
void ResetBoard();
void DrawBoard(int);
PLACESHIPS UserInputShipPlacement();
bool UserInputAttack(int&,int&,int);
bool GameOverCheck(int);

int main()
{
   LoadShips();
   ResetBoard();

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

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

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

             
           for (int p=1; p<aShip.shipType.distance; ++p)
           {
               if (aShip.direction == HORIZONTAL)
{
                   aShip.shipType.onGrid[p].X = aShip.shipType.onGrid[p-1].X+1;
                   aShip.shipType.onGrid[p].Y = aShip.shipType.onGrid[p-1].Y; }
               if (aShip.direction == VERTICAL)
{
                   aShip.shipType.onGrid[p].Y = aShip.shipType.onGrid[p-1].Y+1;
                   aShip.shipType.onGrid[p].X = aShip.shipType.onGrid[p-1].X; }

                 
               player[aplyr].grid[aShip.shipType.onGrid[p].X][aShip.shipType.onGrid[p].Y] = isSHIP;
           }
             
       }
         
   }

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

         
       bool goodInput = false;
       int x,y;
       while (goodInput == false)
{
           goodInput = UserInputAttack(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<BOARD_WIDTH; ++w)
{
           for (int h=0; h<BOARD_HEIGHT; ++h)
{
                 
               if (player[enemyPLAYER].grid[w][h] = isSHIP)
                   {
                       winner = false;
                       return winner;
                   }
       }}
     
   return winner;
}


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

PLACESHIPS UserInputShipPlacement()
{
   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>=BOARD_WIDTH) return tmp;
   if (y<0 || y>=BOARD_HEIGHT) return tmp;
     
   tmp.direction = (DIRECTION)d;
   tmp.shipType.onGrid[0].X = x;
   tmp.shipType.onGrid[0].Y = y;
   return tmp;
}

void LoadShips()
{
     
   ship[0].name = "Cruiser"; ship[0].distance = 2;
   ship[1].name = "Frigate"; ship[1].distance = 3;
   ship[2].name = "Submarine"; ship[2].distance = 3;
   ship[3].name = "Escort"; ship[3].distance = 4;
   ship[4].name = "Battleship"; ship[4].distance = 5;
}
void ResetBoard()
{
     
   for (int plyr=1; plyr<3; ++plyr)
   {
         
       for (int w=0; w<BOARD_WIDTH; ++w){
           for (int h=0; h<BOARD_HEIGHT; ++h){
               player[plyr].grid[w][h] = isWATER;
       }}
         
   }
}

void DrawBoard(int thisPlayer)
{
     
   cout << "PLAYER " << thisPlayer << "'s GAME BOARD ";
     

     
   cout << " ";
   for (int w=0; w<BOARD_WIDTH; ++w) {
       if (w < 10)
             
           cout << w << " ";
       else if (w >= 10)
             
           cout << w << " ";
   }
   cout << " ";

     
   for (int h=0; h<BOARD_HEIGHT; ++h){
       for (int w=0; w<BOARD_WIDTH; ++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 == BOARD_WIDTH-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