Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ In this programming assignment, you are expected to develop a simplified version of the Battleship guessing game. The assignment is broken into two parts,

C++

In this programming assignment, you are expected to develop a simplified version of the Battleship guessing game. The assignment is broken into two parts, test part, and full program. The below game and header file description apply to both parts. The idea is to encourage you to work on the development of the components (classes) that are expected to be used to build up the system (the battleship game) and test them before using them to build your game.

Battleship game description. The field (ocean or space) is a square 7x7 grid. Both of the coordinates of the grid (x and y) are numbers (from 1 to 7). Your program should randomly place a fleet of seven 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 (x & y). 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. However, the program tells the player that the ship is already sunk if that spot it occupies has been already chosen before.

Classes and methods description. The classes and methods needed to implement the game are mostly declared in the header file (attached). There are three main classes:

  • Location stores the x and y coordinates of a ship or a shot.
  • Ship stores the coordinates of the ship in a Location object and a Boolean variable signifying whether the ship was sunk.
  • Fleet contains the fleet of the deployed ships.

The methods and functions you are expected to implement are explained in the header file and project description.

  • Functions that display the location of the fleet. After the fleet is deployed, the user is prompted if she would like to see the location of the ships (Hint: use this option for debugging). If the user selects this option, the locations of the ship (and their status: sunk or not) are printed after every shot

The printout is done using the following methods (see header as well):

  • Extraction operator is overloaded for the Ship class. It prints the location and status of a single ship. It is already implemented for you (please use the same concept for future projects).
  • printFleet() prints the location and the status (sunk or not) of the whole fleet of ships. This function uses the overloaded

[2, 3]-> up , [5, 1]-> up , [5, 7]-> sunk , [4, 4]-> up , [3, 5]-> sunk , [4, 1]-> up , [3, 3]-> sunk

Battle functions that control the gameplay.

