Question: Create a project titled Lab7_Test. Add the header file battleship.h described above to your project. Add this file testShips.cpp to your project as well. Implement
Create a project titled Lab7_Test. Add the header file battleship.h described above to your project. Add this file 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 in battleship.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. Once your project works correctly with all code of testShips.cpp uncommented, submit your project.
// structure definitions and function prototypes // for the battleship assignment // Mikhail Nesterenko // 10/24/2017 #include#include #ifndef BATTLESHIP_H_ #define BATTLESHIP_H_ // // data structures definitions // const int fleetSize=6; // number of battleships const int fieldSize=5; // the field (ocean) is fieldSize * fieldSize // coordinates (Location) of the ship and shots struct Location{ int x; // 1 through fieldSize char y; // 'a' through fieldSize }; // 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 x-coordinate is -1 // and y-coordinate is '*' (a star) to signify // that the Ship is not deployed Location pick(); // generates a random Location bool match(const 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(const 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_ */
// Mikhail Nesterenko // tests battleShip functions implementation // 10/13/2013 #include "battleship.h" #includeusing std::cout; using std::cin; using std::endl; int main(){ // srand(time(nullptr)); // random seed srand(1); // fixed seed // // 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[fleetSize]; 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 "; */ }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
