Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

With this step we are starting the development phase of project. Create the board Create the ship class Initialize 5 instances of the ship class:

With this step we are starting the development phase of project.

Create the board

Create the ship class

Initialize 5 instances of the ship class:

Frigate 2

Sub 3

Destroyer 3

Battleship 4

Aircraft Carrier 5

The ships take random spots on the board (programmatically)

For this exercise, show each ship position using the first letter of the ship name

Here's my code. My issue is spawning the whole ship's length, and choosing a direction randomly without having the ships interfere with each other

#include #include #include #include using namespace std; const int ROWS = 10; const int COLS = 10;

void Pos(string[][COLS], int, int, int, int, int, int, int, int, int, int);

class Frigate { public: int FRow, FCol; }; class Sub { public: int SRow, SCol; }; class Destroyer { public: int DRow, DCol; }; class Battleship { public: int BRow, BCol; }; class AircraftCarrier { public: int ARow, ACol; };

int main() {

string board[10][10]; int row, col;

Frigate Fri; Sub Sub; Destroyer Des; Battleship Bat; AircraftCarrier Air;

srand(time(0)); // initialize RNG

Fri.FRow = (rand() % 10) + 1; Fri.FCol = (rand() % 10) + 1;

Sub.SRow = (rand() % 10) + 1; Sub.SCol = (rand() % 10) + 1;

Des.DRow = (rand() % 10) + 1; Des.DCol = (rand() % 10) + 1;

Bat.BRow = (rand() % 10) + 1; Bat.BCol = (rand() % 10) + 1;

Air.ARow = (rand() % 10) + 1; Air.ACol = (rand() % 10) + 1;

Pos(board, Fri.FRow, Fri.FCol, Sub.SRow, Sub.SCol, Des.DRow, Des.DCol, Bat.BRow, Bat.BCol, Air.ARow, Air.ACol);

return 0;

}

void Pos(string arr[][COLS], int FRow, int FCol, int SRow, int SCol, int DRow, int DCol, int BRow, int BCol, int ARow, int ACol) { // Initialize the array with empty spaces for (int r = 0; r < ROWS; r++) { for (int c = 0; c < COLS; c++) { arr[r][c] = "[ ]"; } } cout << "The position of the boats are..." << endl; arr[FRow][FCol] = "[F]"; arr[SRow][SCol] = "[S]"; arr[DRow][DCol] = "[D]"; arr[BRow][BCol] = "[B]"; arr[ARow][ACol] = "[A]";

//Show the current positions of boats cout << " A B C D E F G H I J "; for (int r = 0; r < ROWS; r++) { if (r <= 8) { cout << " " << r + 1 << " "; } else { cout << r + 1 << " "; } for (int c = 0; c < COLS; c++) { cout << arr[r][c]; } cout << endl; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Practical Neo4j

Authors: Gregory Jordan

1st Edition

1484200225, 9781484200223

Students also viewed these Databases questions