How to program a text adventure in C

1. Introduction

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, and you would like to learn some more 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.

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. But while writing my own humble text adventure, I learned that C fits the bill quite well.

The ideas you find here apply not only to C but to other programming languages as well. If you prefer Java or Python over C, it should not be too difficult to translate my code samples.

I tried to keep this tutorial readable for a broad audience, without too much off-topic elaboration. For terms that might warrant further explanation, please follow the hyperlinks to relevant Wikipedia articles.

Throughout this tutorial, we will be developing a fully functional text adventure game. This will be done in an incremental fashion. With every chapter, some code is added to our program. Each increment, however small, adds some value to the game. And every time, the result is a working program, ready to run once built with a proper C compiler.

We start off with just 10 lines of code; it is little more than a trivial “Hello World” program. If you can't even get this simple example to work, you should find some help with that first, before you are ready to move on to the next chapter.

Sample output Welcome to Little Cave Adventure. It's very dark in here. Bye!

main.c 1 2 3 4 5 6 7 8 9 10 #include <stdio.h> int main() { printf("Welcome to Little Cave Adventure.

"); printf("It's very dark in here.

"); printf("

Bye!

"); return 0; }

Some explanation for those not quite familiar with C: Line 1: a text adventure needs no fancy libraries; standard input/output is sufficient, and is widely available. But feel free to replace it with any user interface library you like, for example Glk.

Line 3: function main is the starting point of any C program.

Line 5, 6, 8: output text to the screen; the escape sequence

represents a newline.

represents a newline. Line 9: function main returns a zero exit code to successfully complete the program.

Next chapter: 2. The main loop