Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

c++ battleship game program. need Assigment 2, game. cpp. the battleship. h. is

ID: 3687434 • Letter: C

Question

c++ battleship game program. need Assigment 2, game. cpp.

the battleship. h. is :

#ifndef BATTLESHIP_H_ #define BATTLESHIP_H_ const int FLEET_SIZE=5; const int FIELD_SIZE=5; struct location{ int x; char y; }; struct ship{ location loc; bool sunk; }; void initialize(ship[]); location pick(); bool match(ship, location); int check(const ship[], location); void deploy(ship[]); void printShip(ship); void printFleet(const ship[]); bool operational(const ship[]); location fire(); void sink(ship&); #endif

the battleship.cpp is:

#include <iostream>
#include <ctime>
#include <cstdlib>
#include "battleship.h"

using std::cout; using std::cin; using std::endl;

bool match(ship myShip, location mySpot){
// returns true if this location matches
// the location of the ship
// returns false otherwise
if (myShip.loc.x == mySpot.x && myShip.loc.y == mySpot.y)
return true;
else
return false;
}

int check(const ship myFleet[], location mySpot){
// returns the index of element of the array
// that matches the location
// returns -1 if none do

for (int i = 0; i < FLEET_SIZE; ++i)
if (match(myFleet[i], mySpot)) {
return i;
}
return -1;
}

void initialize(ship myFleet[]) {
// places all ships in -1 X location to signify
// that the ship is not deployed

for (int i=0; i < FLEET_SIZE; ++i) {
myFleet[i].loc.x = -1;
myFleet[i].loc.y = 'X';
}
}

location pick() {
// generates a random location

location mySpot;
srand(int(time(0)));
mySpot.x = rand() % 5 + 1;
int y = rand() % 5 + 1;
switch like emoticon {
case 1:
mySpot.y = 'a';
break;
case 2:
mySpot.y = 'b';
break;
case 3:
mySpot.y = 'c';
break;
case 4:
mySpot.y = 'd';
break;
case 5:
mySpot.y = 'e';
break;
}
return mySpot;
}

void deploy(ship myFleet[]) {


int i = 0;
while (i < FLEET_SIZE) {
location mySpot = pick();
int tmp = check(myFleet, mySpot);
if (tmp == -1) {
myFleet[i].loc = mySpot;
myFleet[i].sunk = false;
++i;
}
}
}



void printShip(ship myShip) {

cout << "The ship is at: " << myShip.loc.x << myShip.loc.y << endl;
cout << "Status of ship: ";
if (myShip.sunk == false)
cout << "The ship is still alive!" << endl;
else
cout << "The ship was sunk!" << endl;
}

void printFleet(ship const myFleet[]) {


for (int i = 0; i < FLEET_SIZE; ++i) {
printShip(myFleet[i]);
}
}



bool operational(ship const myFleet[]) {

for (int i = 0; i < FLEET_SIZE; ++i) {
if (myFleet[i].loc.x != -1 && myFleet[i].loc.y != -1)
return true;
}
return false;
}
location fire() {


location mySpot;
cout << "Where will you take your next shot? (Coordinates 1-5 a-e separated by a space): ";
cin >> mySpot.x >> mySpot.y;
return mySpot;
}

void sink(ship& myShip) {


myShip.sunk = true;
}

Structures Introduction You are to program a simplified version of the Battleship guessing game. The assignment is broken into two parts. The below game and header file description applies to both parts Battleship game description. The field (ocean) is a square 5x5 grid. One of the coordinates of the grid is a number (from 1 to 5) and the other -- a letter (from 'a' to 'e'). Your program should randomly place a fleet of five ships in the ocean. Each ship takes up exactly one location in the ocean. Multiple ships cannot be placed in the same location. The ships, however, can be placed in adjacent locations. The user fires on the ships by specifying the coordinates of the shot. The program reports whether each shot was a hit or a miss. If the shot was a hit, the ship is sunk. The game continues until all ships are sunk. The program does not keep track of the locations of the previously fired shots. Data structures and function description. The data structures and functions needed to implement the game are declared in this header shown below Figure 1. The header file defines two structures: location stores the number and letter coordinates of a ship or a shot; ship stores the coordinates of the ship in location substructure and a boolean variable signifying whether the ship was sunk. The fleet of the deployed ships should stored in the array where each element is a structure variable of ship. This array is first initialized and then used in the game. . · The functions are separated into three groups: Initialization functions that place the fleet of battleships in the ocean. The major function among the initialization functions is deploy () that accepts an array of ships by reference and initializes their locations. It uses the other two initialization functions: pick() and check () The pseudocode for deploy () is as follows: . declare a variable that stores the number of the deployed ships, initially zero this variable is to be used as an index ln the array of ships loop until all ships are deployed invoke pick) to get a new random location in the ocean invoke check) to verify whether this location is occupied

Explanation / Answer

Given below is the completed code for the question. Modified the batttleship.cpp code to meet the specifications in the question. Specifically these functions - operational(), printShip() and initialize() were modified. The game.cpp is implemented and output shown below. For the purpose of debugging, the printFleet() is done in each iteration. That line can be commented out. Hope the answer helped. If it did, please don't forget to rate the answer. Thank you very much.

battleship.cpp

