Jump to content
  • Advertisement
Sign in to follow this  
Hakiko

Text Adventure

This topic is 4977 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.


Share this post


Link to post
Share on other sites
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 input
cin >> inputBuffer;

//start at the beggining
ib_pos = 0;

//while I'm not at the end of the buffer
while (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.

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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);

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!