Question: Given the below code which produces a working although one sided version of battleship, how would you reconstruct this code(primarily that of the functions) so

Given the below code which produces a working although one sided version of battleship, how would you reconstruct this code(primarily that of the functions) so that it instead of structures being used to make it function, input classes for the location and ship data and make it work with the functions.

header file:

#ifndef BATTLESHIP_H_ #define BATTLESHIP_H_ // // data structures definitions // const int FLEET_SIZE=5; // number of battleships const int FIELD_SIZE=5; // the field (ocean) is FIELD_SIZExFIELD_SIZE // coordinates (location) of the ship and shots struct location{ int x; // 1 through FIELD_SIZE char y; // 'a' through FIELD_SIZE }; // contains ship's coordinates (location) and whether is was sunk struct ship{ location loc; bool sunk; }; // // initialization functions // void initialize(ship[]); // places all ships in -1 X location to signify // that the ship is not deployed location pick(void); // generates a random location bool match(ship, location); // returns true if this location matches // the location of the ship // returns false otherwise int check(const ship[], location); // returns the index of the element // of the ship[] array that matches // location. Returns -1 if none match // uses match() void deploy(ship[]); // places an array of battleships in // random locations in the ocean // // display functions // void printShip(ship); // prints the location and status (sunk or not) // of a single ship void printFleet(const ship[]); // prints the locations of all the ships and // whether they are sunk // // battle functions // bool operational(const ship[]); // returns true if at least one ship in the array // is not sunk location fire(void); // asks the user to input the coordinates of the next // shot // note that check() is also used in the battle void sink(ship&); // sets "sunk" member variable of the ship to true #endif /* BATTLESHIP_H_ */ 

The main () portion:

#include  #include "BATTLESHIP.h" #include  #include  using namespace std; int main(){ srand(time(NULL)); ship s[FLEET_SIZE]; // enemy ship structure with 5 elements initialize(s); // run initialize function with structure s deploy(s); // run deploy function with structure s char shipLoc; // user input for ship status display ship user; // users fire guess cout << "WELCOME TO BATTLESHIP " << "_____________________ " << " -Sink enemy ships- "; cout << "You have the option to view the status of the ships or go straight to the game. "; while(operational(s)){ // run this while there is at least one standing ship cout << "Would you like to see the location and status of the ships?  "; cin >> shipLoc; if (shipLoc == 'y' || shipLoc == 'Y'){ printFleet(s); // prints status and location of enemy ships user.loc=fire(); // users guess stored from fire function int c = check(s, user.loc); // holds the index of the array or -1 if (c!=-1){ sink(s[c]); // changes status of sink in individual element cout << "HIT! Ship sunk. "; } else // if c = -1 then report a miss cout << "Sorry you missed, try again. "; } else if (shipLoc == 'n' || shipLoc == 'N'){ // same as above, without printing ships user.loc=fire(); int c = check(s, user.loc); if (c!=-1){ sink(s[c]); cout << "HIT! Ship sunk. "; } else cout << "Sorry you missed, try again. "; } else cout << " You entered an invalid option. Please enter  " << endl; } cout << " Congratulations! You have sunk all enemy ships. "; } void initialize(ship s[]){ // sets all ships in -1 to show it is not deployed for(int i=0; i: "; cin >> temp.y; cout << "X-Coordinate <1-5>: "; cin >> temp.x; cout << endl; return temp; // returns temps locations } void sink(ship& s){ s.sunk=true; // sinks the ship } 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!