Friday Afternoon Hack - Getting Beyond the Basics
Nov 13, 2009 15:28

Friday Afternoon Hack - Getting Beyond the Basics

We're still doing the weekly Friday Afternoon Hacks at The Hub Halifax. In my previous blog post, I talked about how I was going to finally catch up with my 12-year-old self and type in a BASIC adventure game program from 1983.

A surprising number of friends emailed me after the last post, saying they remember that book, and that they had never managed to type in the program either.

So for those who are interested, here's the game. You can get the code here. Then you can copy-and-paste it into this JavaScript-based BASIC interpreter.

It's certainly not the best text-based adventure ever, but it's surprisingly more interesting to play than I expected. You'll notice that you can get stuck in the Marsh and then can't do anything - there's no Quit command, but just click on the "STOP" button on the web page. If you don't know what to do, type HELP to get a list of commands.

Typing this in was a bit of a challenge. The source code in the book looks like it was taken directly from a dot-matrix printout of the real application code - quirky line numbers and all. The program uses $O and $D as variable names - It's still a bad idea, but it was especially a bad idea back when fonts were blurry and blocky and hard to tell apart.

Also I can't imagine what it would have been like typing this program in with the rudimentary editors available in 1983. Copying and pasting repetitive code, doing bulk search and replace, using version control, even simply being able to save my progress in a fraction of a second - these are all things I take for granted now that really didn't exist on the original vintage computers of the early 80s.

Now that the BASIC version works, I'm going to be rewriting it in as many platforms and technologies as I can. First up, to keep things simple, I'm rewriting this in Ruby, running on the command line with standard I/O.

I thought I would take one pass through transliterating the code from BASIC to Ruby as closely as possible, and then rewrite it using Ruby features and idioms. Unfortunately, the original BASIC approach used GOTOs and GOSUBs everywhere, which is a 'feature' that thankfully hasn't been implemented like that in Ruby. Here's the heart of the program's command parser, for example:

460 ON VB GOSUB 500,570,640,640,640,640,640,640,640,980,980,1030,1070,1140,1180,1220,1250,1300,1340,1380,1400,1430,1460,1490,1510,1590

Pretty sexy, eh? I'm still going to write this in Ruby twice, but in a different way.

The BASIC program mushes all of the components together: the main event loop, the locations, the allowable words and what they do, how magic items work, scoring - everything. The data is stored in a set of parallel arrays that aren't otherwise linked together. The parsing, as you can see above, depends entirely on jumping to line number references.

The first Ruby version will store the data and handle events in roughly the same way as the BASIC program. The second Ruby version will try to be as open as possible. The core application will be an event loop and a set of frameworks for managing locations and setting up data, and the data and vocabulary will be set up as a set of configuration files. That way, the game can be modified by simply changing these configuration files. If I was working in Java, I'd probably do these in XML - but for this I'll use actual Ruby files with arrays and hashes.

Some of the items or actions in the game have special behaviours - such as changing the exits in a specific room, or teleporting you to a random location. If I was doing this in Java and setting up the configuration in XML files, I'd have to plan ahead and have configuration parameters for every possible special case. With Ruby, since the configuration files are in Ruby, and since code can be passed around just like data, I can include code blocks directly in the configuration elements.

Even though I've been using Ruby and Rails for several years, I really haven't explored this whole passing code blocks things very thoroughly - so this should be a fun exercise. I figure this will also help get me ready for when I write this in LISP.

