Contents

1. Introduction
2. The main loop
3. Locations
4. Objects
5. Inventory
6. Passages
7. Distance
8. North, east, south, west
9. Code generation
10. More attributes
11. Conditions
12. Open and close
13. The parser
14. Multiple nouns
15. Light and dark
16. Savegame
17. Test automation
18. Abbreviations
19. Conversations
20. Combat
21. Multi-player
22. Client-server
23. Database
24. Speech
25. JavaScript

How to program a text adventure in C

by Ruud Helderman <r.helderman@hccnet.nl>

Licensed under MIT License

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 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.

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.

Incremental development

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 9 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 is very dark in here.

Bye!
main.c
  1. #include <stdio.h>
  2. int main()
  3. {
  4. printf("Welcome to Little Cave Adventure.\n");
  5. printf("It is very dark in here.\n");
  6. printf("\nBye!\n");
  7. return 0;
  8. }

Some explanation for those not quite familiar with C:

Trivial as it may be, the program does demonstrate the most important aspect of any text adventure: descriptive text. A good story is one of the things that makes a good adventure game. I deliberately kept it brief in my code sample. I will focus on the programming aspect of the game, and leave the rest up to your imagination.


⭳   Download source code 🌀   Run on Repl.it

Next chapter: 2. The main loop