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

3. Locations

Traditionally, a text adventure is a virtual world consisting of (many) different locations. Though this is not essential (some adventures take place in a single room!), it is a good way to explain the use of data structures.

We start by defining a struct to represent a location. It contains two simple attributes to start with (more may follow later).

struct location { const char *description; const char *tag; };

Next, we define an array of locations. For now, we keep it really simple: just two locations.

struct location locs[2];

We can use an initializer to immediately fill in all the static data.

Very basic map

struct location locs[2] = { {"an open field", "field"}, {"a little cave", "cave"} };

Using the data from the array is simple. For example, the following line of code will output "You are in an open field."

printf("You are in %s.\n", locs[0].description);

Let’s put this into practice. In the code sample from the previous chapter (parsexec.c), we alter lines 4, 18 and 22 (slightly highlighted below).

Sample output
Welcome to Little Cave Adventure.
You are in an open field.

--> go cave
OK.
You are in a little cave.

--> go field
OK.
You are in an open field.

--> go field
You can't get much closer than this.

--> look around
You are in an open field.

--> go kitchen
I don't understand where you want to go.

--> quit

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

Explanation:

Next, we add a new module to the project.

location.h
  1. extern void executeLook(const char *noun);
  2. extern void executeGo(const char *noun);
location.c
  1. #include <stdio.h>
  2. #include <string.h>
  3. struct location {
  4. const char *description;
  5. const char *tag;
  6. }
  7. locs[] = {
  8. {"an open field", "field"},
  9. {"a little cave", "cave"}
  10. };
  11. #define numberOfLocations (sizeof locs / sizeof *locs)
  12. static unsigned locationOfPlayer = 0;
  13. void executeLook(const char *noun)
  14. {
  15. if (noun != NULL && strcmp(noun, "around") == 0)
  16. {
  17. printf("You are in %s.\n", locs[locationOfPlayer].description);
  18. }
  19. else
  20. {
  21. printf("I don't understand what you want to see.\n");
  22. }
  23. }
  24. void executeGo(const char *noun)
  25. {
  26. unsigned i;
  27. for (i = 0; i < numberOfLocations; i++)
  28. {
  29. if (noun != NULL && strcmp(noun, locs[i].tag) == 0)
  30. {
  31. if (i == locationOfPlayer)
  32. {
  33. printf("You can't get much closer than this.\n");
  34. }
  35. else
  36. {
  37. printf("OK.\n");
  38. locationOfPlayer = i;
  39. executeLook("around");
  40. }
  41. return;
  42. }
  43. }
  44. printf("I don't understand where you want to go.\n");
  45. }

Explanation:

Now the commands look around and go <location> have really come to life! Feel free to experiment by adding more locations. Of course, the game is still far from perfect. Right now, there is too much freedom: from every location, the player can freely jump to any other location. In chapter 6, we will show how to ‘connect’ locations. But next up, we will start adding objects to the game.


⭳   Download source code 🌀   Run on Repl.it

Next chapter: 4. Objects