Halifax Friday Hack and Back to Basics
Oct 31, 2009 08:26
Thoughts on HAML
Feb 10, 2010 07:54
Other Blog Posts
My Expanded Twitter Thread about BurgerWeek 2021 This Is Nowhere: The Server Side Building a React Native App Without Tears - Part 3 Building a React Native App Without Tears - Part 2 Building a React Native App Without Tears (Mostly) This Is Nowhere: The Memento Edition This Is Nowhere: Aspects of Accessibility Presentations About NowHere This Is Nowhere: Head-First Into React Native This Is Nowhere: Bloomsday Halifax This Is Nowhere: Why an HTML/JavaScript Single-Page App With GPS Is A Bad Idea This Is Nowhere: GPS and Wayfinding and More UX This Is Nowhere: The Single-Button UX This Is Nowhere: Don’t Just Stand There! This Is Nowhere: Finding My Duck Finding Burgers Fast: My DIY Halifax Burger Week Site "This is Nowhere" at PodCamp Halifax 2018 The Diary Diaries: Fixing Remembary's Facebook Connection Special Leap Day Edition of "Some Weird Things About Time" What's Up With Remembary Can't get pg_dump To Work Now That Heroku Has Upgraded Postgresql to 9.4? The Best Thing I Ever Did To Promote My App If You Build It, They WON'T Come #deployaday, My Big Hairy Plan for 2015 Extracting Plain Text from an NSAttributedString My Year of "Hits" Part 2: Remembary Rolling My Year of "Hits" Part 1: Remembary Rises (and Stumbles) Handy Little Test Method to Check for Translations in Rails Apps My Suddenly Slow-Waking MacBook Air Indie App PR: Keeping Control of Your Tone A Quick Note on 'clone' in Rails 3.2 My eBook Apps 2: iOS, JavaScript, and Ruby My eBook Apps 1: Introduction Quick Tip: No Sound on Mountain Lion My Upcoming Talk at PodcampHFX 2012: My Year of "Hits" Building at the Speed of Funny Screencast Tips Remembary's Cool New Picture Support Indie App PR 2: Keeping On Top Of User Feedback Indie App PR 1: How to Handle an App Disaster Giles Bowkett Diary Project 2 Remembary Video Congratulations! Welcome to Your Nightmare! How My iPad App Remembary Took Off Why You Should Have an App in the App Store (Even If You Probably Won't Make Any Money) PodCampHFX Remembary Presentation - Part 3 How I Used MailChimp Autoresponders to Promote Remembary PodCampHFX Remembary Presentation Part 2 PodCampHFX Remembary Presentation Part 1 Why AdWords Ads Don't Work for iPad Apps Remembary is Sponsoring PodcampHFX Why Can't I Resize my Views in Interface Builder? Momento and Remembary Concerning Remembary iPad-Friendly eBooks of Gracian's Art of Worldly Wisdom Project Report: PTOS2 A Quick Note on Encryption We're all LUsers Thoughts on HAML Friday Afternoon Hack - Getting Beyond the Basics Halifax Friday Hack and Back to Basics Quote from Wil Shipley FutureRuby Make Web Not War Busy Week I: Toronto Ruby Job Fair Employment.nil - the Toronto Ruby Job Fair Code Count: Ruby on Rails vs. C#/ASP.NET A Brief Note on Twitter The Hub Halifax and Mobile Tech for Social Change Deep Thoughts on Microsoft From The Accordion Guy The Two Kinds of Defensive Programming Presentation - Fixing Careerious: From C#/.NET to Ruby on Rails Enterprise! Presenting at Ruby on Rails Project Night - May 7th New Name and New Look for Careerious/Clearfit FutureRuby and More From Unspace Health Tips for Programmers This tables meme won't die Careerious - Ruby and Rails vs. C#/.NET Yeah I Use Tables For Layout, So Sue Me The Different Kinds of Done Giles Bowkett's RubyFringe presentation OfficeTime: Great Time-Tracking App for OS X Back With A New Look Non-DRY Feed torontorb Keeping Your Sanity With The Command Design Pattern shindigital Is All Grown Up! (according to the spambots) Startup Stars? I'm so bored! The Magic Words for RMagick Jennifer from Operations You see? Naming is HARD Business Software as Process Documentation Deployment note: 'execve failed' Steve Jobs on Market Research Why Canada Is Better for Entrepreneurs "Program first and blog second" Toronto Tech Collage The MacBook Air Is A Roadster RubyFringe! Quote of the Week: Steve Yegge Starting Up: Cards Great design tool: Starting Up: The Logo Quotes Of The Day: Hedge Fund Interview TSOT Ruby / Rails Presentation Night - Part 1 Moneyworks: Accounting Software for Canadians on OS X Starting Up: The Name Nice logo, but why is your site so bland? Welcome to