Structures
Study automatic code completion feature with
Eclipse/CDT described here. As
you work on the below project, demonstrate to the lab instructor
automatic code completion with either looping constructs or
structures.
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:
- 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
is 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 match()
The implementation outline for deploy()is as
follows:
deploy() maintains an index of the ship
array for the next ship to be deployed.
deploy() loops until all ships are deployed,
In the body of the loop
- deploy() calls pick(). This function returns a random location
in the ocean.
- deploy() iterates
over already deployed ships and calls match() on each
to see if the new location is already occupied.
- If the location is occupied,
deploy() proceeds to the next iteration
otherwise (location is available)
deploy() puts the location
into the coordinates of the ship to be deployed and marks it as not sunk (deploys
a new ship at this location).
Then, it increments the index in the ship array for the next ship to be deployed.
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.
- 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 outline of the game algorithm (to be
implemented in main() or a dedicated function) is as
follows.
The game continues while function operational() returns
true. This function iterates over the array of the
deployed battleships to check if there is at least one that is not
sunk (member variable sunk is false). If
there is an operational battleship, fire() is invoked
that asks the user for number and letter of the location of the
shot. After obtaining this location, iterate over the array of the
battleships to check if one of them is deployed at the location
of the shot by invoking match(). If yes, report a hit
and sink the ship by invoking sink(). If no, 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. Submit
your projects to the subversion repository. Do not forget to verify
your submission on the web.