Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You are to program a simplified version of the Battleship guessing game. This as

ID: 3685037 • Letter: Y

Question

You are to program a simplified version of the Battleship guessing game. This assignment is for practice and is broken into two parts. The below game and header file description applies to both parts.

Game description: The field is a square 5x5 grid. One of the coordinates of the grid is a number (from 1 to 5) and the other -- a letter (from 'a' to 'e'). Your program should randomly place a fleet of five ships in the ocean and each ship takes up exactly one location in the field. Multiple ships cannot be placed in the same location, however the ships can be placed in adjacent locations. The user fires on the ships by specifying the coordinates of the shot. 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.

Data structures and function description:

The data structures and functions needed to implement the game are declared in this header shown below Figure 1.

The header file defines two structures:

· location stores the number and letter coordinates of a ship or a shot;

· ship stores the coordinates of the ship in location substructure and a boolean variable signifying whether the ship was sunk. The fleet of the deployed ships should stored in the array where each element is a structure variable of ship. This array is first initialized and then used in the game.

The functions are separated into three groups:

· Initialization functions that place the fleet of battleships in the ocean. The major function among the initialization functions is deploy() that accepts an array of ships by reference and initializes their locations. It uses the other two initialization functions: pick() and check() The pseudocode for deploy()is as follows:

declare a variable that stores the number of the deployed ships, initially zero this variable is to be used as an index in the array of ships loop until all ships are deployed

invoke pick() to get a new random location in the ocean

invoke check() to verify whether this location is occupied if this location is available then deploy the next ship at this location by storing the coordinates of this location in the ship's location, change the status of the ship to "not sunk" increment the number of the deployed ships

Hint: To randomly assign a character coordinate (from 'a' to 'e') for the location in pick(), randomly select a number from 1 to 5 and then use a switch to select the appropriate letter.

Hint 2: The logic of initialize(), check() and deploy() is very similar to the logic of lottery number selection functions in one of the previous labs.

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

The printout is done using the following functions.

printShip() prints the location and status of a single ship

printFleet() prints the location and the status (sunk or not) of the whole fleet of ships. This function uses printShip(). The output of this function might look like:

b5 sunk, c3 up, a2 up, e1 sunk, e4 up

· Battle functions that control the game play. After the fleet of ships is deployed, and its location is printed if desired, the game starts. The pseudocode of the game algorithm (to be implemented in main() or a dedicated function) is as follows.

While at least one ship is operational invoke fire() to get the location of the next shot from the user invoke check() to see if this location is occupied by a ship

if location is occupied then invoke sink() to sink the ship report a hit

else report a miss

First Test. Create a project titled Lab8_Test. Add the header file in Figure 1 battleship.h described above to your project. Add the file show in Figure 2 (testShips.cpp) to your project as well. Implement the functions prototyped in battleship.h file and invoked in testShips.cpp and place these function definitions inbattleship.cpp Note that presently portions of the file are commented out. This is to encourage incremental program development. You need to implement the functions that are not commented first. Then, uncomment the second portion of code and implement those functions. Ensure the assignment works correctly with all code of testShips.cpp uncommented. Then create a project titled Lab8_Game. Use battleship.h, battleship.cpp from the test above; add game.cpp that contains main() , invokes the game functions declared in battleship.h and implements the Battleship game as described in the introduction.

Figure 1:

battleship.h 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 every Ship in a Location where xcoordinate is -1

                                         // and y-coordinate is '*' (a star) to signify

                                         // that the ship is not deployed

location pick(); // 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 element of the array

                                                          // that matches the location

                                                          // returns -1 if none do

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

testShips.cpp file

#include "battleship.h"

#include <iostream>

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

int main() {

       // checking location functions

       // create location

       location mySpot;

       mySpot.x = 5;

       mySpot.y = 'e';

       // print location

       cout << "mySpot.x = " << mySpot.x << " mySpot.y = " << mySpot.y << endl;

       mySpot = pick(); // assign randomly generated location to mySpot

       cout << "mySpot.x = " << mySpot.x << " mySpot.y = " << mySpot.y << endl;

       mySpot = fire(); // assign user input location to mySpot

       cout << "mySpot.x = " << mySpot.x << " mySpot.y = " << mySpot.y << endl;

       // checking ship functions

       ship myShip;

       myShip.loc = pick(); // assign random location to myShip

       myShip.sunk = false;   // myShip is not sunk

       printShip(myShip); // print myShip information

       if (match(myShip, mySpot))

              cout << "myShip is at mySpot location ";

       else

              cout << "myShip is not at mySpot location ";

       sink(myShip); // sinking myShip

       printShip(myShip); // print sunk ship

                                     // checking fleet functions

       ship myFleet[FLEET_SIZE];

       initialize(myFleet); //assigning -1 to all ship's locations in myFleet

       printFleet(myFleet);

       deploy(myFleet); // deploying ships at random locations

       printFleet(myFleet);

       if (check(myFleet, mySpot) != -1)

              cout << "myFleet has a ship at mySpot ";

       else

              cout << "myFleet does not have a ship at mySpot ";

       if (operational(myFleet))

              cout << "at least one ship is not sunk ";

       else

              cout << "all ships are sunk ";

}

Explanation / Answer

Below are the function definitions:

location fire(){

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote