Question
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.
- 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.
- 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
testingFile.cpp
Incomplete implementation.cpp file:
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
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started