CSE110 Spring 2001

Homework #9

Due Friday, April 27th at 5pm

Note: bold text in the examples is input from the user.


  1. [45 points] You have had it up to here with Ringo's nitpicking. First he doesn't think the crab behaves realistically enough, then he doesn't want to accidentally teleport any more, and now he keeps meowing about the descriptions of the objects: this one has to have a delicate musculature, and this one's a splendid so and so, etc, etc. Get him off your back once and for all by making the simulator read the name and description of each object out of a text file that Ringo can edit by himself, using the following format:
    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:

    1. a name and description for Ringo,
    2. a name and description for the octopus,
    3. a name and description for the crab,
    4. a name and description for an eel,
    5. a name and description for a drum.
    If the file contains more than five entries, the system should create an ObjectInfo structure instantiating each additional description, with arbitrarily chosen map coordinates.

  2. [40 points] You may be somewhat cross with him, but you and Ringo had some great times together, adventuring in the frigid airless depths and learning about programming. You think back on all the other crazy characters who have touched your life in one way or another: those recalcitrant pirates who learned to share their puppets, the tempremental crab with its insane hatred of industrial waste, the squirrels you nurtured through painful mitosis and infancy; and don't forget your non-fictional friends Jim and Nobo, and all the TA's who helped you achieve your dreams of taming the mysteries of the deep.

    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.