I need help breaking this code into header .h, implemenetation .cpp, and client/
ID: 3685537 • Letter: I
Question
I need help breaking this code into header .h, implemenetation .cpp, and client/main files .cpp:
#include <cstdlib>
#include <iostream>
#include <ctime>
using namespace std;
const int ROWS = 5; //fixed game board size
const int COLUMNS = 5;
void outputBoard(char board[][COLUMNS]);
void placeShip(char board[][COLUMNS], int size);
void checkBoard(char board[][COLUMNS], bool &victory);
int main()
{
srand(time(NULL)); //initialize random number generator
char directions, board[ROWS][COLUMNS], gameBoard[ROWS][COLUMNS];
int sizeShip, numShip;
for (int i=0; i<ROWS; i++) //initialize game board
{
for (int j=0; j < COLUMNS; j++)
{
board[i][j] = '.';
gameBoard[i][j] = '.';
}
}
cout << "Welcome to Battleship! This is a single-player game. ";
do //try/catch block for user input (ship amount)
{
try {
cout << " Number of enemy battleships (greater than 0, less than 4): ";
cin >> numShip;
if(numShip < 1 || numShip > 3){
throw numShip;
}
} catch (int exH) {
cout << "Error: Invalid input. Try again. ";
cin.clear();
}
} while(numShip < 1 || numShip > 3);
for (int i = 1; i <= numShip; i++) //try/catch block for user input (ship size)
{
do
{
try {
cout << "Size of enemy battleship #" << i << " (greater than 0, less than 4): ";
cin >> sizeShip;
if (sizeShip < 1 || sizeShip > 3){
throw sizeShip;
}
} catch (int exH) {
cout << "Error: Invalid input. Try again. ";
cin.clear();
}
} while (sizeShip < 1 || sizeShip > 3);
placeShip(board, sizeShip);
}
cout << " Would you like directions (Y/N): ";
cin >> directions;
int nBombs = 25, iGuess, jGuess;
bool victory = false;
if (directions == 'N' || directions == 'n')
cout << " There are " << numShip << " enemy battle ships hidden here. ";
else
cout << " There are " << numShip << "enemy battle ships hidden here. "
<< "You have 25 bombs. Make your first move now. ";
cin.clear();
for (int n = 1; n <= nBombs && !victory; n++){ //try/catch block for user input (guesses)
outputBoard(gameBoard);
do {
try {
cout << " Bomb #" << n << " Row: ";
cin >> iGuess;
cout << "Column: ";
cin >> jGuess;
if ((iGuess < 0 || iGuess > 6) || (jGuess < 0 || jGuess > 6)) {
throw 01234;
}
} catch (int exH) {
cout << "Invalid input. Try again. ";
cin.clear();
}
} while ((iGuess < 0 || iGuess > 6) || (jGuess < 0 || jGuess > 6));
if (board[iGuess - 1][jGuess - 1] == 'S') {
cout << " Hit! ";
board[iGuess -1][jGuess -1] = '*';
gameBoard[iGuess - 1][jGuess - 1] = '*';
}
else if (board[iGuess - 1][jGuess - 1] == '.') {
cout << " Miss! ";
board [iGuess - 1][jGuess - 1] = 'o';
gameBoard[iGuess -1][jGuess - 1] = 'o';
}
}
cout << "Here is the final board:"; //end-game result summary
outputBoard(board);
cout << ""." is open sea. "
<< ""*" is a hit on an enemy battleship. "
<< ""o" is a missed bomb. ";
if (victory)
cout << "Congratulations, you win! ";
else
cout << "You didn't sink every battleship. Please try again. ";
return 0;
}
void outputBoard(char board[][COLUMNS])
{
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLUMNS; j++) {
cout << board[i][j];
}
cout << endl;
}
return;
}
void placeShip (char board[][COLUMNS], int size)
{
int iStart,jStart,ort;
Randomize: //random ship placement
do
{
iStart = rand()%ROWS;
jStart = rand()%COLUMNS;
ort = rand()%2;
}
while (board[iStart][jStart] == 'S'); //horizontal orientation
if (ort == 0) // ships cannot be placed off the board
{
while (jStart + size >= COLUMNS) {
jStart = rand()%COLUMNS;
}
for (int j = jStart; j < jStart + size; j++) {
if (board[iStart][j] == 'S')
goto Randomize;
}
for (int j = jStart; j < jStart + size; j++) { // ships cannot be stacked ontop of one another
board[iStart][j] = 'S';
}
}
if (ort == 1) { //vertical orientation
while (iStart + size >= ROWS) {
iStart = rand()%ROWS;
}
for (int i = iStart; i < iStart + size; i++) {
if (board[i][jStart] == 'S')
goto Randomize;
}
for (int i = iStart; i < iStart + size; i++) {
board[i][jStart] = 'S';
}
}
return;
}
void checkBoard (char board[][COLUMNS], bool &victory) {
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLUMNS; j++) {
if (board[i][j] == 'S')
victory = false;
}
}
Explanation / Answer
The class declaration goes into the header file. It is important that you add the #ifndef include guards, or if you are on a MS platform you also can use #pragma once.
header.h
const int ROWS = 5; //fixed game board size
const int COLUMNS = 5;
void outputBoard(char board[][COLUMNS]);
void placeShip(char board[][COLUMNS], int size);
void checkBoard(char board[][COLUMNS], bool &victory);
implementation.cpp
#include <cstdlib>
#include <iostream>
#include <ctime>
using namespace std;
void outputBoard(char board[][COLUMNS])
{
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLUMNS; j++) {
cout << board[i][j];
}
cout << endl;
}
return;
}
void placeShip (char board[][COLUMNS], int size)
{
int iStart,jStart,ort;
Randomize: //random ship placement
do
{
iStart = rand()%ROWS;
jStart = rand()%COLUMNS;
ort = rand()%2;
}
while (board[iStart][jStart] == 'S'); //horizontal orientation
if (ort == 0) // ships cannot be placed off the board
{
while (jStart + size >= COLUMNS) {
jStart = rand()%COLUMNS;
}
for (int j = jStart; j < jStart + size; j++) {
if (board[iStart][j] == 'S')
goto Randomize;
}
for (int j = jStart; j < jStart + size; j++) { // ships cannot be stacked ontop of one another
board[iStart][j] = 'S';
}
}
if (ort == 1) { //vertical orientation
while (iStart + size >= ROWS) {
iStart = rand()%ROWS;
}
for (int i = iStart; i < iStart + size; i++) {
if (board[i][jStart] == 'S')
goto Randomize;
}
for (int i = iStart; i < iStart + size; i++) {
board[i][jStart] = 'S';
}
}
return;
}
void checkBoard (char board[][COLUMNS], bool &victory) {
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLUMNS; j++) {
if (board[i][j] == 'S')
victory = false;
}
}
main.cpp
#include <cstdlib>
#include <iostream>
#include <ctime>
int main()
{
srand(time(NULL)); //initialize random number generator
char directions, board[ROWS][COLUMNS], gameBoard[ROWS][COLUMNS];
int sizeShip, numShip;
for (int i=0; i<ROWS; i++) //initialize game board
{
for (int j=0; j < COLUMNS; j++)
{
board[i][j] = '.';
gameBoard[i][j] = '.';
}
}
cout << "Welcome to Battleship! This is a single-player game. ";
do //try/catch block for user input (ship amount)
{
try {
cout << " Number of enemy battleships (greater than 0, less than 4): ";
cin >> numShip;
if(numShip < 1 || numShip > 3){
throw numShip;
}
} catch (int exH) {
cout << "Error: Invalid input. Try again. ";
cin.clear();
}
} while(numShip < 1 || numShip > 3);
for (int i = 1; i <= numShip; i++) //try/catch block for user input (ship size)
{
do
{
try {
cout << "Size of enemy battleship #" << i << " (greater than 0, less than 4): ";
cin >> sizeShip;
if (sizeShip < 1 || sizeShip > 3){
throw sizeShip;
}
} catch (int exH) {
cout << "Error: Invalid input. Try again. ";
cin.clear();
}
} while (sizeShip < 1 || sizeShip > 3);
placeShip(board, sizeShip);
}
cout << " Would you like directions (Y/N): ";
cin >> directions;
int nBombs = 25, iGuess, jGuess;
bool victory = false;
if (directions == 'N' || directions == 'n')
cout << " There are " << numShip << " enemy battle ships hidden here. ";
else
cout << " There are " << numShip << "enemy battle ships hidden here. "
<< "You have 25 bombs. Make your first move now. ";
cin.clear();
for (int n = 1; n <= nBombs && !victory; n++){ //try/catch block for user input (guesses)
outputBoard(gameBoard);
do {
try {
cout << " Bomb #" << n << " Row: ";
cin >> iGuess;
cout << "Column: ";
cin >> jGuess;
if ((iGuess < 0 || iGuess > 6) || (jGuess < 0 || jGuess > 6)) {
throw 01234;
}
} catch (int exH) {
cout << "Invalid input. Try again. ";
cin.clear();
}
} while ((iGuess < 0 || iGuess > 6) || (jGuess < 0 || jGuess > 6));
if (board[iGuess - 1][jGuess - 1] == 'S') {
cout << " Hit! ";
board[iGuess -1][jGuess -1] = '*';
gameBoard[iGuess - 1][jGuess - 1] = '*';
}
else if (board[iGuess - 1][jGuess - 1] == '.') {
cout << " Miss! ";
board [iGuess - 1][jGuess - 1] = 'o';
gameBoard[iGuess -1][jGuess - 1] = 'o';
}
}
cout << "Here is the final board:"; //end-game result summary
outputBoard(board);
cout << ""." is open sea. "
<< ""*" is a hit on an enemy battleship. "
<< ""o" is a missed bomb. ";
if (victory)
cout << "Congratulations, you win! ";
else
cout << "You didn't sink every battleship. Please try again. ";
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.