Advertisement

Text RPG Actions (updated)

Started by May 21, 2002 03:44 PM
6 comments, last by Ragadast 22 years, 7 months ago
Ok. I'm done with the reading the room info part of the game. Now I need to understand how to read the user input word by word, so I can make the player go north, look, put something inside a bag, examine something, etc. Again, thanks to all who helped me before. [edited by - Ragadast on May 21, 2002 9:52:08 PM]
From your previous post, it sounds like this is running as a console app?

If so, then the easiest way of actually getting the data in is to use cin - it''s a bit klunky, but is not too hard to use. Something like this:

char cData[255];
cout << "Enter your command: ";
cin >> cData;

(play about with it a bit if you want to allocate dynamic char* data using new and delete)

Your more pressing concern is what to do with the data once it''s input. You need to consider building a simple parser next - chop the input up into words (scan for spaces), and match the words up. Traditional text games have always followed verb-noun type input (go north, wield sword etc.) - start simple like that and build it up. Write down a list of verbs you think you need to support and go from there (a big switch statement could match up your verbs - again, it''s not an advanced parser if you do it this way, but it''ll get you up and running).
Advertisement
How do you check for a blank space in a char[]?
quote: Original post by Ragadast
How do you check for a blank space in a char[]?


The same you way you'd check for any other character:

if(mychar == ' '){	//...}  


[edited by - SilentCoder on May 21, 2002 6:07:17 PM]
That is really simple.. A space has ascii value 32.. just cycle trough your string in a loop and check for 32.. or use strstr() library function..
example:

char string[255] // string containing words

int index;
for( index = 0; index < strlen(string); index++)
if( string[index] == 32 )
{
// found space, do something here (copy word)
}

nice is:
int index = 0;
while( string[index] != 0 )
{
if( string[index] == 32 )
{
// found space, do something here (copy word)
}
index++;
}

this works because a c_string (array of chars) is terminated by a zero :-)
quote: Original post by Ragadast
How do you check for a blank space in a char[]?

Which language are you using?

[ C++ FAQ Lite | ACCU | Boost | Python | Agile Manifesto! ]
Advertisement
I''m using BC++. I got an odd problem on loading info from text files. I do this:

text file:
blah blah blah blah
blah blah blah
2
0
0
3
1
0
0
0
0

The first 2 lines correspond to the name and the description of the room. The 4 next lines corresponde to the exits, and the rest correspond to the items on the room. I do this to load it:

ifstream infile("1.rom");
infile.getline(name, 50); //Name of the room
infile.getline(desc, 255); //Description of the room
infile >> room.exits[0]; //Room link to north exit
infile >> room.exits[1]; //Room link to south exit
infile >> room.exits[2]; //Room link to east exit
infile >> room.exits[3]; //Room link to west exit
infile >> room.objects[0]; //First object
infile >> room.objects[1]; //etc...
infile >> room.objects[2];
infile >> room.objects[3];
infile >> room.objects[4];
infile.close()

Well, when i print out the info i got from loading my text file It confuses 2 variables: room.exits[3] and room.objects[0]. room.exits[3] gets the value of room.objects[0], making mistakes on my game. I dont get what happened Besides, It''s not recognizing '' '' as a blank char or 32 as a blank char too.
Let me point you to the rudimentary interpreter I had posted at some point : there

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement