Background: The Pacific Ocean is a powder keg ripe for explosion with the Imperi
ID: 3769912 • Letter: B
Question
Background: The Pacific Ocean is a powder keg ripe for explosion with the Imperial Red Fleet (IRF) making trouble by terrorizing the shipping lanes between us and our interests in the Far East. The Pacific Blue Fleet (PBF) needs a capable commander to lead them in this time of peril. You have been selected as the new commander of the United States Pacific Blue Fleet. Reconnaissance units have located the enemy fleet. You job is to engage the enemy fleet and annihilate them completely. The IRF fleet is well equipped and possesses ships that rival your own. They would like nothing more than to send your fleet to the bottom of the ocean. This battle would be decided by the cunning and tactics of the commander.
Mode: The game should have three player modes: player vs. player or player vs. computer or computer vs. computer.
Goal: Strategically place your ships throughout your part of the ocean to keep them away from the enemy. Try and sink the enemy by hitting their ships before they hit you!
Skill Level: When the game is in player vs. computer mode, there should be 3 skills levels. The skill levels are as follows:
Beginner: The computer randomly guesses selects coordinates. No intelligence involved.
Intermediate: The computer will make an educated guess passed on previous hit. For instance, if computer achieved hit, it should check next available coordinate along x coordinate.
Expert: The computer will make an educated guess passed on previous hit. For instance, if computer achieved hit, it should check next available coordinate along x axis and/or y axis.
Ocean Grids: The Pacific Ocean is broken up into two ocean grids: one grid for each fleet. An ocean grid consists of a 10 x 10 grid. Each position on the grid will be represented by a two positive integers that constitutes a XY coordinate.
Ships: There are a total of 5 ships for both the PBF and the IRF respectively. Each fleet has the same complement of ships. The description of the ships is listed below including the number of hits needed to sink a ship:
Aircraft Carrier 5 hits
Battleship – 4 hits
Cruiser – 3 Hits
Destroyer 3 hits
Submarine – 2 Hits
Deployment: Each ship will be strategically placed in their part of the ocean. Ships must be placed horizontally or vertically. It can not be diagonal. Basically, for each ship placement, either the x or y coordinate must be the same for every coordinate of that particular ship. For example, a cruiser could be: (8,1) (8,2) (8,3) or a battleship could be: (5,3) (6,3) (7,3) (8,3)
How to Begin: When the game begins, the program should prompt the PBF player to enter the coordinates of every ship in the PBF fleet one at a time. Once all of your ships have been entered, the IRF player will enter the coordinates for all of the ships of their fleet.
Game Rules: The PBF player will fire first by entering a XY coordinate. If the entered coordinate scores a hit (i.e. the entered coordinate corresponds to a position on the ocean grid occupied by an enemy ship) the program should indicate that an enemy ship was hit. The PDF player would be allowed to enter in another coordinate. Once the PBF player one misses (selecting a coordinate that is not occupied by an enemy ship), player 2 would be asked to enter a coordinate. Once a player sinks a ship, the program should indicate that ship was sunk and specify which ship was sunk. Note: The program should not indicate the type of ship when it is first hit. It should only be specified once the ship is sunk. The first player that sinks all of the enemy ships will win the game.
Error Checking: The game should have some basic error checking functionality built-in. The following functionality should be included:
The game should check to make sure that multiple ships are not placed on the same coordinates.
When deploying a ship, the game should insure that the players place acceptable coordinates for each ship. For example, if a ship has a 1st coordinate of (2,1), it should not accept (3,4) as a second coordinate because they are not continuous.
The game should make sure that when a player fires on a ship, they don’t select a coordinate outside of the grid.
The game should make sure no ship is placed on a coordinate outside of the grid.
The game should not allow a player to fire at the same coordinate more than once during a game.
The code should be written in C and not C++
Explanation / Answer
#include<iostream>
#include<cstdlib>
#include<iomanip>
#include<ctime>
#include<string>
using namespace std;
struct player
{
char row;
int col;
int carrier_hits;
int battleship_hits;
int submarine_hits;
int destroyer_hits;
};
const int SIZE=10;//global constant to set the number of rows.
enum vessel {Carrier='C', Battleship='B', Submarine='S', Destroyer='D', MISS='O', HIT='X', _=' '};// enumerated type that contains the ships and other variables such as O and X that will be used for the actual playing of the game.
typedef vessel* vesselPtr;
void personal_info(); //function that will display author's info.
void instructions(int tries);//function that contains the instructions of the game.
void initial_board(vesselPtr *Board);//initializes the board
void print_board(vesselPtr *Board, bool reveal);//prints the board.
void assignShips(vesselPtr *Board);
bool check(vesselPtr *Board, player& hits);
int main()
{
char choice;//will store the choice the user picks.
int tries;// depending on which level tries is assigned a different value.
int count;
int length;
string hitPos;
string Colstr;
vesselPtr *Board = new vesselPtr[SIZE];
player hits;
bool sunk = false;
srand(time(0));//randomization of ships and directions will be performed.
for (int i = 0; i < SIZE; i++)
{
Board[i] = new vessel[SIZE];
}
personal_info();//displays the author's information.
//do while loop will keep looping as long as the difficulty level chosen in not one of the options given.
do
{
cout << "Enter difficulty level (Easy, Normal, Hard) of game (E, N, or H): " << endl;
cin >> choice;
if (choice == 'E' || choice == 'e')
{
tries=30;
}
else if ( choice =='N' || choice =='n')
{
tries=25;
}
else if (choice=='H' || choice =='h')
{
tries=20;
}
}while((choice !='E')&&(choice !='e')&&(choice !='N')&&(choice !='n')&&(choice !='H')&&(choice !='h'));
instructions(tries);//calls for instructions
initial_board(Board);//calls for the initialization of the board.
assignShips(Board);//once the second ship has been set this function calls for the actual positioning.
print_board(Board, false);//printing of the board.
hits.carrier_hits = 0;
hits.battleship_hits = 0;
hits.submarine_hits = 0;
hits.destroyer_hits = 0;
do
{
cout << "Enter position to fire torpedo #" << count++ << "(e.g., B7): ";
cin >> hitPos;
length = hitPos.length();
if ((length > 1) && (length < 4))
{
hits.row = toupper (hitPos[0]);
Colstr = hitPos.substr(1, hitPos.size() - 1);
hits.col = atoi(Colstr.c_str());
if ((hits.row < 'A')||(hits.row > 'J')||(hits.col < 1)||(hits.col > 10))
{
cout << "Invalid Position at " << hitPos << ". Try Again..." << endl;
// count++;
print_board(Board, false);
continue;
}
sunk = check(Board, hits);
print_board(Board, false);
}
}while ((count < tries) && (!sunk));
if (sunk)
{
cout << "Congratulations! You sunk both ships in " << count++ << " tries!" << endl;
}
else
{
cout << "Sorry, but you were not able to sink my ships! Tough break..." << endl;
print_board(Board, true);
}
for (int i = 0; i < SIZE; i++)
{
delete [] Board[i];
}
delete[] Board;
return 0;
}
/*
===================================================================================
Function: personal_info
Parameters: none
Return: void function returns no value
Description:Display information about author.
==================================================================================
*/
void personal_info()
{
cout << " W e l c o m e t o B a t t l e s h i p " << endl;
return;
}
/*
===================================================================================
Function: instructions
Parameters: int tries referring to the number of tries.
Return: void function returns no value
Description:Displays the instructions of the game along with how many tries the
user get according to which level they chose.
==================================================================================
*/
void instructions(int tries)
{
cout << "-----------------------------------------------------------" << endl;
cout << "This program will randomly choose two ships from your fleet" << endl;
cout << "made up of the following vessels: Carrier, Battleship, Sub-" << endl;
cout << "marine, and Destroyer. It will then randomly assign both of" << endl;
cout << "the vessels to the board that are oriented either vertical-" << endl;
cout << "ly or horizontally. As a player you will then have " << tries << " tries" << endl;
cout << "to sink both of the computer's vessels!"<< endl;
cout << "-----------------------------------------------------------" << endl;
cout << "Initializing board..." << endl;
cout << "Assigning vessels..." << endl;
return;
}
/*
===================================================================================
Function: initial_board
Parameters: using the enum created vessel we refer to the Board and its coordinates.
Return: void function returns no value
Description:This function will initialize the board before printing it.
==================================================================================
*/
void initial_board(vesselPtr *Board)
{
for( int i=0; i< SIZE;i++)//for loop will check every spot that's less than the amount of ROWS
{
for (int j=0; j < SIZE; j++)//for loop will check every spot that's less than the amount of COLS.
{
Board[i][j]= _;//The board inside will just be blank spaces.
}
}
}
/*
===================================================================================
Function: print_board
Parameters: using the enum created vessel we refer to the Board and its coordinates.
Return: void function returns no value.
Description:This function will print the board.
==================================================================================
*/
void print_board(vesselPtr *Board, bool reveal)
{
cout << " 1 2 3 4 5 6 7 8 9 10 " << endl;
cout << " +--------------------+" << endl;
for (int i=0; i< SIZE; i++)//for loop sets the boundaries to less than ROW.
{
cout <<static_cast<char>(i+65) << "|";//this will output the letters and the right hand side border
for (int j=0; j< SIZE; j++)//for loop sets the bottom boundaries to less than COLS.
{
if (((Board[i][j] == Destroyer)||(Board[i][j]== Submarine)||(Board[i][j] == Battleship)||(Board[i][j] == Carrier)) && (!reveal))
{
cout << static_cast<char>(_) << " ";
}
else
{
cout << static_cast<char>(Board[i][j]) << " ";
}
}
cout << "|" << endl;//the left hand side border
}
cout << " +--------------------+" << endl;//bottom border.
return;
}
void assignShips(vesselPtr *Board)
{
const int NUM_SHIPS = 2;
int i;
int j;
int vertical;
int shipRowPos;
int shipColPos;
int ship[NUM_SHIPS];
bool valid;
int counter = 0;
vessel shipType;
srand(time(0));
ship[counter] = (rand() % 4) + 2;
counter++;
do
{
ship[counter] = (rand()% 4) + 2;
}while (ship[counter - 1] == ship [counter]);
for (int index = 0; index < NUM_SHIPS; index++)
{
do{
valid = true;
vertical = rand() % 2;
shipRowPos = rand() % 10;
shipColPos = rand() % 10;
if (vertical)
{
if ((shipRowPos + ship[index]) <= SIZE)
{
for ( i = shipRowPos; i < (shipRowPos + ship[index]); i++ )
{
if (Board[i][shipColPos] != _)
{
valid = false;
}
}
}
else
{
valid = false;
}
if (valid)
{
switch(ship[index])
{
case 2:
shipType = Destroyer;
break;
case 3:
shipType = Submarine;
break;
case 4:
shipType = Battleship;
break;
case 5:
shipType = Carrier;
break;
}
for (i = shipRowPos; i < (shipRowPos + ship[index]); i++)
{
Board[i][shipColPos] = shipType;
}
}
}
else
{
if ((shipColPos + ship[index]) <= SIZE)
{
for (j = shipColPos; j < (shipColPos + ship[index]); j++)
{
if (Board[shipRowPos][j] != _)
{
valid = false;
}
}
}
else
{
valid = false;
}
if (valid)
{
switch(ship[index])
{
case 2:
shipType = Destroyer;
break;
case 3:
shipType = Submarine;
break;
case 4:
shipType = Battleship;
break;
case 5:
shipType = Carrier;
break;
}
for (j = shipColPos; j < (shipColPos + ship[index]);j++)
{
Board[shipRowPos][j] = shipType;
}
}
}
}while (!valid);
}
return;
}
bool check(vesselPtr *Board, player& hits)
{
bool sunk = true;
int row_number = hits.row - 'A';
switch(Board[row_number][hits.col - 1])
{
case _:
cout << "Miss..." << endl;
Board[row_number][hits.col - 1] = MISS;
break;
case HIT:
case MISS:
cout << "Wasted shot.. already fired torpedo at this location!" << endl;
break;
case Destroyer:
cout << "Hit... nice shot!" << endl;
Board[row_number][hits.col - 1] = HIT;
hits.destroyer_hits++;
if (hits.destroyer_hits == 2)
{
cout << "Congratulations! You sank my Destroyer!!" << endl;
}
break;
case Submarine:
cout << "Hit... nice shot!" << endl;
Board[row_number][hits.col - 1] = HIT;
hits.submarine_hits++;
if (hits.submarine_hits == 3)
{
cout << "Congratulations! You sank my Submarine!!" << endl;
}
break;
case Battleship:
cout << "Hit... nice shot!" << endl;
Board[row_number][hits.col - 1] = HIT;
hits.battleship_hits++;
if (hits.battleship_hits == 4)
{
cout << "Congratulations! You sank my Battleship!!" << endl;
}
break;
case Carrier:
cout << "Hit... nice shot!" << endl;
Board[row_number][hits.col - 1] = HIT;
hits.carrier_hits++;
if (hits.carrier_hits == 5)
{
cout << "Congratulations! You sank my Aircraft Carrier!!" << endl;
}
break;
default:
break;
}
for (int i = 0; i < SIZE; i++)
{
for (int j = 0; j < SIZE; j++)
{
if ((Board[i][j] == Destroyer)||(Board[i][j] == Submarine)||(Board[i][j] == Battleship) || (Board[i][j] == Carrier))
{
sunk = false;
}
}
}
return sunk;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.