#include <iostream>
#include <ctime>
#include <cstdlib>
#include "battleship.h"

using std::cout; using std::cin; using std::endl;

bool match(ship myShip, location mySpot){
// returns true if this location matches
// the location of the ship
// returns false otherwise
if (myShip.loc.x == mySpot.x && myShip.loc.y == mySpot.y)
return true;
else
return false;
}

int check(const ship myFleet[], location mySpot){
// returns the index of element of the array
// that matches the location
// returns -1 if none do
  
for (int i = 0; i < FLEET_SIZE; ++i)
if (match(myFleet[i], mySpot)) {
return i;
}
return -1;
}

void initialize(ship myFleet[]) {
// places all ships in -1 * location to signify
// that the ship is not deployed
  
for (int i=0; i < FLEET_SIZE; ++i) {
myFleet[i].loc.x = -1;
myFleet[i].loc.y = '*';
}
}

location pick() {
// generates a random location
  
location mySpot;
  
mySpot.x = rand() % FIELD_SIZE + 1;
int y = rand() % FIELD_SIZE + 1;
switch(y)
{
case 1:
mySpot.y = 'a';
break;
case 2:
mySpot.y = 'b';
break;
case 3:
mySpot.y = 'c';
break;
case 4:
mySpot.y = 'd';
break;
case 5:
mySpot.y = 'e';
break;
}
return mySpot;
}

void deploy(ship myFleet[]) {
  
  
int i = 0;
while (i < FLEET_SIZE) {
location mySpot = pick();
int tmp = check(myFleet, mySpot);
if (tmp == -1) {
myFleet[i].loc = mySpot;
myFleet[i].sunk = false;
++i;
}
}
}

void printShip(ship myShip) {
  
cout << myShip.loc.y << myShip.loc.x << " ";
if(myShip.sunk)
cout << "sunk" ;
else
cout << "up";
}

void printFleet(ship const myFleet[]) {
  
printShip(myFleet[0]);
for (int i = 1; i < FLEET_SIZE; ++i) {
cout << ", ";
printShip(myFleet[i]);
}
  
cout << endl;
}

bool operational(ship const myFleet[]) {
  
for (int i = 0; i < FLEET_SIZE; ++i) {
if (!myFleet[i].sunk)
return true;
}
return false;
}
location fire() {
  
location mySpot;
cout << "Where will you take your next shot? (Coordinates a-e 1-5 e.g. a3 or d5): ";
cin >> mySpot.y >> mySpot.x;
return mySpot;
}

void sink(ship& myShip) {
myShip.sunk = true;
}

game.cpp

#include <ctime>
#include <cstdlib>
#include <iostream>
#include "battleship.h"
using namespace std;
int main()
{
ship myFleet[FLEET_SIZE];
string ans;
  
srand(int(time(0)));
  
initialize(myFleet);
deploy(myFleet);
  
cout << "Do you want to print the Fleet? y/n: ";
cin >> ans;
if(ans == "y" || ans == "Y")
printFleet(myFleet);
  
while(operational(myFleet))
{
location myLocation = fire();
int index = check(myFleet, myLocation);
if(index != -1)
{
sink(myFleet[index]);
cout << "It's a HIT !" << endl;
}
else
{
cout << "It's a MISS!" << endl;
}
printFleet(myFleet); //for debugging only, comment it out
}
  
cout << "Game over !" << endl;
}

output

Do you want to print the Fleet? y/n: n
Where will you take your next shot? (Coordinates a-e 1-5 separated by a space e.g. a3 or d5): b4
It's a MISS!
d1 up, e1 up, d5 up, a2 up, c5 up
Where will you take your next shot? (Coordinates a-e 1-5 separated by a space e.g. a3 or d5): a5
It's a MISS!
d1 up, e1 up, d5 up, a2 up, c5 up
Where will you take your next shot? (Coordinates a-e 1-5 separated by a space e.g. a3 or d5): e1
It's a HIT !
d1 up, e1 sunk, d5 up, a2 up, c5 up
Where will you take your next shot? (Coordinates a-e 1-5 separated by a space e.g. a3 or d5): c3
It's a MISS!
d1 up, e1 sunk, d5 up, a2 up, c5 up
Where will you take your next shot? (Coordinates a-e 1-5 separated by a space e.g. a3 or d5): a2
It's a HIT !
d1 up, e1 sunk, d5 up, a2 sunk, c5 up
Where will you take your next shot? (Coordinates a-e 1-5 separated by a space e.g. a3 or d5): d1
It's a HIT !
d1 sunk, e1 sunk, d5 up, a2 sunk, c5 up
Where will you take your next shot? (Coordinates a-e 1-5 separated by a space e.g. a3 or d5): c3
It's a MISS!
d1 sunk, e1 sunk, d5 up, a2 sunk, c5 up
Where will you take your next shot? (Coordinates a-e 1-5 separated by a space e.g. a3 or d5): c5
It's a HIT !
d1 sunk, e1 sunk, d5 up, a2 sunk, c5 sunk
Where will you take your next shot? (Coordinates a-e 1-5 separated by a space e.g. a3 or d5): d5
It's a HIT !
d1 sunk, e1 sunk, d5 sunk, a2 sunk, c5 sunk
Game over !

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote