2
\$\begingroup\$

I am making a text-based adventure game. How should I go about changing to another scene if the player wants to e.g. go to the house or walk down the road?

I am using Objective C, but C will work also. Here is my code so far:

printf("Welcome to Kingdom Quest! This is a text game.\nYou will be shown what is going on ");
printf("and it is up to you to decide what to do. Use words like get, go and pick up.\nDo not use any upper case letters.");

printf("\n\nYou are in a feild and it is almost nighttime. You are facing north. The only thing you have is an empty ruksak.");
printf("\nWhere do you want to go");
printf("\n>");

while (true) {

char string[256];
fgets(string, 255, stdin);


if (strcmp(string, "go north\n") == 0 || (strcmp(string, "go n\n") == 0))
{
    printf("There is a rusty old sword on the ground.");
    printf("\n>");
}
else if (strcmp(string, "get sword\n") == 0 || (strcmp(string, "pick up sword\n") == 0))
{
    printf("Sword taken.");
    printf("\n>");
}
else if (strcmp(string, "go south\n") == 0 || (strcmp(string, "go s\n") == 0))
{
    printf("Nothing is this way.");
    printf("\n>");
}
else if (strcmp(string, "go east\n") == 0 || (strcmp(string, "go e\n") == 0))
{
    printf("There is a dirt road here.");
    printf("\n>");
}
else if (strcmp(string, "go west\n") == 0 ||  (strcmp(string, "go w\n") == 0))
{
    printf("There is a small mountain up ahead, with a house on top of it.");
    printf("\n>");

}
else
{
    printf("What?");
    printf("\n>");
}
\$\endgroup\$
4
  • 6
    \$\begingroup\$ You might be interested in using a language which was designed for making this sort of game. inform7.com \$\endgroup\$ Feb 28, 2013 at 21:33
  • \$\begingroup\$ +1 for making a text adventure game. Brings back fond memories. (Zork!) \$\endgroup\$ Feb 28, 2013 at 21:52
  • \$\begingroup\$ Thanks, but i am using Xcode to do it, and i am limited to what languages i can use. \$\endgroup\$ Feb 28, 2013 at 22:06
  • \$\begingroup\$ That's not really Objective-C, just C. \$\endgroup\$ Mar 1, 2013 at 8:29

2 Answers 2

4
\$\begingroup\$

Ok, let's go at this in a multi-stage fashion:

Room representation

Before we can do anything else, we need to consider how to represent the rooms internally. This is just a rough thumbnail sketch, refine as necessary:

@interface TARoom : NSObject {
  NSDictionary *_links;    // Contains neighbouring rooms.
  NSString *_identifier;   // This will become important later.
  NSString *_description;  // The text that displays when you enter a room.
  TAInventory *_inventory; // This can get as simple or as fancy as you want, but for now we'll just pretend it's a full class.
}

@end

room.links is essentially a { north: TARoom(identifier) } type dictionary.

Input handling

With TARoom in place, we can focus on handling input, as a method on a notational TAGame class:

// Object here refers to a gramatical object, rather than Objective-C object.
// verb is essentially the first word of a string from the input system, and object is the rest
- (NSString *)handleVerb:(NSString *)verb withObject:(NSString *)object {
  // the player wants to move
  // a refinement would be providing synonyms
  if ([verb isEqualToString:@"go"]) {
    TARoom *destination;

    if ((destination = [self.currentRoom.links valueForKey:object])) {
      // the room exists
      return [self moveToRoom:destination]; // -[TAGame moveToRoom:] returns the room description, for instance
    } else {
      // no such room
      return [NSString stringWithFormat:@"You cannot go \"%@\"", verb];
    }
  } else if ([verb isEqualToString:@"get"]) {
    // left blank as an exercise to the reader
  } else {
    return [NSString stringWithFormat:@"I don't know \"%@\"", verb];
  }
}
\$\endgroup\$
0
8
\$\begingroup\$

You could think of individual places as "rooms" with "doors" connecting them:

rooms labelled A, B and C

To implement this, you could create a struct Room to hold a room, with fields for a set of items currently in it and what directions its exits lie in. Then simply keep an array of all rooms and have a pointer to the one the player is currently in.

There are ways of getting extra fancy with this, but simplicity is a virtue in programming.

\$\endgroup\$
8
  • \$\begingroup\$ can you show me an example with code? \$\endgroup\$ Feb 28, 2013 at 22:07
  • 1
    \$\begingroup\$ I've forgotten much of my C, so I'd get it wrong! :) If you have trouble implementing it, your programming skills likely need some refreshing: K&R is a great C reference. \$\endgroup\$
    – Anko
    Feb 28, 2013 at 22:13
  • \$\begingroup\$ Ok, well thank you, i am new to c, so i thought making a game would be a good way to learn. \$\endgroup\$ Feb 28, 2013 at 22:21
  • \$\begingroup\$ @code That's how I learnt to program too. Best of luck! \$\endgroup\$
    – Anko
    Feb 28, 2013 at 23:25
  • 1
    \$\begingroup\$ Here's a sample program to get you started: ideone.com/apGmWW \$\endgroup\$ Mar 1, 2013 at 1:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .