Sunday, August 10, 2014

Write a ZMachine!

Your assignment: Write a ZMachine capable of playing the original Zork.

The game Zork was published in 1980 and remains one of the most loved interactive fiction games of all time.   The game was originally written in a variant of MIT's MDL language, and run in a virtual machine known as the ZMachine.


While I was familiar with Zork, my first real exposure to the ZMachine itself came when we decided to include a working version of Zork as an easter egg in the game Call of Duty: Black Ops.  We couldn't find an existing ZMachine with licensing that met our requirements, so I sat down one weekend and started to write one from scratch.  Another engineer also got involved, and in a very short time our easter egg was working perfectly.  We introduced a new generation to a wonderful game, and as a happy side-effect our implementation probably ended up becoming the most commercially successful one of all time.


Since then, writing a ZMachine has become my default project for messing around in new programming languages or just getting myself out of a creative funk.  There are a number of reasons that I think this project is great for programmers of all experience levels.



It's fun!


I'm not sure why, but I always end up having a blast with this project.  Maybe it's the VM itself, which has a very nice overall design.  Maybe it's the fact that you're seeing a piece of gaming history come to life before your eyes.  Maybe it's just the fact that you're playing a classic of the interactive fiction genre.   But whatever the reason, it's a ton of fun.

There's nothing quite like finally implementing the last instruction and seeing the classic Zork intro pop up on screen in an interpreter that you just finished writing.  I'm on my third ZMachine implementation and it's still incredibly rewarding every time I see that first prompt.


It's challenging, but not difficult.


The ZMachine itself is a relatively straightforward piece of software, but it includes some complex interactions that present an interesting design challenge.   Looking at how you solve these problems and comparing with others can give you some interesting insight into how you write software and how you can improve the ways you design code.

I feel like system and code design is one of the areas in which novice and beginning programmers are weakest.  For more experienced engineers, maybe we spend more time fixing bugs or maintaining existing code than we do actually thinking out how a new system will work.  Writing a ZMachine exercises those muscles.  I find it valuable, especially when working in new languages where idiomatic code may look different than what I'm used to writing.


It's not time-consuming... unless you want it to be.


A good ZMachine can be written in a weekend, or more likely spread out across a few of them if you don't want to rush it.  But it can become complicated if that's what you want.

Did you enjoy writing an interpreter?  How about writing a debugger?  What about writing a compiler for the original Zork MDL source?  What about writing your own interactive fiction game, toolkit, or VM?

Want to ease into a new UI toolkit?  Hook your ZMachine up to it.  Want to play around with an IRC bot or web service?  Have it run the ZMachine.  There are thousands of ways to branch this project into a series of others.


Interested?


Here are some resources to help you get started.


  • Z-Machine Standards Document - This is your bread and butter.  The 1.1 standard is hard to read and there are a few non-obvious things about how some of the instructions work, but this is the best place to start.  If you are just interested in running Zork, you can ignore everything that's higher than Version 3.
  • ZTools - The disassembler provided by this toolkit can prove quite helpful in checking things out.
You'll also need a copy of zork.z3 or another Version 3 binary, but I can't distribute any of them here.

No comments:

Post a Comment