Lab 8
Objectives:
Use structures and arrays
There is only one program this time. Use multiple files, including a header file, and create a makefile. Name the main function code as specified.This is similar to, but different from, the problem assigned in Dr. Nesterenko's class. You may read the material on his Battleship Game at http://deneb.cs.kent.edu/~mikhail/classes/ioop/Labs/Lab8/ to help you, but do not discuss with anyone in the other class their solution. You may modify the header file he provides, but note you must make changes for this problem and you should cite him if you use the basic outline of the file.
Robot World: Create a driver program titled Lab8_Robot. You are to program a simplified version of a guessing game. The field is a square 5x5 grid. Each coordinate of the 2-dimensional grid is a number (from 1 to 5).Your program should randomly place five robots on the field. Each robot takes up exactly one location in the field. Two robots cannot be placed in the same location at any time.
The goal of the game is to fire on the robots by specifying the coordinates of the shot, terminate all the robots, and to do this with the smallest number of shots. An added wrinkle is the fact that a robot can be designated as protected.
The user fires on a robot by specifying the coordinates of the shot. A counter is kept of the number of shots taken which is printed at the end of the game as the user's score. Lower is better obviously. The program reports whether each shot was a hit or a miss. If the shot misses, nothing is done to the robot. If the shot was a hit and the robot is not protected, the robot is terminated and is assumed to no longer occupy the square given by the location. (The robots are neat and have robots that move terminated robots off the field so their locations are set to a default). If the shot was a hit and the robot is protected, the robot loses its protected status, becomes unprotected and automatically jumps to a new square. The choice of the square is again done randomly.The handling after the jump depends on whether or not the new square is occupied.
If the square is not occupied (which happens even if it had been occupied and the robot there was terminated) the robot that jumped occupies that square wth no protection and, consequently, its location changes to the new square's coordinates.
If the square is occupied and its robot has not been terminated, the jumping robot is assumed to have jumped to the square, but both robots are terminated (friendly fire casuality) independent of the protected status of the original occupant of the square.
Note: If the square had been occupied, but its robot has been terminated, we can assume the square is available for the jumping robot.
After each firing, the game specifies how many robots are still active. The game continues until all robots are terminated. The program does not keep track of the locations of the previously fired shots.
The header file at the site given above provides some powerful hints as to how to proceed, but it doesn't work by just making a copy of it However, except for the additional twists, the basic idea is the same.
Basic Idea
The header file defines two structures:
location that
is used to store the number and letter coordinates of a robot or a shot;
robot stores
the coordinates of the robot in the location substructure, a boolean variable specifying whether or not the robot
is protected, and a boolean variable signifying whether the robot was terminated or not.
The army of the deployed robots is stored in an array where each element is a structure variable of robot. 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 two
groups:
Initialization functions that place the army of robots on the field.
The major function among the initialization functions is deploy() that accepts an array of robots 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 robot array for the next robot to be deployed. deploy() loops until all robots
are deployed. In the body of the loop deploy() calls pick(). This function returns a random location in the field.
deploy() iterates over already deployed robots 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 (i.e. location is available)
deploy() puts the location into the coordinates of the robot to be deployed and marks it as not terminated i.e.
deploys a new robot at this location). All robots are initially protected. Then, the index in the robot array is
incremented so the next robot can be deployed.
Battle functions that control the game play. After the army of robots is deployed, 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
robots to check if there is at least one that is not terminated (member variable terminated is false). If there
is an operational robot, fire() is invoked that asks the user for the number and letter of the location of the
shot. After obtaining this location, iterate over the array of the robots to check if one of them is deployed at
the location of the shot by invoking match(). If yes and protected is true, change the protected field to false
using unprotect(). If yes and protected is false, report a hit and terminate the robot by invoking terminate().
If no, report a miss.
Hint for debugging: Create a boolean variable debug and include in your driver program something like
if (DEBUG) ....//output the information about each robot.
Normally in the game you don't want to allow the user to see this. But,
for debugging purposes you should know information about the various robots. When not debugging, compile with debug
set to false. Otherwise, set it to true. You can include these at various spots and you can leave this debug code
in your code. Note this is alternative to using the debugger to examine the army.
You are free to modify the existing functions or introduce new ones. For example, you may elect to pass the robot
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.