Homework #9
Due Friday, April 27th at 5pm
Note: bold text in the examples is input from the user.
first object's name (newline) first object's description (newline) (blank line) second object's name (newline) ...The program should behave the same way as in the last problem of the previous homework, but the object names and descriptions should depend on the contents of the object file (prompt the user for the file name at the beginning of the simulation), so this should just be a matter of changing the hardcoded names and descriptions in your initializations of game objects to do the initializations using whatever is found in the object file instead.
In order to synchronize all the game objects with their descriptions, you will have to make assumptions as you read the object description file. Specifically, you should assume that the descriptions of interactive objects (the ones that affect the game if you do certain things to them) will always occur as the first five records of the file, in the following order:
Seeing your reflective mood, Ringo claps you on the back and brushes aside a wayward tear. ``Just for old times' sake,'' he says, ``why don't you write me an object editor. The program should not be a simulator, but rather a new program which just reads the simulator's object description files and allows you to edit them. Have the program prompt for the name of an object description file (as described in the previous problem), then read the file into an array of 100 augmented (see below) ObjectDescription structures, and allow the user (me!) to edit the array using the keywords `add,' `delete,' and `save.' ''
As soon as the file is loaded, and after every command (except `save'), the editor should print a numbered list of all the object descriptions in the array: first the name, then the description. If the user types `add N,' the editor should prompt for an object name and a description, in the format described above, and insert it at the Nth position in the array. If the user types `delete N,' the editor should remove the Nth object description from the database. The editor should save the map file and terminate (without printing the list) when the user types the command `save.'
The ObjectDescription structure, for this program, should have the same string fields as before, but should also have an additional boolean field valid. This field will be used to tell whether a given entry in the array had valid information (i.e. information that should be printed and saved) or not. Thus, as you read the file, you should mark the array entries you use to store the strings you find as containing valid data (by setting the valid field to TRUE) and marking all the entries you don't use invalid (by making their valid field FALSE). Then, you can delete entries by simply marking them invalid. You should always only print out the entries that are marked as valid when you print out the list of entries as shown below. You should not allow the user to overwrite an existing entry (i.e. a valid one) without first deleting it.
Note also that you should make sure the editor does not produce object description files with gaps in the middle (the example below contains a gap at entry 2, for example). You can close these gaps when the program saves the file by suppressing output for array positions which do not contain valid entries.
Example:
% a.out file to edit: myobjs Reading myobjs... File myobjs read. 1 Ringo a famous actor and drummer 2 octopus a cephalopod mollusk with dirty toenails command: add 3 name: crab description: a red thing which occupies physical space and is alive Entry added. 1 Ringo a famous actor and drummer 2 octopus a cephalopod mollusk with dirty toenails 3 crab a red thing which occupies physical space and is alive command: delete 2 Entry deleted. 1 Ringo a famous actor and drummer 3 crab a red thing which occupies physical space and is alive command: save Writing myobjs... File myobjs written.