Walking Around In Our World



Ok, now that we can see our world, let's write some code that lets us walk around in it. The function walk-direction (not in the functional style) takes a direction and lets us walk there:

(defun walk-direction (direction)
  (let ((next (assoc direction (cddr (assoc *location* *map*)))))
    (cond (next (setf *location* (third next)) (look))
	  (t '(you cant go that way.)))))

The special command let allows us to create the local variable next, which we set to the path descriptor for the direction the player wants to walk in- cdr just chops the first item off of a list. If the user types in a bogus direction, next will be nil. The cond command is like a chain of if-then commands in Lisp: Each row in a cond has a value to check and an action to do. In this case, if the next location is not nil, it will setf the player's location to the third item in the path descriptor, which holds the symbol describing the new direction, then gives the user a look of the new place. If the next location is nil, it falls through to the next line and admonishes the user. Let's try it:


(walk-direction 'west)
==> (YOU ARE IN A BEAUTIFUL GARDEN.
THERE IS A WELL IN FRONT OF YOU.
THERE IS A DOOR GOING EAST FROM HERE.
YOU SEE A CHAIN ON THE FLOOR.
YOU SEE A FROG ON THE FLOOR.)
Now, we were able to simplify our description functions by creating a look command that is easy for our player to type. Similarly, it would be nice to adjust the walk-direction command so that it doesn't have an annoying quote mark in the command that the player has to type in. But, as we have learned, when the compiler reads a form in Code Mode, it will read all its parameters in Code Mode, unless a quote tells it not to. Is there anything we can do to tell the compiler that west is just a piece of data without the quote?

<< PREVIOUS                 NEXT >>