The Principles of Programming in SpaceChem

Programming, Video Games

SpaceChem screenshot

SpaceChem is a remarkable puzzle game about fake chemistry. The game challenges you to build a factory in order to transmute the given input molecules into the given output molecules. While chemistry is the theme, on a mechanical level it has more in common with programming. The methods used to tackle challenges in SpaceChem are akin to real techniques used by computer programmers. I’d like to elaborate on these manifold similarities, as well as explore how games like SpaceChem could be used to promote procedural literacy.

The player commands two circular “waldoes” by laying out paths and instructions for them. The waldoes follow the path and sequentially execute any instruction they come across. They can grab, drop, rotate, sync, bond, fuse, request input or dispatch output. The waldo’s analogue in computing is the processor, a hardware component that sequentially executes basic operations defined by machine code. Like processors, waldoes are the engines that drive the control flow of a SpaceChem factory.

When a waldo grabs a molecule, it gains the ability to perform instructions directly on it. In other words, a grabbed element is more readily and rapidly available than one lying elsewhere on the grid. Conceptually this is analogous to storing data in registers, a form of computer memory that is accessed very quickly and that the CPU manipulates directly. Just as a waldo can only grab one molecule at a time, computers have very few registers and must therefore rely on caching.

If grabbed molecules are like data in registers, then molecules left on the grid are cached. The cache is a larger, cheaper form of memory, but it is slower to read and write. Data must be written from the cache to a register in order to be manipulated directly by the CPU. The amount of memory in SpaceChem’s “cache” is governed by the area of the grid (8 x 10). Each coordinate on the grid can therefore be considered a unique memory address. This analogy is enforced mechanically: a factory “crashes” if two atoms collide on the grid, since you can’t store two values in the same memory address.

SpaceChem screenshot

Each factory has two waldoes, and they must be properly coordinated as they move through time and space. This coordination is facilitated by the sync node, which tells a waldo to wait until its twin has also hit a sync node. Parallel waldo management is akin to parallel programming, and they share the same perils: deadlock, starvation, race conditions, etc. The waldoes are like threads operating on shared memory space, and sync nodes are functionally similar to semaphores (operations that tell threads to signal and wait.) Operating two waldoes simultaneously in SpaceChem forces the player to confront the same shared resource problems as parallel computing.

SpaceChem and programming involve similar challenges: laying out simple instructions to achieve a complex result while managing limited time and resources. Like a good software specification, each puzzle is clearly presented as a black box defined only by its inputs and outputs. The player lays out instructions, starts up the factory, observes errors and corrects them, iterating until the puzzle is solved. Solutions can then be further optimized to take less time and use fewer instructions. SpaceChem and programming are engaging, flow-inducing activities because they have an identical inner loop: implementing, debugging and optimizing.

At this year’s GDC, Michael John asserted that programming is 21st century literacy. If computer programming is currently considered esoteric knowledge, it’s because our general education is not preparing students to think about problems in an algorithmic or systematic manner. Ian Bogost calls this procedural literacy: “the ability to reconfigure basic concepts and rules to understand and solve problems.” SpaceChem may not be an ideal game for the classroom (it’s far too difficult, for one), but it strongly suggests that the best way to learn about and engage with complex systems it to play with them.

→ 1 CommentTags: · 

One Response to “The Principles of Programming in SpaceChem”

  1. Max Williams Says:
    July 22nd, 2011 at 5:37 pm

    I’ve just started playing SpaceChem, and i love it. I’m also a software engineer, (and ex-videogame designer) and my first thought when playing the game was “Wow, this feels like working as a nanotechnologist”. In other words, at some point in the future (i hope!) we will have machines to “compile” matter from raw atoms, and the process of programming these machines might be similar to SpaceChem.

    There’s some lovely little touches in there too. I love how your score is shown on a population distribution instead of a high score table, so you can see if you are ahead of the curve or behind the curve, and also see the little bumps down at the left hand side and think “how the hell did they manage to do that!”. I also love how every aspect of every puzzle is changeable, even the start points: typically with a puzzle game like this, the difficulty progression would involve sticking more and more obstacles in your way, but in SpaceChem the requirements just get more challenging – again, just like programming.

    I agree with you that there should be more “procedural literacy” teaching in schools: too often, computer classes just involve learning how to consume software packages. When that package is Excel it’s just depressing as Excel is the most uninspiring introduction to programming that i could imagine: for many kids, the first function they ever write will be adding some numbers in a column, and they might think that that is the essence of computer science. SpaceChem would be a much better introduction to the exciting discipline of software engineering. BTW, SpaceChem fans should check out the boardgame RoboRally, which deals with similar procedural principles, with a massive dollop of chaos thrown in.

© 2007-2021 Matthew Gallant. Powered by Wordpress. Privacy Policy.