Created
August 15, 2013 21:41
-
-
Save doug-wade/6245229 to your computer and use it in GitHub Desktop.
Robbie the robot!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;Set up the rooms | |
(setf rooms | |
'((living-room (north front-stairs) | |
(south dining-room) | |
(east kitchen)) | |
(upstairs-bedroom (west library) | |
(south front-stairs)) | |
(dining-room (north living-room) | |
(east pantry) | |
(west downstairs-bedroom)) | |
(kitchen (west living-room) | |
(south pantry)) | |
(pantry (north kitchen) | |
(west dining-room)) | |
(downstairs-bedroom (north back-stairs) | |
(east dining-room)) | |
(back-stairs (south downstairs-bedroom) | |
(north library)) | |
(front-stairs (north upstairs-bedroom) | |
(south living-room)) | |
(library (east upstairs-bedroom) | |
(south back-stairs)) | |
)) | |
;Set Robbie's starting location | |
(setf loc 'pantry) | |
(defun choices (room) | |
"Returns the rooms that robbie can move to from the given ROOM" | |
(cdr (assoc room rooms)) | |
) | |
(defun look (direction room) | |
"Returns the room to the given DIRECTION from the given ROOM" | |
(cdr (assoc direction (choices room))) | |
) | |
(defun set-robbie-location (place) | |
"Moves Robbie to the given PLACE by setting the variable LOC." | |
(setf loc place) | |
) | |
(defun how-many-choices () | |
(length (choices loc)) | |
) | |
(defun upstairsp () | |
(or (equal loc 'library) (equal loc 'upstairs-bedroom)) | |
) | |
(defun onstairsp () | |
(or (equal loc 'front-stairs) (equal loc 'back-stairs)) | |
) | |
(defun where () | |
(cond ((upstairsp) (append '(robbie is upstairs in the) (list loc))) | |
((onstairsp) (append '(robbie is on the) (list loc))) | |
(t (append '(robbie is downstairs in the) (list loc))) | |
) | |
) | |
(defun move (direction) | |
(cond ((not (assoc direction (choices loc))) '(ouch! robbie hit a wall)) | |
(t (setf loc (second (assoc direction (choices loc)))) (where))) | |
) | |
(defun get-user-choice () | |
(setf valid-choices '(north south east west quit)) | |
(print (choices loc)) | |
(format t "~%Choose a direction to move:~%") | |
(setf user-input (read)) | |
(cond ((not (find user-input valid-choices)) (format t "~%Please choose a valid direction.~%") (get-user-choice)) | |
(t user-input)) | |
) | |
(defun explore () | |
(print "Welcome to Robbie the Robot's world! 'Quit' quits.") | |
(print (where)) | |
(loop | |
(setf user-choice (get-user-choice)) | |
(if (equal user-choice 'quit) | |
(return) | |
(print (move user-choice)) | |
) | |
) | |
t | |
) | |
;Tests | |
;(print (choices 'pantry)) | |
;(print (choices 'kitchen)) | |
;(print (look 'north 'pantry)) | |
;(print (look 'west 'pantry)) | |
;(print (look 'south 'pantry)) | |
;(print (how-many-choices)) | |
;(print (where)) | |
;(print (move 'south)) | |
;(print (move 'north)) | |
(explore) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment