Homework #8
Friday the 20th of April at 5pm
Note: bold text in the examples is input from the user.
It's about time you set up an `e(x)amine' command, which prints a description of whatever object Ringo is holding:
void Examine(ObjectInfo objs[MAX_OBJS]);
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. The crab swims south, out of the crabs cottage, into the eels estates. Your action: t You throw the octopus onto the ground. It is momentarily dazed. The crab swims east, out of the eels estates, into the empty sea. Your action: e You swim east, out of the octopus' garden, into the eels estates. The crab tries to swim east, out of empty sea, but the current draws it back. Your action: g You grab an eel. The crab swims west, out of the empty sea, into the eels estates. Your action: x You have a luscious electric eel in your hand. It runs on three `D' batteries and fills your nerves with tingly electricity, and there's some delicate musculature on it somewhere. The crab swims south, out of the eels estates, into the marine refinery. Your action: q You quit, bored of undersea life already.
void Look ( ObjectInfo objects[100] ) ;which should search the array that's given as its argument for all objects at Ringo's current location (except for Ringo himself), and print a message for each object X like this:
There is an X here.It is fine to print a line for each object if there's more than one object (the example implementation does something a bit fancier, printing a comma-separated list of objects, but you need not do that). Then, add a new action `l' to the switch statement in main and call your Look function on that action. The function should use only the ObjectInfo array elements and the ObjectDescription structures that they point to. It should not use a large conditional statement (such as a switch case) containing several constant printf statements. Your simulator should also call this Look function on every movement action (i.e. `n', `s', `e' or `w'), so Ringo doesn't have to explicitly (l)ook every time he moves.
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. There is an octopus here. Your action: g You grab the octopus with your mighty European arms. The crab swims south, out of the crabs cottage, into the eels estates. Your action: t You throw the octopus onto the ground. It is momentarily dazed. The crab swims east, out of the eels estates, into the empty sea. Your action: e You swim east, out of the octopus' garden, into the eels estates. There is an eel here. The crab swims north, out of the empty sea, into the yellow submarine. Your action: s You swim south, out of the eels estates, into the marine refinery. There is a drum here. There is a drum here. There is a drum here. There is a drum here. There is a drum here. There is a drum here. There is a drum here. There is a drum here. There is a drum here. There is a drum here. There is a drum here. There is a drum here. The crab swims south, out of the yellow submarine, into the empty sea. Your action: q You quit, bored of undersea life already.
Change your simulator so that it allows Ringo to specify the name of the object he wants to pick up, so he doesn't risk this kind of fiasco again if there is more than one object in the same area. If Ringo gives a `g' (grab) command, your program should read another string of characters from the input line, and compare it to the name of each object at Ringo's location. The first object with a matching name should be moved to Ringo's hand. Just as in the previous problem, the code that does this should use only the ObjectInfo array elements and the ObjectDescription structures that they point to. It should not use a large conditional statement (such as a switch case) containing several constant printf messages.
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. There is an octopus here. Your action: g octopus You grab the octopus with your mighty European arms. The crab swims east, out of the crabs cottage, into the yellow submarine. Your action: t You throw the octopus onto the ground. It is momentarily dazed. The crab swims north, out of the yellow submarine, into the Micky Dolens' locker. Your action: s You swim south, out of the octopus' garden, into the stolen puppets. There is nothing interesting here. The crab swims west, out of Micky Dolens' locker, into the spooky sinkhole. Your action: e You swim east, out of the stolen puppets, into the marine refinery. There is a drum here. There is a drum here. There is a drum here. There is a drum here. There is a drum here. There is a drum here. There is a drum here. There is a drum here. There is a drum here. There is a drum here. There is a drum here. There is a drum here. The crab swims east, out of the spooky sinkhole, into the Micky Dolens' locker. Your action: g drum You grab a drum. The crab swims south, out of Micky Dolens' locker, into the yellow submarine. Your action: n You swim north, out of the marine refinery, into the eels estates. There is an eel here. The crab swims south, out of the yellow submarine, into the empty sea. Your action: t You throw a drum onto ground. The crab swims south, out of the empty sea, into the barnacle pile. Your action: l You look around... There is an eel here. There is a drum here. The crab swims west, out of the barnacle pile, into the marine refinery. Your action: y You yell, "go watch TV!" The crab swims west, out of the marine refinery, into the stolen puppets. Your action: g drum You grab drum. The crab swims north, out of the stolen puppets, into the octopus garden. Your action: l You look around... There is an eel here. The crab swims north, out of the octopus' garden, into the brine & spirits. Your action: q You quit, bored of undersea life already.