After the fleet of ships is deployed (ships are placed in random locations), and its location is printed if desired, the game starts. the pseudocode for your main() method 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 ( 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 the ship is already sunk, report that as well.

The code and data are declared (Figure 1: header file battleship.h (Links to an external site.), below). The information about locations, ships, and the entire fleet is encapsulated in classes.

  1. Create a test project that uses the given header file (battleship.h) shown below, to the project and test source file Figure 2: testingFile.cpp (Links to an external site.), shown below. Add them to your test project. Then, implement member methods for the classes of battleship.h and place the defined member functions in a code file (battleship.cpp)

You should implement the member functions for classes in an incremental way. Start with implementing member functions for class Location, then proceed to ship and then to the fleet. Uncomment the portions of testShips.cpp as you work on a particular class. Once your project works correctly with all code of testShips.cpp uncommented, submit your test project. So, now you are confident that your classes can be used to develop the game. That is, your header file and implementation file should be used in the second project.

  1. Create a project and use battleship.h and implementation.cpp from the test project 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

Summary of methods you need to implement for the declared classes:

Class Location:

Location::Location(): A default constructor that dynamically allocated an array with two elements coordinates[0] for x coordinate and coordinates[1] for y coordinate. Both are initialized with -1 Location::Location(int x, int y) : An overloaded constructor that dynamically allocated an array with two elements coordinates[0] for x coordinate and coordinates[1] for y coordinate. parameters are used to initialize the array elements.

void Location::pick(): generates and assigns random coordinates [1 to 7] to the array elements void Location::fire(): Prompts the user to enter valid coordinates and assign them to the location array elements. User entry is verified and a message is printed to the user if she enters invalid coordinates.

void Location::print() const: a constant method that print the location coordinates in the format [x,y]

//rule of three, since we have a member that uses dynamic memory allocation Location::Location(const Location& existing) : copy construtor Location& Location::operator=(const Location& existing) : assignment overloading Location::~Location() { delete[] coordinates; }

Class Ship:

Ship::Ship(): Default constructor that assigns false to the sunk bool variable

bool Ship::match(const Location& otherLocation) const: A constant method that returns true if the received location parameters have the same coordinates. It does invoke the friend function compare(). The method returns false if the Location objects are not equal.

void Ship::sink(): sinks the ship. void Ship::setLocation(const Location& currentLoc): updates the ship location with the received parameter.

Class Fleet:

int Fleet::check(const Location& loc) const: a constant method that returns the index of the ship that occupies the received location object. The method returns -1 if there is no ship in the fleet that has the same coordinates.

void Fleet::deployFleet() // deploys the ships in random locations such that no location is occupied by more than one ship. bool Fleet::operational() const: returns true as long as there is at least one none-sunk ship. It returns false if all the fleet is sunk.

int Fleet::isHitNSink(const Location& loc): checks the object that was fired by the user and sent to this method to see if it is occupied y any ship in the fleet.

This method:

returns 0 if none of the fleet ships occupies this location, which means it is a miss.

returns 1 if it is a hit, where a ship in the fleet is found to be occupying this location. This method sinks the ship and then returns 1.

returns 2 if the ship that occupies this location is already sunk.

Tips:

Think about a method that is part of the class Fleet that can be used here inside this method to search for a location used by the fleet that matches the received location coordinates.

void Fleet::printFleet() const: A constant methods that prints the fleet information in the format shown below:

[2, 3]-> up , [5, 1]-> up , [5, 7]-> sunk , [4, 4]-> up , [3, 5]-> sunk, [4, 1]-> up , [3, 3]-> sunk

You are expected to benefit from the class ship printability (thanks to operator

Ship* Fleet::getShipList() : returns the fleet array.

int Fleet::getFleetSize(): returns the fleet size. Technically, it returns the const member FLEET_SIZE that is declared inside the fleet.

battleship.h

image text in transcribed

image text in transcribed

testingFile.cpp

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

Incomplete implementation.cpp file:

image text in transcribed I was unable to add them because it would exceed the limit, could you please save the introduction in a separate document so I can come back and erase everything and add the .cpp and .h?

// class definitions for battleship assignment battleship.h // class definitions for battleship assignment #ifndef BATTLESHIP_H_ #define BATTLESHIP_H_ #include #include #include #include using namespace std; // coordinates (location of the ship and shots class Location { public: Location(); // void constructor, dynamically allocates the coordinates array and assigns -1 to both of the coordinates Location (int, int);// overloaded constructor to be used to create a location object with given coordinates. void pick(); // picks a random location. Means assigns 1 to 7 to the elements of the coordinates array void fire(); // asks the user to input coordinates of the next shot void print) const; // prints location in format "[x,y]" e.g., [2,4] int getCoordx() { return coordinates[0]; } int getCoordy { return coordinates[1]; } // predicate returns true if the two locations match friend bool compare(const Location&, const Location&); //rule of three Location (const Location& ); // copycostructor Location& operator=(const Location&); //assignment overloading Location();//destructor //overloading == bool operator==(Location rhs) const{ return (coordinates [0] == rhs.coordinates[0] && coordinates[1] } == rhs.coordinates[1]); private: static const int FIELD_SIZE = 7; // the field (ocean) is fieldSize X fieldSize int* coordinates; }; // 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(); 1/ sets "sunk" member variable of the ship to true void setLocation(const Location&); // deploys the ship at the specified 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 Location getLocation() { return loc; } ostream& operator" using std::cout; using std::cin; using std::endl; bool validLocations (Fleet); bool uniqueLocations (Fleet); 11/ this is main function int main() { // srand(time(nullptr)); // random seed // checking location objects created by default and overloaded constructors srand(1); Location mySpot (4, 5), userShot; userShot.print(); -1) if (userShot.getCoordX() == -1 && userShot.getCoordy cout 0 && mySpot == previous Loc) ++possibleconsecutiveDuplications; if ((mySpot.getCoordX() >= 1 && mySpot.getCoordX() = 1 && mySpot.getCoordY() = 1 && mySpot.getCoordX() = 1 && mySpot.getCoordY() Passed!" Failed!" 10) cout Passed! "; else cout Passed! "; else cout Passed! "; else cout Passed! "; else cout Passed! "; else cout Passed! "; else cout Failed! "; if (myShip.match(userShot)) cout Passed! "; else cout Passed! "; if (!myShip.issunk() { cout Passed! Sinking it. "; myShip.sink(); } if (myShip.isSunk() { cout Passed! "; } else cout Failed! "; //Testing the innsertion operator overloading. Manual inspection is needed. cout Passed!"; else cout Failed!"; myFleet.deployFleet(); // fleet is deployed at random locations if (validLocations (myFleet)) cout Passed!"; else cout Failed!"; if (uniqueLocations (myFleet)) cout Passed!"; else cout Failed!"; if (myFleet.operational() { cout Passed! "; if (operational) { cout Passed! "; } else { cout Failed! "; > > if (myFleet.isHitnSink(userShot)) cout = 1 && fleet Infolist[i].getLocation().getCoordx() = 1 && fleet Infolist[i].getLocation().getCoorde() #include #include using namespace std; //this is a friend function to Location class. bool compare(const Location& loci, const Location& loc2) { return (loc1 == loc2); } //overloding the " #include #include #include using namespace std; // coordinates (location of the ship and shots class Location { public: Location(); // void constructor, dynamically allocates the coordinates array and assigns -1 to both of the coordinates Location (int, int);// overloaded constructor to be used to create a location object with given coordinates. void pick(); // picks a random location. Means assigns 1 to 7 to the elements of the coordinates array void fire(); // asks the user to input coordinates of the next shot void print) const; // prints location in format "[x,y]" e.g., [2,4] int getCoordx() { return coordinates[0]; } int getCoordy { return coordinates[1]; } // predicate returns true if the two locations match friend bool compare(const Location&, const Location&); //rule of three Location (const Location& ); // copycostructor Location& operator=(const Location&); //assignment overloading Location();//destructor //overloading == bool operator==(Location rhs) const{ return (coordinates [0] == rhs.coordinates[0] && coordinates[1] } == rhs.coordinates[1]); private: static const int FIELD_SIZE = 7; // the field (ocean) is fieldSize X fieldSize int* coordinates; }; // 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(); 1/ sets "sunk" member variable of the ship to true void setLocation(const Location&); // deploys the ship at the specified 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 Location getLocation() { return loc; } ostream& operator" using std::cout; using std::cin; using std::endl; bool validLocations (Fleet); bool uniqueLocations (Fleet); 11/ this is main function int main() { // srand(time(nullptr)); // random seed // checking location objects created by default and overloaded constructors srand(1); Location mySpot (4, 5), userShot; userShot.print(); -1) if (userShot.getCoordX() == -1 && userShot.getCoordy cout 0 && mySpot == previous Loc) ++possibleconsecutiveDuplications; if ((mySpot.getCoordX() >= 1 && mySpot.getCoordX() = 1 && mySpot.getCoordY() = 1 && mySpot.getCoordX() = 1 && mySpot.getCoordY() Passed!" Failed!" 10) cout Passed! "; else cout Passed! "; else cout Passed! "; else cout Passed! "; else cout Passed! "; else cout Passed! "; else cout Failed! "; if (myShip.match(userShot)) cout Passed! "; else cout Passed! "; if (!myShip.issunk() { cout Passed! Sinking it. "; myShip.sink(); } if (myShip.isSunk() { cout Passed! "; } else cout Failed! "; //Testing the innsertion operator overloading. Manual inspection is needed. cout Passed!"; else cout Failed!"; myFleet.deployFleet(); // fleet is deployed at random locations if (validLocations (myFleet)) cout Passed!"; else cout Failed!"; if (uniqueLocations (myFleet)) cout Passed!"; else cout Failed!"; if (myFleet.operational() { cout Passed! "; if (operational) { cout Passed! "; } else { cout Failed! "; > > if (myFleet.isHitnSink(userShot)) cout = 1 && fleet Infolist[i].getLocation().getCoordx() = 1 && fleet Infolist[i].getLocation().getCoorde() #include #include using namespace std; //this is a friend function to Location class. bool compare(const Location& loci, const Location& loc2) { return (loc1 == loc2); } //overloding the "

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

Database Principles Programming And Performance

Authors: Patrick O'Neil

1st Edition

1558603921, 978-1558603929

More Books

Students also viewed these Databases questions

Question

3. What strategies might you use?

Answered: 1 week ago

Question

What is DDL?

Answered: 1 week ago