Programming Games: SpaceChem

In this series on games that involve programming as part of playing, I’ve so far covered RoboRally and LightBot, and this time it’s on to SpaceChem.

SpaceChem doesn’t help its own cause. For starters, it has a dull, misleading name: it may sound like a chemistry game, but it’s got only slightly more relation to chemistry than Angry Birds has to birdwatching. SpaceChem is a programming game, through and through. Its interface is rather, well, austere (see picture below), the tutorial is a bit sparse, and it’s difficulty curve is a lesson in what the function f(x) = x^x looks like. Now that we’ve set out its flaws, we can go on to look at why I believe SpaceChem to be the best programming game yet made.

Programming Model

SpaceChem is rather close to assembly language programming. It has two parallel threads of execution, each with an instruction pointer (called a “waldo”) and a series of instructions that are rather familiar: input, pick up (load), put down (store), output, and later on: conditional execution. The difference between SpaceChem and text-based assembler is that the “memory” is a 2D grid, the instruction pointer moves on a two-dimensional track of your choosing, and the data to be manipulated is represented by chemical atoms which can be bonded together. Here is the solution to one of the early puzzles involving simply shuffling a molecule from the input area (left) to the output area (right):

The blue instruction pointer (the hollow circle) starts at the START node. It then follows the path traced by the blue arrows. Each square can have 0 or 1 arrows and 0 or 1 instructions. The INPUT node will summon an atom to the input area, PICK UP will pick it up, so that the instruction pointer carries the atom around the track, until the PUT DOWN node is reached, and then the OUTPUT node sends it off. The instruction track is a loop, so the program will repeat until enough output atoms have been produced.

Although SpaceChem is primarily a sequence of instructions to be executed, as in RoboRally or LightBot, there is also a strong element of control flow. LightBot has functions and basic recursion, but SpaceChem has an abundance of loops, and in fact has control flow beyond most programs (instructions can usually be executed twice by lying where two control paths intersect). You must consider state and dependency a lot more: INPUT instructions must execute before the PICK UP instruction, so that the atom is ready to be picked up. You must make sure you output old data before attending to the new data: a primitive sort of scope. Problems must be broken down into smaller steps or else you have no hope of solving them. Small components (e.g. a fetch-bond loop) can (and must) be re-used as part of larger solution. The complexity is quickly on a different level to that of LightBot: if LightBot is for kids playing around, SpaceChem is for teenagers or adults who must apply serious brainpower.

Difficulty Wall

SpaceChem shares several traits with LightBot: it has a clearly defined goal (the atoms that you must output), all the state is visible at once (essentially, the grid, and the atoms currently present), and it’s relatively easy to see how the instructions get executed. But SpaceChem is not content to build up a progression of simple puzzles. You soon start using two threads in tandem (one red instruction pointer, one blue), which leads to issues with race hazards, so you have to start using a SYNC instruction to coordinate. You can see the two tracks in action in SpaceChem’s intro video:

You move on from programming one reactor (as seen earlier) to systems of several connected reactors wired together, so that the output of one reactor feeds to the input of another. Therefore large problems must be broken down into several smaller steps, each to be solved by a different reactor:

You can even save the reactors for later use. Code re-use and modularisation! As you progress through the game, you get access to new instructions: detectors for conditional execution, “flip-flops” for state. And soon the problems grow large enough that the eight by ten grid is very limited space, so you must be cleverer about what you are doing. But this really is programming: hardcore clever fiendish programming of the sort that old programmers get all nostalgic about, and that is largely (and thankfully) lost to the ages. Even as a fairly experienced programmer, I found it quite a challenge, and still have yet to complete all the levels! To give you a feel, here’s one of the neater and simpler solutions for a later level:

Bettering SpaceChem

SpaceChem is too hard to give to most students, but it has a nice programming model and visualisation that has a higher ceiling than any other programming game. I feel like there is a great game to be made, suited to a much wider range of students, based on SpaceChem’s core template. SpaceChem Jr, if you like. Dump the atoms to avoid the chemistry link and replace them with farm animals, or sticky balls of goo, or whatever. Remove the explicit INPUT and OUTPUT instructions, make the grid larger (to avoid the cramped space), greatly reduce the difficulty curve, maybe add in re-usable components on the reactor level, and find a better way of doing extra state than SpaceChem (flip-flops) and LightBot 2.0 (colours). I think that could be a real asset for introducing programming in the classroom via games.

SpaceChem is available for Windows, Mac, Linux, and also iOS and Android (though I wouldn’t recommend it on a phone — only for a tablet). There’s a demo if you want to give it a try (demo link is on the right-hand side of this page). The game is only 10 USD if you want the full thing — though if you hurry, it’s currently 50% off on Steam. In the next post I’ll look at a couple of the competitive aspects of SpaceChem and why programming games tend to teach some bad practice.


Leave a comment

Filed under Programming Games

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s