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 > Walking Around in Our World

Walking Around in Our World

Now that we can see things in our world, let’s write some code so that we can walk around. The walk function (not in the functional style) takes a direction and lets us walk there:

(defun walk (direction)
  (let ((next (find direction
                    (cdr (assoc *location* *edges*))
                    :key #'cadr)))
  (if next
      (progn (setf *location* (car next))
             (look))
      '(you cannot go that way.))))

First, this function looks up the available walking paths in the *edges* table, using the current location . This is used by the find function to locate the path marked with the appropriate direction . (find searches a list for an item, then returns that found item.) The direction (such as west, upstairs, and so on) will be in the cadr of each path, so we need to tell find to match the direction against the cadr of all the paths in the list.


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

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

  

Start a Free 10-Day Trial


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