Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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>");
}
share|improve this question
5  
You might be interested in using a language which was designed for making this sort of game. inform7.com –  Trevor Powell Feb 28 '13 at 21:33
    
+1 for making a text adventure game. Brings back fond memories. (Zork!) –  Cameron Fredman Feb 28 '13 at 21:52
    
Thanks, but i am using Xcode to do it, and i am limited to what languages i can use. –  codegeek511 Feb 28 '13 at 22:06
    
That's not really Objective-C, just C. –  Williham Totland Mar 1 '13 at 8:29

2 Answers 2

up vote 4 down vote accepted

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];
  }
}
share|improve this answer
    
Awesome thanks dude! –  codegeek511 Mar 2 '13 at 2:36

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.

share|improve this answer
    
can you show me an example with code? –  codegeek511 Feb 28 '13 at 22:07
1  
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. –  Anko Feb 28 '13 at 22:13
    
Ok, well thank you, i am new to c, so i thought making a game would be a good way to learn. –  codegeek511 Feb 28 '13 at 22:21
    
@code That's how I learnt to program too. Best of luck! –  Anko Feb 28 '13 at 23:25
1  
Here's a sample program to get you started: ideone.com/apGmWW –  congusbongus Mar 1 '13 at 1:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.