Monday, June 4, 2012

That's Not How You Do It or Data Driven BASIC Adventure Games!

I'm way better than Windows!
I was reading an online book that was originally published in the early 80s on writing DnD style text adventure games in BASIC.  As a matter of fact it's a book I used to own but somehow and somewhere between the 80s and now it disappeared.

I have several things that have done that and I often wonder where they've gone. Probably that sacred place where all my socks go during the dry cycle when doing laundry.

As I was reading I came across this page where the author shows how to implement room descriptions for your nasty little dungeon.  Go ahead and take a look. I'll stick around until you get back.

Back? Good! Did you notice what was "wrong"? Not really? Okay, let me point something out.

1020 RETURN
1030 REM ************
1040 REM ROOM 1
1050 PRINT "YOU ARE IN THE HALLWAY"
1060 PRINT "THERE IS A DOOR TO THE
SOUTH"
1070 PRINT "THROUGH WINDOWS TO THE NORTH YOU
CAN SEE A SECRET HERB GARDEN"
1080 RETURN

Here's the problem. He should have used data statements and an array to store the data. Instead he hard coded everything into the program.


Now granted there might have been memory issues with using data statements with arrays but the cleaner way to do it would be to do it my way.  For example here is three rooms read into an array.

10 DIM RM$(3)
20 FOR X = 0 TO 2
30 READ RM$(X)
40 NEXT X
50 DATA "YOU ARE IN THE HALLWAY. THROUGH THE WINDOW TO THE NORTH YOU CAN SEE A SECRET HERB GARDEN. "
60 DATA "YOU ARE IN THE KITCHEN. THE AIR IS FRESH WITH THE SCENT OF FRESHLY BAKED BREAD. "
70 DATA "YOU ARE IN THE BEDROOM. THE BED LOOKS AS IF SOMEBODY HAS RECENTLY BEEN SLEEPING IN IT. "

Why do it this way? Well, elsewhere in the book we find that he is using the variable RO to keep track of what room you are in. I have three rooms so RO will either be 0, 1 or 2. So, in my game loop if I want to describe where I am I would simply do this.

100 PRINT "YOU LOOK AROUND AND SEE THAT "
110 PRINT RM$(RO)

This makes sense because he uses Data statements elsewhere in the program to store where the exits would be in each room. If you use a four dimensional array to store the North, South, East and West direction then it's easy to use that information to direct your character as you play!

Anyway, it's pretty interesting to read this book and it has given me the programming bug and the desire to plunk around in an old 8-bit computer (ahem...C64...cough, cough).  Maybe that'll be a project of mine in the near future..

No comments:

Post a Comment