by Ruud Helderman <firstname.lastname@example.org>
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.
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 become a general-purpose language, but it most certainly did.
Yes, there are general-purpose programming languages that are better than C in terms of maintainability and reliability. The ideas proposed here apply equally well to those languages. If you prefer Java, C# or Python over C, it should not be too difficult to translate my code samples.
My choice for C is a personal one. I know the language well, and I like it a lot. But more importantly, C is close to the metal. I have always admired pioneers like Scott Adams who managed to bring the adventure game genre to the early home computers, despite extreme memory constraints. The best of these attempts were written in assembly language, so I was curious how far I could get in a language that was originally designed as a portable alternative for assembly. This may feel like cheating: most 8-bit computers did not have an (optimizing) C compiler. But assuming we will not be relying on advanced libraries, C maps pretty well to assembly, even if you have to do so by hand.
I will try to keep this story 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 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.
Welcome to Little Cave Adventure.
It is very dark in here.
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