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

2. The main loop

Let’s bring some basic interaction into our program!

The basic principle of a text adventure is simple:

  1. The player enters a command.
  2. The program parses and executes the command.
  3. Repeat steps 1 and 2 until the player decides to quit.
The code sample below consists of three functions, one for each step:
  1. Function getInput.
  2. Function parseAndExecute.
  3. Function main, which takes care of calling the other two functions repeatedly.

main.c
  1. #include <stdbool.h>
  2. #include <stdio.h>
  3. #include "parsexec.h"
  4. static char input[100] = "look around";
  5. static bool getInput(void)
  6. {
  7. printf("\n--> ");
  8. return fgets(input, sizeof input, stdin) != NULL;
  9. }
  10. int main()
  11. {
  12. printf("Welcome to Little Cave Adventure.\n");
  13. while (parseAndExecute(input) && getInput());
  14. printf("\nBye!\n");
  15. return 0;
  16. }

Explanation:

I implemented function parseAndExecute in a separate module, as it is the function that is subject to change in chapters to come.

parsexec.h
  1. extern bool parseAndExecute(char *input);
parsexec.c
  1. #include <stdbool.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. bool parseAndExecute(char *input)
  5. {
  6. char *verb = strtok(input, " \n");
  7. char *noun = strtok(NULL, " \n");
  8. if (verb != NULL)
  9. {
  10. if (strcmp(verb, "quit") == 0)
  11. {
  12. return false;
  13. }
  14. else if (strcmp(verb, "look") == 0)
  15. {
  16. printf("It is very dark in here.\n");
  17. }
  18. else if (strcmp(verb, "go") == 0)
  19. {
  20. printf("It's too dark to go anywhere.\n");
  21. }
  22. else
  23. {
  24. printf("I don't know how to '%s'.\n", verb);
  25. }
  26. }
  27. return true;
  28. }

Explanation:

Sample output
Welcome to Little Cave Adventure.
It is very dark in here.

--> go north
It's too dark to go anywhere.

--> look around
It is very dark in here.

--> eat sandwich
I don't know how to 'eat'.

--> quit

Bye!

This program, however small it may be, already responds to commands like go north, look around and quit; see the sample output on the right. But of course, it is not much of a game. It has no locations, no items; all there is, is darkness. So in the next chapter, we will begin adding locations.

Quit

Notice our program stops at nothing but a command from the player (either quit or end-of-file). So what about ‘game over’ situations, i.e. death and victory? Well, those are conditions that might end the game, but not necessarily end the program.

There is also a practical problem with terminating the program without the player’s consent: in a windowing system, it is not unlikely for a text-based program to be running in a terminal window that immediately closes when the program ends, even before the player has had a chance to read the final message. We could delay this effect with “press any key to exit” or “wanna quit (y/n)”, but why bother when there is already a perfectly sane alternative? Just let the player type in ‘quit’ whenever he is ready to leave the imaginary world behind.

Naturally, all this is just an advice; you can take it or leave it. That’s the great thing about writing a game in a general-purpose language: you can mould it any way you like.


⭳   Download source code 🌀   Run on Repl.it

Next chapter: 3. Locations