Text Adventure

Started by
9 comments, last by Hakiko 19 years, 1 month ago
Has anyone made an old style text adventure with a parser. One where you type Get Sword etc. I am trying to figure out how to make one and was wondering if there was any source code examples or tutorials out there. I am learning programming in C and C++ but I can follow most code pretty well so almost any language will do (I can always pick up a book and learn what I need to follow it). I have been working my way through the requisite Tic-Tac-Toe, Choose your own adventure, hangman etc. stuff and this seems like a good next step before I start moving on to graphics. [Edited by - Hakiko on March 4, 2005 5:25:44 AM]
Advertisement
why not try your own ascii adventure?
char *asciiman[] = " 0 " ,                   "/|\\" ,                   "/ \\";Draw(asciiman,10,5);class AsciiRenderer{    public:        AsciiRenderer();        ~AsciiRenderer();       // copies our ascii man to 'scene'       // skips spaces (so don't end up overwriting       // other things with spaces)       void Draw(const char *d[] , int x , int y);       // clears screen,       // and cout's scene array       void Flip();    private:       // edit X,Y to height,width of your console/DOS window       char scene[X][Y];    // stores what scene looks like before flip};

Personal Message me if you need help with this, if you decide you want to do this.
Which bit are you having trouble with? I have some basic code around if you'd like it, but it's just setting up the world and items. I'm working on it again so would be happy to talk it through with you. In terms of command parsing you could start looking at code such as Gaz Iqbal's command console tutorial for ideas.
This is the problem I am having:
I take as input char inputBuffer
I then want to iterate through inputBuffer till I get to a " " and set that equal to the first word in a sentance.

something like:

char inputBuffer[MAX_SIZE];
char currentChar;
char word1[MAX_SIZE];
int i = 0;

cin >> inputBuffer;

currentChar = inputBuffer;

do {
currentChar = inputBuffer;
word1 = inputBuffer;
} while(currentChar != " ");

I have tried doing it a few different ways and using strings instead of char. But I always wind up with a data type error, or garbage in word1. For example I would enter "look" but when I send word1 to cout I get "look &9 $$9" and other ASCII garbage like smiley faces.

I need to be able to parse through till the space then set that to word1. Then set whats left to word2.


Well, you forgot the ending \0 character. Printf doesn't know when to stop printing the word. That's why you get all those smileys :)

The way you should do it is something like that:

char inputBuffer[1024];char myWord[1024];int ib_pos;int w_pos;//read the user's inputcin >> inputBuffer;//start at the begginingib_pos = 0;//while I'm not at the end of the bufferwhile (inputBuffer[ib_pos] != 0){   //start a brand new word   w_pos = 0;   //if it's not space or end of buffer   while ((inputBuffer[ib_pos] != ' ')&&(inputBuffer[ib_pos] != 0))      //copy the char into the word      myWord[w_pos++] = inputBuffer[ib_pos++];   //now it's the trick: insert the string terminator   myWord[w_pos] = 0;   //I found something! I found something!   printf("Got word: '%s'\n", myWord);   //now jump over the spaces (the user might have typed more of them)   while ((inputBuffer[ib_pos] == ' ')&&(inputBuffer[ib_pos] != 0))      ib_pos++;}


I haven't tested this, but I hope it works.
Have FUN,FeeL E!
Look, i'm thinking about writing a 'how to make a text adventure tutorial' or something, but I lack the motivation. If you think you might need something like that, mail me (fili@mymail.ro) and maybe I'll start writing it.

Have FUN,
FeeL E!
Have FUN,FeeL E!
Thanks alot, I see what I was doing wrong.
Take a look at Brass Lantern for help with creating and playing adventure games.

You can also download games (many with source) and creation software from The Interactive Fiction Archive.
Thanks I'll check it out.
Quote:Original post by Hakiko
This is the problem I am having:
I take as input char inputBuffer
I then want to iterate through inputBuffer till I get to a " " and set that equal to the first word in a sentance.


Reading into a C++ std::string automatically splits on whitespace. (In C, using "%s" with scanf does the same.)

using namespace std;vector<string> words;string word;while (cin >> word) words.push_back(word);

This topic is closed to new replies.

Advertisement