CSE110 Spring 2001

Homework #5

Due Friday, March 9th at 5pm

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


  1. [25 points] Famous actor/drummer Ringo Starr wrote a song called "Octopus' Garden" about how great it would be to live under the sea, despite the fact that it's all salty and dangerous down there. Write a program to simulate how awful undersea life would actually be, in order to convince Ringo not to pursue this goal. Your program should provide an interface through which Ringo can swim around an aquatic wasteland and fend off the inevitable attacks of giant squid, coelacanths, tube worms, sand dollars, and other horrific denizens of the deep, through repeated prompts for single-character commands. The commands are:
    n   -   swim north
    s   -   swim south
    e   -   swim east
    w   -   swim west
    y   -   yell
    h   -   hide
    g   -   grab
    t   -   throw
    q   -   quit
    You should use a switch case statement to implement this interface, but you are not to use any functions yet.

    The simulation should begin in an octopus' garden. The octopus lets Ringo in, knows where he's been, etc, but then begins mauling him with over-ripe sea cucumbers and carrotfish. Ringo can only escape the garden-fresh barrage by (g)rabbing the octopus, (t)hrowing it on the ground, and then swimming away (n, s, e, or w), in that order. Failing to execute the steps in the proper order, one immediately after the other, should result in a face full of vegetables. Note that, although the directional swimming commands are necessary to escape the octopus, you don't need to worry about keeping track of Ringo's location other than whether or not he is in the garden.

    Example:

    % a.out
    You're in an octopus' garden, just like in your song, except
    the octopus is throwing things at you and frightening you.
    Your action: e
    You try to swim east, but the octopus gets in your way.
    The octopus keeps harassing you.
    Your action: t
    You try to throw something, but you have nothing in your hands.
    The octopus keeps harassing you.
    Your action: g
    You grab the octopus with your mighty European arms.
    Your action: y
    You yell at the octopus, "go watch TV!"
    The octopus wriggles free of your grasp.
    The octopus keeps harassing you.
    Your action: g
    You grab the octopus with your mighty European arms.
    Your action: t
    You throw the octopus onto the ground.  It is momentarily dazed.
    Your action: h
    You try to hide, but there is nothing to hide behind.
    The octopus recovers from its fall.
    The octopus keeps harassing you.
    Your action: g
    You grab the octopus with your mighty European arms.
    Your action: t
    You throw the octopus onto the ground.  It is momentarily dazed.
    Your action: e
    You swim east, finally free of the octopus' garden.
    Your action: q
    You quit, weary of undersea life already.
    
  2. [30 points] The current simulation works fine within the octopus' garden, but once Ringo leaves, the `g' and `t' commands should not have the same effect (of manhandling a cephalopod mollusk). Make the effect of every instruction depend on Ringo's location by adding another level of conditionals to your program, so that a command of `g' in some other nightmare creature's garden will grab that creature instead of the octopus. In order to reduce the complexity of this task, you should use an enumerated type Location for all the possible locations in the simulation (but you still shouldn't use any functions yet).

    Have your program simulate a region to the east of the octopus' garden, filled with electric eels, and a region to the north of that, containing a crab. If Ringo grabs the electric eels, he gets several kwH of free electric power for his jam box (free electric power is the only good thing Ringo will find under the sea), but if he grabs the crab, it will cut him and leave a scab.

    Example:

    % a.out
    You're in an octopus' garden, just like in your song, except
    the octopus is throwing things at you and frightening you.
    Your action: g
    You grab the octopus with your mighty European arms.
    Your action: t
    You throw the octopus onto the ground.  It is momentarily dazed.
    Your action: e
    You swim east, out of the octopus' garden.
    This area is packed full of electric eels.
    Your action: g
    You grab some eels.  Their ultra-conductive skin makes them very 
    slippery and painful to hold, but the thought of all that free 
    electricity strengthens your grip.
    Your action: n
    You swim north, away from the electric eels.
    There is a crab here.
    Your action: g
    You grab the crab.
    This will taste good with a Diet Coke, or a Tab.
    Your action: q
    You quit, weary of undersea life already.
    
  3. [30 points] Good work junior aquanauts, when Ringo gets a load of this simulation he'll be the biggest landlubber you know! But with all those nested conditionals, the simulation program has begun to look a bit messy, hasn't it? Since you will have the good fortune to work with this program every week for the rest of the semester, you'll want to make it easier to read and edit. Do this by creating a separate Move function to handle the four movement commands, matching the following prototype.

    Location Move ( Location start, char direction ) ;

    which takes an enumerated location type for the starting location and a character representing the direction of movement (using the first letter of the direction, as in the user interface command), and returns the resulting location. The behavior of the program should not change from the version in problem 2, however.