How to program a text adventure in C by nivethan
by Ruud Helderman
<r.helderman@hccnet.nl>
Licensed under
MIT License
This is not a C tutorial; there are plenty of those on the web.
The reader is assumed to have some basic knowledge of programming in general,
and preferrably the C programming language in particular.
Maybe you just finished reading a tutorial somewhere,
and you would like to learn some more about programming
by studying other people’s source code.
Or could it be that you really are genuinely interested in
the ancient art of writing text adventure games from scratch?
Maybe you are just having a sense of nostalgia about the old days,
when life was simple, and so was software.
In either case, you have come to the right place.
In the 1980s,
text adventures
were a respectable genre of computer games.
But times have changed:
in the 21st century, they pale in comparison with modern
MMORPGs with 3D engines.
Where books survived the rise of film,
text-based games quickly lost the battle against their graphical counterparts.
‘Interactive fiction’ is kept alive by an active community,
but its commercial value is long gone.
There is a bright side to this:
authoring tools of professional quality are now available free of charge.
This is also the single biggest advice I can give you
if you intend to write your own adventure:
use one of the many dedicated
development systems.
Why C
But then, why this tutorial?
Why write an adventure game using a
general-purpose programming language?
Because doing so can be entertaining, challenging and educational.
The programming language
C
may not be the most obvious choice for writing a text adventure;
it’s nothing like LISP-based languages like
MDL and
ZIL.
Some people will claim C is not even a general-purpose language.
I disagree; maybe it should not have
7 Comments
serhack_
I was wondering: does anybody know if there are any good resources for writing a good text adventure? Any nice tips and tricks? Mainly related to the content. I guess it overlaps with "writing a good novel", but I bet there're some specific advices that can be applied to the text adventure.
I wanted to write my text adventure, but I'd offer reader to have multiple options, especially for those who are not really practical with english (includes myself ^-^).
metabagel
Section 9 – Code Generation uses awk to parse a text file to generate c files. Very nice.
zabzonk
I honestly think that writing an adventure can be best done by first creating an adventure-writing DSL (in C, if you like).
A few observations on the C code (I didn't read all of it):
– please, no strtok
– a little more concentration on the UI, for example not using strcmp to test inputs
– make all preprocessor definitions be uppercase
– those conditional operators confused the hell out of me – just use if/else
drwu
I thought it was something like LPMud or MudOS [0].
[0] https://mud.fandom.com/wiki/MudOS
parshua
The best way to implement a text adventure in C would be to implement a simple lisp interpreter in C and then implementing the actual game in a lisp DSL. Lisp lends itself surprisingly well to this, and defining game logic declaratively instead of imperatively is much more intuitive. Here are a few examples:
[1] http://www.ulisp.com/show?383X
[2] https://github.com/mswift42/MetalHead
[3] https://github.com/xlxs4/lisp-spels/blob/main/spel.el
HexDecOctBin
I thought it was going to be something like this: https://github.com/dfremont/glulx-llvm
dmbaggett
Many years ago (circa 1993) I ported the original Colossal Cave adventure by Crowther and Woods to TADS, a language created by Mike Roberts specifically for authoring text adventures. (Colossal Cave just came up recently here.)
https://ifdb.org/viewgame?id=c896g2rtsope497w
Graham Nelson ported my port to his Inform language, and Inform is probably your best choice if what you actually want to do is write a (plain text) adventure game.
If you want to learn C programming, writing a text adventure in C would be a fun learning project! But aside from pedagogy there’s no real reason to write a text adventure in anything other than Inform, TADS, etc. Not only is it much easier to use one of these purpose-built languages, with Inform you get multi-platform compatibility going back to the 8-bit era for free!
Personally if I had any free time, I’d be more interested in looking at how to use a frontier LLM like llama as an integral part of a text adventure. There was something like this using GPT-2 circulating on here a while back, but it was pretty rough.
However, it’s clear that if you figured out how to precisely control the LLM so it didn’t produce crazy stuff, you could realize the dream of truly realistic NPCs in these games. Text adventures would seem to be a perfect laboratory for experimenting with this.