2. Write a program to play a game in which you try to sink a fleet of five navy
ID: 664634 • Letter: 2
Question
2. Write a program to play a game in which you try to sink a fleet of five navy vessels by guessing their locations on a grid. The program uses random numbers to position its ships on a 15 x 15 grid. The ships are of different lengths as follows: Frigate: 2 locations Tender: 2 locations Destroyer: 3 locations Cruiser: 3 locations Carrier: 4 locations The program must pick one square as the starting location, then pick the direction of the ship on the board, and mark off the number of squares in that direction to represent the size of the ship. It must not allow a ship to overlap with another ship or to run off the board The user enters coordinates in the range of 1 through 15 for the rows and A through O for the columns. The program checks this location, and reports whether it is a hit or a miss. If it is a hit, the program also checks whether the ship has been hit in every location that it occupies. If so, the ship is reported as sunk, and the program identifies which ship it is The user gets 60 shots to attempt to sink the fleet. If the user sinks all of the ships before using all 60 shots, then he or she wins the game. At the end of the game, the program should output the grid, so that the user can see where the ships are locateExplanation / Answer
//HEADER FILES
#include<iostream>
#include<conio.h>
using namespace std;
//GLOABAL DECLARATION OF GRIDMAP
char gridmap[15][15]; // 15X15 PLAYING GRID
int row_size, col_size; // ROWS AND COLUMNS OF THE GRID
// PUT A SHIP ON THE MAP
int PUTSHIP(char ship1, int spots1)
{
int dir1; // DIRECTION
int K1;
// KEEP TRYING TO FIND AN OPEN SPOT FOR THE SHIP
while (1) {
// GET A RANDOM ROW, COLUMN, DIRECTION
row_size = rand() %15;
col_size = rand() %15;
dir1 = rand() %4;
if (gridmap[row_size][col_size] != '.')
continue;
if (dir1 == 0) { // LEFT DIRECTION
// CHECK IF PUTTING THE SHIP TO THE LEFT WILL GO OUT OF BOUNDS
// IF IT DOESN'T WORK, CONTINUE SO WE CAN DO THE WHILE LOOP
// AGAIN AND CHOOSE A NEW POSITION RANDOMLY
if ((col_size - spots1 + 1) < 0)
continue;
// CHECK IF THE SPOTS ARE ALREADY TAKEN BY ANOTHER SHIP
for (k1 = 1; k1 < spots; k1++) {
if (gridmap[row_size][col_size - k1] != '.')
continue;
}
// mark the ship into the grid
for (k1 = 0; k1 < spots; k1++) {
gridmap[row_size][col_size - k1] = ship1;
}
}
else if (dir1 == 1)
{ // UP
if ((row_size - spots1 + 1) < 0) {
continue;
}
for (k1 = 1; k1 < spots1; k1++) {
if (gridmap[row_size - k1][col_size] != '.') {
continue;
}
}
for (k1 = 0; k1 < spots1; k1++) {
gridmap[row_size - k1][col_size] = ship1;
}
}
else if (dir1 == 2) { // RIGHT
if ((col_size + spots1) > 15) {
continue;
}
for (k1 = 1;k1 < spots1; k1++) {
if (gridmap[row_size][col_size + k1] != '.') {
continue;
}
}
for (k1 = 0; k1 < spots1; k1++) {
gridmap[row_size][col_size + k1] = ship1;
}
}
else if (dir1 == 3)
{ // DOWN
if ((row_size + spots1) > 15) {
continue;
}
for (k1 = 1; k1 < spots1; k1++) {
if (gridmap[row_size + k1][col_size] != '.') {
continue;
}
}
for (k1 = 0; k1 < spots1; k1++) {
gridmap[row_size +k1][col_size] = ship1;
}
}
break;
}
}
//PRINT THE GRID ON THE CONSOLE
void printGridMap()
{
// PRINT THE GRID
cout << " ABCDEFGHIJKLMNO"<<endl;
for (row_size = 0; row_size < 15; row_size++)
{
printf("%2d ", row_size+1);
for (col_size = 0; col_size < 15; col_size++)
{
cout << gridmap[row_size][col_size];
}
cout << endl;
}
}
//MAIN METHOD
int main()
{
int shots1 = 60;
int sunk1 = 0;
char map[15][15];
// INITIALIZE EVERYTHING TO ZERO
for (row_size = 0; row_size < 15; row_size++)
{
for (col_size = 0; col_size < 15; col_size++)
{
gridmap[row_size][col_size] = '.';
}
}
// ADD THE SHIPS INTO THE GRID
PUTSHIP('F', 2); // F for FRIGATE
PUTSHIP('T', 2); // T for TENDER
PUTSHIP('D', 3); // D for DESTROYER
PUTSHIP('C', 3); // C for CRUISER
PUTSHIP('A', 4); // A for CARRIER
// SAVE A COPY OF THE GRID FOR DISPLAY
for (row_size = 0; row_size < 15; row_size++) {
for (col_size = 0; col_size < 15; col_size++) {
map[row_size][col_size] = gridmap[row_size][col_size];
}
}
// HAVE USER FIRE A SHOT AND SEE IF IT HIT ANYTHING
while (shots1-- > 0)
{
int r1;
char char1;
int ro1, co1;
char type1;
printGridMap();
cout<<"ENTER POSITION (for example B3): ";
cin >> char1 >> r1;
ro1 = r1-1;
co1 = char1-'A';
type1 = gridmap[ro1][co1];
// CHECK IF IT HIT A SHIP SPOT
if ((type1 != '.') && (type1 != 'X')) {
int pos1;
int remaining1 = 0;
cout << "HIT! ";
gridmap[ro1][co1] = 'X';
// Check ROW AND COLUMN TO SEE IF ANY PART OF THE SHIP REMAINS
for (pos1 = 0; pos1 < 15; pos1++)
{
if ((gridmap[ro1][pos1] == type1) || (gridmap[pos1][co1] == type1))
{
remaining1 = 1;
break;
}
}
// CHECK IF A SHIP HAS BEEN COMPLETELY SUNK
if (remaining1 == 0)
{
cout << "YOU SUNK MY ";
if (type1 == 'F') {
cout << "FRIGATE! ";
}
else if (type1 == 'T') {
cout << "TENDER! ";
}
else if (type1 == 'D') {
cout << "DESTROYER! ";
}
else if (type1 == 'C') {
cout << "CRUISER! ";
}
else if (type1 == 'A')
{
cout << "CARRIER! ";
}
sunk1++;
if (sunk1 == 5)
{
break;
}
}
}
else
{
cout << "MISSED! ";
gridmap[ro1][co1] = 'X';
}
cout << shots1 << " SHOTS LEFT ";
}
// CHECK IF ALL SHIPS ARE SUNK
if (sunk1 == 5)
{
cout << "YOU SUNK ALL MY BATTLESHIPS! ";
}
else
{
cout << "YOU RAN OUT OF SHOTS ";
}
// RESTORE THE ORIGINAL GRID FOR DISPLAy
for (row_size = 0; row_size < 15; row_size++)
{
for (col_size = 0; col_size < 15; col_size++)
{
gridmap[row_size][col_size] = map1[row_size][col_size];
}
}
printGridMap(); //PRINT THE GRID
return 0;
}//MAIN END
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.