Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

5. Building a Text Game Engine > Describing the Location

Describing the Location

Now that we’ve created an alist of our game world, we need to create a command to describe a location. To accomplish this, we’ll use the assoc function to find the correct item in the list using a key:

> (assoc 'garden *nodes*)
(GARDEN (YOU ARE IN A BEAUTIFUL GARDEN. THERE IS A WELL IN FRONT OF YOU.))

Using assoc, we can easily create the describe-location function:

(defun describe-location (location nodes)
   (cadr (assoc location nodes)))

To use this function, we pass in a location and the *nodes* list:

> (describe-location 'living-room *nodes*)
(YOU ARE IN THE LIVING-ROOM. A WIZARD IS SNORING LOUDLY ON THE COUCH.)

Why don’t we just reference the *nodes* variable directly from the describe-location function? Because this function is written in the functional programming style. In this style, a function will reference only parameters or variables declared in the function itself, and it will do nothing besides return a value, which is the description of the location in this case.


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint