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

Sample template program (does not need to stay in this form): #include #include

ID: 3857465 • Letter: S

Question

Sample template program (does not need to stay in this form):

#include
#include

using namespace std;

const int BOARDSIZE= 25;
const int BOATCOUNT = 6;
const int BOATLEN = 5;

const int W_UNK_BOAT = 0;
const int W_UNK_EMPTY = 1;
const int W_EXP_BOAT = 2;
const int W_EXP_EMPTY = 3;


void displayBoard(const int board[BOARDSIZE][BOARDSIZE]);

void calculateAttack(int board[BOARDSIZE][BOARDSIZE], int target[2]);

bool checkGameOver(const int board[BOARDSIZE][BOARDSIZE]);

void initBoard(int board[BOARDSIZE][BOARDSIZE]){
for(int r = 0; r < BOARDSIZE; r++){
for(int c = 0; c < BOARDSIZE; c++){
board[r][c] = W_UNK_EMPTY;
}
}
for (int i = 0; i < BOATCOUNT; i++){
int bx = rand() % BOARDSIZE;
int by = rand() % BOARDSIZE;
int horiz = rand() % 2;
int minbx, maxbx;
if(bx > BOARDSIZE / 2){
minbx = bx - BOATLEN;
maxbx = bx;
}
else{
minbx = bx;
maxbx = bx + BOATLEN;
}
for(int x = minbx; x < maxbx; x++){
if(horiz){
   board[x][by] = W_UNK_BOAT;
}
else{
   board[by][x] = W_UNK_BOAT;
}
}
}
}

int main(){

int waterState[BOARDSIZE][BOARDSIZE];
const int MAXTURNS = 10;
bool playingGame = true;
int target[2];

cout << "Enter a number for RND seed";
long sval;
cin >> sval;
srand(sval);
  
initBoard(waterState);
int turns = 0;
while(playingGame && turns < MAXTURNS){
displayBoard(waterState);
cout << "Enter a zero-indexed x y location to fire!";
cin >> target[0] >> target[1];

calculateAttack(waterState, target);
playingGame = checkGameOver(waterState);
turns = turns + 1;
}
if(!playingGame){
cout << "You Win! ";
}
else{
cout << "You Lose! ";
}
}

Sample program run:

Lose:

Enter a number for RND seed5

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 X;
        int Y;
};

struct SHIP {
      
        string name;
      
        int length;
      
        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;

//Functions
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 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 length of " << ship[thisShip].length << " ";
                        cout << "Where do you want it placed? ";
                      
                      
                        PLACESHIPS aShip;
                        aShip.shipType.onGrid[0].X = -1;
                        while (aShip.shipType.onGrid[0].X == -1)
                        {
                                aShip = UserInputShipPlacement();
                        }

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

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

                    
                        for (int i=1; i<aShip.shipType.length; ++i)
                        {
                                if (aShip.direction == 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.direction == 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[aplyr].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");
                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 any ships remain, game is NOT over
                                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].length = 2;
        ship[1].name = "Frigate"; ship[1].length = 3;
        ship[2].name = "Submarine"; ship[2].length = 3;
        ship[3].name = "Escort"; ship[3].length = 4;
        ship[4].name = "Battleship"; ship[4].length = 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 << "---------------------- ";

      
        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