>>14
joey ([info]untoward) wrote,
@ 2011-05-29 20:04:00
Previous Entry Add to memories! Share Next Entry
Text adventure fun in Common Lisp
The lisp programming is going well. I've started to get more engaged by the ideas of it. This is my first introduction to the idea of functional programming and it's interesting! Right now the book is going through how to build a text adventure, and it's taking a very practical, very expandable form that I find interesting.
Every room is defined as a node, and the paths between rooms are edges. This is completely different from how I might have tried to do this in perl. Though the objects IN the rooms are defined using global variables, which isn't functional, but I suspect will be changed as the program evolves. Right now the code is REALLY dense. The recursion here reminds me of obfuscated perl japh code.
Here's what I have:
( defparameter *nodes* ' (( bedroom ( you are in a small bedroom. a
wizard is snoring loudly on the bed. ))
(front-step ( you are on the
front step. there is a
garbage-bin in front of you.))
(bathroom ( you are in the
bathroom. there is a welding
torch in the corner.))))
This sets up a list of the rooms (nodes) and also includes a brief description of them. There is some weirdness here because these descriptions are just lists, they aren't actually strings, but I am promised that this will be for the best.
Here's the function to return the description of a room:
(defun describe-location (location nodes)
(cadr (assoc location nodes)))
cadr is an insane lisp function that returns certain elements of a list. It takes various forms. The simplest of which are car and cdr. car returns the first item in a list. cdr returns everything else except the first item. cadr returns the first item in the list that is returned by cdr, that is: the second item in the original list. caddr returns the first item in the list returned after the first item is removed from the original and then the first is removed from THAT. (the third item) Good lord.
(defparameter *edges* '(( bedroom ( front-step outside door )
( bathroom
north hall))
(front-step (bedroom inside
door))
(bathroom (bedroom south
hall))))
This sets up the pathways between the rooms (edges). I actually find this very cool, and a neat way to do this. The format here is (room ( where-the-edge-leads directional-word form-of-pathway) so like, (bedroom (front-step outside door) means you use the door to go outside to the front step. Or, "a door leads outside to the front-step". Which is what this code does:
(defun describe-path (edge)
`(there is a ,(caddr edge) going ,(cadr edge) from here.))
and then, to describe all the edges from a certain node, you have this:
(defun describe-paths (location edges)
(apply #'append (mapcar #'describe-path (cdr (assoc location edges)))))
I am really enjoying this stuff. It's a bit of a puzzle, even though I've programmed before. It's fun!
(1 comment) - (Post a new comment)
[info]ffreak3
2011-05-30 12:52 am UTC (link)
Glad to hear you're enjoying LISP programming. :)
I almost always end up using LISP notation and ideas in general algorithms, finding them to be more elegant and simple in expressing most complex ideas.
I've been thinking of picking up Land Of LISP for myself recently, but I learn better with a lot of theory, and leaving the code practice to the student, as in LISPcraft . . . which is a bit outdated having been written in '84. Wait until you reach lambda calculus! Or when you realize the true power of car and cdr, (that is, with proper eval statements) ;)
(Reply to this)
(1 comment) - (Post a new comment)
About
Contact
Advertise
Jobs
Site News
More...
Help
Support / FAQs
Safety Tips
Get Involved
Volunteer
Developers
Legal
Terms of Service
Privacy Policy
Copyright
Abuse Policy
LJ Labs
LJ Aqua
More...
Store
Upgrade Account
Virtual Gifts
Merchandise
More...
Change language:
Current version:
v.80.3
» View Full Sitemap
Copyright © 1999 LiveJournal, Inc. All rights reserved.
Home
Create an account
Explore
Shop
LJ Extras
Games
Username: Create an Account
Forgot your login or password?
Facebook Twitter More login options
Password:
Remember Me
English • Español • Deutsch • Русский…