> go east

I’ve had a connection with text adventures for a long time, coming back to the format year after year. For the most part I’m not a player of text adventures, but instead an enthusiast for implementations thereof. I’ve talked before about using this as a way to learn a new language and to learn about parsers, but I also just like making them, an effort that has shaded off from actual plausible implementations into mildly unusual attempts at the format. The latest of these is to just host the world in the filesystem.1

├── North Side of House
│   ├── west -> ../Back of House
│   ├── rock
│   └── east -> ../House
├── Back of House
│   ├── south -> ../South Side of House
│   ├── west  -> ../Grove
│   └── north -> ../North Side of House
├── South Side of House
│   ├── east -> ../House
│   └── west -> ../Back of House
├── Grove
│   └── east -> ../Back of House
└── House
    ├── leaflet
    ├── north -> ../North Side of House
    └── south -> ../South Side of House

With this implementation, a shell can provide a minimal environment that serves as a text adventure player:

export PROMPT="> "

chpwd () {
  cat .description

alias go=cd
alias examine=cat
% source ./game.zshrc && cd world/Grove

You are in a grove of tall trees.
To the east there appears to be a house of some kind.
> go east
Back of House

There's a house nestled in the trees; you've come on its back side.
To the west lies the grove you came from.
If you want to get to the front of the house, 
you'll have to go north or south.

I actually took this a few steps further with support for putting things in your inventory (just another folder) and an autosave feature (another symbolic link), as well as a handful of other commands for fun. If you want you can check out the whole thing. (Only tested on macOS, but will probably work on other Unixy systems with zsh installed too. The reset script is Mac-only, though.)

I like how you could build a real program on top of this implementation (one that didn’t allow you to execute command lines in the middle of the game), but you can also just use a shell, because why not. A real text adventure probably has more to it than just moving around and looking at things, though, so at some point you’d probably want support for more complex actions that you’d be implementing on top of the filesystem (using special files like .description here, or maybe extended attributes) instead of just using the structure directly.

To close, I’ll include a snippet of my favorite text adventure:

"James: west / David: Kanye can't save you now."

  1. Diagram generated by tre↩︎