Simple text adventure game...

Started by
6 comments, last by Fuzztrek 22 years, 1 month ago
Hey all, I''ve been learning C, and I decided to make an extremely simple text adventure game. Basically, you can create a character, specify the characters gender, name, and age. Then you go on your journey to retrive a golden bracelet. (yes yes.. cheesey storyline. i was bored.) Anyway, so far, I''ve just being doing simple question/answer situations like so: You find an unusual stone. You: a)pick it up and chuck it b)pick it up and examin it c)ignore it and keep going etc etc. very basic, i''m sure you''ve all played these type of games or read "choose your own adventure" stories. anyway, I was wondering if theres any sort of random-event thingy.. like, right now, I''m coding as you play, kinda. eg: puts("A upright-walking wolf approches you. You decide to:"); puts("a) Run"); puts("b) Talk to the wolf"); choice = getchar(); if(choice == ''a'' || choice == ''A''){ blah blah; }else ... Is there any other way to do this? I don''t have THAT much of a problem with it, just that it looks kinda funky and unorganized when i type it. And then of course, if i want one choice to lead to a completely different storyline, i''d have to do double the code, which would make it even more messy. Does anyone have any solutions? things that i already know and that don''t have to be yelled at me: i''m a newbie i probably don''t know what i''m doing i don''t know the "standards" to how even the simpilest games are created (ie, standard algorithms, etc) yes.. so! thanks in advance =) Fuzztrek ¬_¬
Advertisement
Use Functions. Lots of 'em!

    void askQuestions(int questionNum){     switch(questionNum)     {          case 1: //Question one code               // you could also call yet another function...               puts(" Yada yada yada ");               choice = getchar();               if ( so and so == this and that )               {                    doSomeStuff();               else               ...         ...     ......  


If you well document your code, and put tabs were needed, it should look neat.

Remember: Whitespaces are your friend.

Edited by - Programmer One on March 8, 2002 10:08:33 PM
Ah. okay. i will try to use more functions. actually, i''m thinking of rewriting my code, so i''ll make sure to use more functions in the second version.

Thanks!

¬_¬
what you can do is put a thing called randomize(); in your void main...

so you will have will have something like
void main()
{
randomize();
}

make it like the first thing... then later in your program put something like x=random(6)+1;, that includes 0 so thats why you have to put in the 1... so it will give you a random 1-6 for your x... then just put in something like
x=random(6)+1;
if (x==1)
{
cout<}
if(x==2)
{
cout<}

oh and at the top i think you might have to put in #include <iomanip.h> just put that below the #include <iostream.h>
thx.. but actually i''m just using C for now.. but i''ll try and find a random function in C (if its different from C++)

fuzztrek

¬_¬
// to "seed" the random number generator// (so you dont always get the same sequence)#include <time.h>srand((unsigned int)time(NULL));// to get a random number between LO and HI:int DaInt = ((rand() / (float)RAND_MAX) * (HI - LO)) + LO; 

that''s the simplist way to get random numbers in c++, i think...

also, as far as "double coding" stuff because the storyline branches and then comes back to some same point, you can store variables that track this stuff, and use functions as someone above said... for example, say there is a place where you can get an unusual stone. if the player picks it up, set a flag to true, otherwise leave it as false. then, if at a later point you need to know if the player has the stone, just check that variable. most likely you will want an array of these flags (for many various things thaty the player may or may not do), or an even more complicated method of tracking the game state... but that is the basic idea behind it anyways.
HTH!
--- krez ([email="krez_AT_optonline_DOT_net"]krez_AT_optonline_DOT_net[/email])
thanks!

¬_¬
quote:Original post by krez
// to get a random number between LO and HI:int DaInt = ((rand() / (float)RAND_MAX) * (HI - LO)) + LO;  

that''s the simplist way to get random numbers in c++, i think...

#define random(High,Low) (((rand() / (float)RAND_MAX) * (High - Low)) + Low) 

OR (to make it easier to read)
#define random(High,Low) (rand() / (float)RAND_MAX * (High - Low) + Low) 

Order of operations: it will still do the same thing

"I''ve learned something today: It doesn''t matter if you''re white, or if you''re black...the only color that REALLY matters is green"
-Peter Griffin
"I've learned something today: It doesn't matter if you're white, or if you're black...the only color that really matters is green"-Peter Griffin

This topic is closed to new replies.

Advertisement