Question: C++ Battleship program using multiple files and classes: I have to create a battleship program using classes. We are given the header and a test

C++ Battleship program using multiple files and classes:

I have to create a battleship program using classes. We are given the header and a test source. We have to create battleship.cpp to define the code then a game.cpp to run the game.

Instructions- Add header file battleship.h. to the project. Add test source file testShips.cpp to the project. Define member functions for the classes of battleship.h and place the defined member functions in battleship.cpp.

You should implement the member functions for classes incrementally. Start with implementing member functions for class location, then proceed to ship and then to fleet. Uncomment the portions of testShips.cpp as you work on a particular class. Use battleship.h, battleship.cpp from the test assignment above; add game.cpp that contains main() , invokes the game functions declared in battleship.h and implements the Battleship game

Hint: the pseudocode for your main() should be as follows:

 declare an object of class fleet call deployFleet() method on it prompt the user if ships' positions and status need to be printed loop while method operational() returns true - i.e. at least one ship is not sunk. Inside the loop, declare an object of class location, invoke fire() on it to get the location of the user's shot, pass this object to isHitNSink() method of your fleet object analyze the return value of this method and report a hit or a miss if requested printing ships' status invoke printFleet()

Header- (battleship.h)

#include  #include  #ifndef BATTLESHIP_H_ #define BATTLESHIP_H_ // coordinates (location) of the ship and shots class Location{ public: Location(); // void constructor, assigns -1 to X coord, and * to Y coord void pick(); // picks a random location void fire(); // asks the user to input coordinates of the next shot void print() const; // prints location in format "a1" // predicate returns true if the two locations match friend bool compare(const Location&, const Location&); private: static const int fieldSize=6; // the field (ocean) is fieldSize X fieldSize int x; // 1 through fieldSize char y; // 'a' through fieldSize }; // contains ship's coordinates (location) and whether is was sunk class Ship{ public: Ship(); // void constructor, sets sunk=false bool match(const Location&) const; // returns true if this location matches // the ship's location bool isSunk() const {return sunk;} // checks to see if the ship is sunk void sink(); // sets "sunk" member variable of the ship to true void setLocation(const Location&); // deploys the ship at the specified location void printShip() const; // prints location and status of the ship private: Location loc; bool sunk; }; // contains the fleet of the deployed ships class Fleet{ public: void deployFleet(); // deploys the ships in random locations // of the ocean bool operational() const; // predicate returns true if at least // one ship in the fleet is not sunk bool isHitNSink(const Location &); // returns true if there was a deployed // ship at this location (hit) and sinks it // otherwise returns false (miss) void printFleet() const; // prints out locations of ships in fleet private: static const int fleetSize=6; // number of battleships int check(const Location &) const; // returns index of the ship // that matches location // -1 if none match Ship ships[fleetSize]; // battleships of the fleet }; #endif /* BATTLESHIP_H_ */

Test Source- (testShips.cpp)

#include "battleship.h" #include  using std::cout; using std::cin; using std::endl; /// this is main function int main(){ // srand(time(nullptr)); // random seed srand(1); // fixed seed // // checking location object // Location mySpot, userShot; mySpot.pick(); // selecting a new random location cout << "Randomly selected location is: "; mySpot.print(); cout << "Input location: "; userShot.fire(); // having user input a location if(compare(mySpot,userShot)) cout << "Random location matches user input. "; else cout << "Random location does not match user input. "; // // checking ship object // /* // uncomment this part once you are done debugging above code Ship myShip; myShip.setLocation(mySpot); // placing ship at mySpot location if(myShip.match(userShot)) cout << "myShip\'s location matches user input. "; else cout << "myShip's location does not match user input. "; if(!myShip.isSunk()){ cout << "myship is not sunk yet, sinking it. "; myShip.sink(); } cout << "myShip\'s status is: "; myShip.printShip(); */ // // checking fleet object // /* // uncomment this part once you are done debugging above code Fleet myFleet; myFleet.deployFleet(); // fleet is deployed at random locations if(myFleet.operational()) cout << "Some ships of myFleet are still up. "; if(myFleet.isHitNSink(userShot)) cout << "there was a ship at userShot location, now it is sunk. "; else cout << "there was no ship at userShot location. "; cout << "myFleet\'s status is: "; myFleet.printFleet(); */ }

Also here is finished code for battleship.cpp that just needs made in to classes for it:

battleship.cpp

#include"battleship.h";

#include ;

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

Location pick() {

Location loc;

loc.x = (rand() % 6) + 1;

switch ((rand() % 6) + 1) {

case 1:loc.y = 'a'; break;

case 2:loc.y = 'b'; break;

case 3:loc.y = 'c'; break;

case 4:loc.y = 'd'; break;

case 5:loc.y = 'e'; break;

case 6:loc.y = 'f'; break;

}

return loc;

}

Location fire() {

Location fireLoc;

cout << "Input x: ";

cin >> fireLoc.x;

cout << "Input y: ";

cin >> fireLoc.y;

return fireLoc;

}

void printShip(const Ship s) {

cout << "Ship is at " << s.loc.x << ",";

cout << s.loc.y << endl;

if (s.sunk) {

cout << "Ship is destroyed" << endl;

}

else {

cout << "Ship is still up" << endl;

}

}

bool match(const Ship myShip, Location myLoc) {

return (myShip.loc.x == myLoc.x && myShip.loc.y == myLoc.y) ? true : false;

}

void sink(Ship &myShip) {

myShip.sunk = true;

}

void initialize(Ship ships[]) {

for (int i = 0; i < fleetSize; i++) {

ships[i].loc.x = -1;

ships[i].loc.y = '*';

ships[i].sunk = false;

}

}

void printFleet(const Ship ships[]) {

for (int i = 0; i < fleetSize; i++) {

printShip(ships[i]);

}

}

void deploy(Ship ships[]) {

int shipIndex = 0;

Location randomLoc;

while (shipIndex < fleetSize) {

Location randomLoc = pick();

if (check(ships, randomLoc) == -1) {

ships[shipIndex].loc.x = randomLoc.x;

ships[shipIndex].loc.y = randomLoc.y;

ships[shipIndex].sunk = false;

shipIndex++;

}

}

}

int check(const Ship ships[], Location loc) {

for (int i = 0; i < fleetSize; i++) {

if (match(ships[i], loc)) {

return i;

}

}

return -1;

}

bool operational(const Ship ships[]) {

for (int i = 0; i < fleetSize; i++) {

if (!ships[i].sunk) {

return true;

}

}

return false;

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!