Describing It All

Now we’ll tie all of these description functions into one easy command called look. Because this will be the actual command players can enter to look around them in the game, look will need to know a player’s current location. So, we need a variable to track the player’s current position. Let’s call it *location*:

(defparameter *location* 'living-room)

Because the *location* value is initialized to the living-room symbol, which occurs at the very start of the game, players will find themselves in the living room of the wizard’s house. At this point, we can write a look function to describe everything we need by having it call all of our descriptor functions:

(defun look ()
  (append (describe-location *location* *nodes*)
          (describe-paths *location* *edges*)
          (describe-objects *location* *objects* *object-locations*)))

Since the look function uses global variable names (such as *location*, *nodes*, and so on), the player won’t need to pass in any funky values in order to look out at the world. However, this also means that the look function is not in the functional programming style, because functions in the functional programming style reference only parameters or variables declared in the function itself. *location* and its ilk are global variables, so the look function doesn’t hold up muster.

Since the player’s location changes as the game progresses, look will do different things at different times in the game. In other words, the things you see when looking around will change depending on your location. In contrast, a function in the functional programming style always returns the same result, as long as the same values are given as parameters. The earlier functions we created, such as describe-location, describe-paths, and describe-objects, always return the same thing, no matter when they are called, as long as their parameters are kept the same.

Now here’s what we see when we use look:

> (look)
(YOU ARE IN THE LIVING-ROOM OF A WIZARD’S HOUSE.
THERE IS A WIZARD SNORING LOUDLY ON THE COUCH.
THERE IS A DOOR GOING WEST FROM HERE.
THERE IS A LADDER GOING UPSTAIRS FROM HERE.
YOU SEE A WHISKEY ON THE FLOOR.
YOU SEE A BUCKET ON THE FLOOR)
image with no caption
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.178.181