Battleship. Create a project titled Lab8_Battleship. You are to program a simpli
ID: 3530265 • Letter: B
Question
Battleship. Create a project titled Lab8_Battleship. You are to program a simplified version of the Battleship guessing game. The field (ocean) 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. 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. 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. You should use this header file. The file defines two structures: // structure definitions and function prototypes // for the battleship assignment #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 all ships in -1 X location to signify // that the ship is not deployed location pick(void); // 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 the element // of the ship[] array that matches // location. Returns -1 if none match // uses match() void deploy(ship[]); // places an array of battleships in // random locations in the ocean location that is used to store 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 header file prototypes the functions to be used in the implementation. 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). The printout is done using two 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 You are free to modify the existing functions or introduce new ones. For example, you may elect to pass battleship array size as a parameter to functions as opposed to using a global constant. Make sure your programs adhere to proper programming style.Explanation / Answer
can u post question using advanced editor im not understanding ur question properly
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.