Question
Please, Separate Header and Implementation Files for the given C++ program. You should have 3 files for this project: header.h main.cpp Functions.cpp #include #include using
Please, Separate Header and Implementation Files for the given C++ program.
You should have 3 files for this project: header.h main.cpp Functions.cpp
#include
#include
using namespace std;
const bool CLEAR_SCREEN = true;
const int Carrier_SIZE = 5;
const int Battleship_SIZE = 4;
const int Cruiser_SIZE = 3;
const int Submarine_SIZE = 3;
const int Destroyer_SIZE = 2;
const int FLEET_SIZE = 5;
struct Point
{
int row;
int col;
};
struct Ship
{
string name;
int size;
int hitCount;
vector
};
struct PlayerBoard
{
char board[10][10];
Ship ships[FLEET_SIZE];
};
void initB(char [][10]);
void displayBoards(char [][10], char [][10]);
void initFleet(PlayerBoard&);
void setShip(PlayerBoard&, int);
void getValidShipInfo(int&, int&, char&, PlayerBoard&, int);
void initBoard(PlayerBoard&, PlayerBoard&);
void turn(PlayerBoard &, PlayerBoard &, int);
void clearScreen();
int main()
{
PlayerBoard P1;
PlayerBoard P2;
int player_turn;
int x;
x = 0;
initB(P1.board);
initB(P2.board);
initFleet(P1);
initFleet(P2);
initBoard(P1, P2);
cout << ". . . ";
player_turn = 1;
clearScreen();
while (1)
{
if (player_turn == 2)
{
turn(P2, P1, player_turn);
displayBoards(P2.board, P1.board);
player_turn = 1;
}
else
{
turn(P1, P2, player_turn);
displayBoards(P1.board, P2.board);
player_turn = 2;
}
cout << " . . . "
<< "Hand terminal to player "<< player_turn << "! "
<< "Enter any digit (0-9) to continue: ";
cin >> x;
clearScreen();
}
return (0);
}
bool game_won(char b[][10], int p_num)
{
for (size_t r = 0; r < 10; r++)
for (size_t c = 0; c < 10; c++)
if (b[r][c] == 's')
return (false);
cout << "You sunk the fleet!!! You win!!! ";
exit(1);
return (true);
}
void turn(PlayerBoard &P1, PlayerBoard &P2, int p_num)
{
char c_row;
int row;
int col;
c_row = 'x';
row = -1;
col = -1;
displayBoards(P1.board, P2.board);
cout << " ";
cin.clear();
do{
c_row = 'x';
row = -1;
col = -1;
cout << "Player "<< p_num<<": "
<< "Fire a shot: ";
cin >> c_row >> col;
cout << " ";
row = (char)toupper(c_row) - 'A';
col -= 1;
if (!(row >= 0 && row <= 9) || !(col >= 0 && col <= 9)
|| (P2.board[row][col] != ' ' && P2.board[row][col] != 's'))
cout << "Invalid ";
}
while (!(row >= 0 && row <= 9) || !(col >= 0 && col <= 9)
|| (P2.board[row][col] != ' ' && P2.board[row][col] != 's'));
if (P2.board[row][col] == 's')
{
cout << "Hit!!! ";
P2.board[row][col] = 'H';
}
if (P2.board[row][col] == ' ')
{
cout << "You Missed.";
P2.board[row][col] = 'M';
}
game_won(P2.board, p_num);
//cout << row << " " << col << endl;
}
void initBoard(PlayerBoard &P1, PlayerBoard &P2)
{
int x;
x = 0;
clearScreen();
cout << "Player 1 set your board. ";
displayBoards(P1.board, P2.board);
for (size_t i = 0; i < FLEET_SIZE; i++)
{
setShip(P1, i);
displayBoards(P1.board, P2.board);
}
cout << " . . . "
<< "Hand terminal to player 2! "
<< "Enter any digit (0-9) to continue: ";
cin >> x;
clearScreen();
cout << "Player 2 set your board. ";
displayBoards(P2.board, P1.board);
for (size_t i = 0; i < FLEET_SIZE; i++)
{
setShip(P2, i);
displayBoards(P2.board, P1.board);
}
cout << " . . . "
<< "Hand terminal to player 1! "
<< "Enter any digit (0-9) to continue: ";
cin >> x;
}
//A setShip function that takes in a PlayerBoard object by reference
void setShip(PlayerBoard &p, int shipIndex)
{
int row;
int col;
char orientation;
row = -1;
col = -1;
orientation = 'x';
getValidShipInfo(row, col, orientation, p, shipIndex);
if (orientation == 'v')
{
if (p.ships[shipIndex].size + row > 10)
{
cout << "Error: Ship placement is outside the board. ";
return (setShip(p, shipIndex));
}
for (size_t i = row; i < p.ships[shipIndex].size + row; i++)
{
p.board[i][col] = 's';
}
}
else if (orientation == 'h')
{
if (p.ships[shipIndex].size + col > 10)
{
cout << "Error: Ship placement is outside the board. ";
return (setShip(p, shipIndex));
}
for (size_t i = col; i < p.ships[shipIndex].size + col; i++)
{
p.board[row][i] = 's';
}
}
// cout << row << " " << col << " " << orientation << " " << shipIndex<< endl;
}
//A spaceOccupied function that takes in the PlayerBoard object,
bool spaceOccupied(PlayerBoard &p, int row, int col,
char orientation, int s_size)
{
if (orientation == 'v')
{
for (size_t i = row; i < s_size + row; i++)
{
if (p.board[i][col] != ' ')
{
cout << "Error: Space already occupied. ";
return (true);
}
}
}
else if (orientation == 'h')
{
for (size_t i = col; i < s_size + col; i++)
{
if (p.board[row][i] != ' ')
{
cout << "Error: Space already occupied. ";
return (true);
}
}
}
return (false);
}
/*
* check for invalid coordinates.
*/
bool check(int row, int col, char orientation)
{
if (!(row >= 0 && row <= 9))
{
cout << "Error: Coordinates are not valid. ";
return (false);
}
else if (!(col >= 0 && col <= 9))
{
cout << "Error: Coordinates are not valid. ";
return (false);
}
if (orientation != 'v' && orientation != 'h')
{
cout << "Error: Orientation of ship is invalid. ";
return (false);
}
return (true);
}
void getValidShipInfo(int &row, int &col, char &orientation,
PlayerBoard &p, int shipIndex)
{
char c_row;
c_row = 'x';
do{
cout << "Enter the starting coordinates of your "
<< p.ships[shipIndex].name << ": ";
cin >> c_row >> col;
cout << endl;
cout << "Enter the orientation of your "
<< p.ships[shipIndex].name << " (horizontal(h) or vertical(v)): ";
cin >> orientation;
cout << endl;
row = (char)toupper(c_row) - 'A';
col -= 1;
orientation = (char)tolower(orientation);
}
while (!check(row, col, orientation));
if (spaceOccupied(p, row, col,
orientation, p.ships[shipIndex].size))
getValidShipInfo(row, col, orientation, p, shipIndex);
}
void initFleet(PlayerBoard &p)
{
for (size_t i = 0; i < FLEET_SIZE; i++)
{
if (i == 0)
{
p.ships[i].name = "carrier";
p.ships[i].size = Carrier_SIZE;
}
else if (i == 1)
{
p.ships[i].name = "battleship";
p.ships[i].size = Battleship_SIZE;
}
else if (i == 2)
{
p.ships[i].name = "cruiser";
p.ships[i].size = Cruiser_SIZE;
}
else if (i == 3)
{
p.ships[i].name = "submarine";
p.ships[i].size = Submarine_SIZE;
}
else if (i == 4)
{
p.ships[i].name = "destroyer";
p.ships[i].size = Destroyer_SIZE;
}
p.ships[i].hitCount = 0;
for (size_t v = 0; v < p.ships[v].size; v++)
{
p.ships[i].points.push_back(Point());
p.ships[i].points[v].row = -1;
p.ships[i].points[v].col = -1;
}
}
}
/*
* Init boards to spaces;
*/
void initB(char a[][10])
{
for (size_t r = 0; r < 10; r++)
for (size_t c = 0; c < 10; c++)
a[r][c] = ' ';
}
/*
* The two boards should appear side by side.
*/
void displayBoards(char a[][10], char b[][10])
{
int c;
int r;
char A;
string l1;
string l2;
A = 'A';
c = 0;
r = 0;
l1 = " -----------------------------------------";
l2 = " ----------------------------------------- ";
cout << " Your Board "
<< " Enemy Board "
<< " 1 2 3 4 5 6 7 8 9 10"
<< " 1 2 3 4 5 6 7 8 9 10 ";
while (A <= 'J')
{
c = 0;
cout << l1 << l2 << A << ' ';
for (size_t i = 0; i < 11; i++)
{
cout << '|';
if (i < 10)
for (size_t s = 0; s < 3; s++)
{
if (s == 1)
cout << a[r][c++];
else
cout << ' ';
}
}
c = 0;
cout << " " << A++ << " ";
for (size_t i = 0; i < 11; i++)
{
cout << '|';
if (i < 10)
for (size_t s = 0; s < 3; s++)
{
if (s == 1 && b[r][c] != 's')
cout << b[r][c++];
else
cout << ' ';
}
}
r++;
cout << endl;
}
cout << l1 << l2;
}
/// @brief Utilizes an escape character sequence to clear the screen
void clearScreen()
{
cout << endl;
if (CLEAR_SCREEN)
{
cout << "\033c";
}
cout << endl;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started