Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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(); */ }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

what is equity multiple?

Answered: 1 week ago