RMRK is retiring.
Registration is disabled. The site will remain online, but eventually become a read-only archive. More information.

RMRK.net has nothing to do with Blockchains, Cryptocurrency or NFTs. We have been around since the early 2000s, but there is a new group using the RMRK name that deals with those things. We have nothing to do with them.
NFTs are a scam, and if somebody is trying to persuade you to buy or invest in crypto/blockchain/NFT content, please turn them down and save your money. See this video for more information.
[Resolved] Text Based Adventure Help

0 Members and 2 Guests are viewing this topic.

***
Rep:
Level 88
Well, I'm coding my first 'game'. I've run into an initial problem though.

When I input for "action", it jumps straight to the bottom, and tells me it's an invailid input.
I'm guessing that it's checking to see if it equals what I tell it to, and since nothing else can be changed unless the first one works, it fails completely. Why is the char variable not being changed?

Here be me code.
Code: [Select]
#include <iostream>
using namespace std;
int main ()
{

int axisx;
int axisy;
axisy = 0;
axisx= 0;
char direction [6];
char action [10];


cout << "####################################################";
cout << "\n";
cout << "#                                                  #";
cout << "\n";
cout << "#              =Text Based Adventure=              #";
cout << "\n";
cout << "#                                                  #";
cout << "\n";
cout << "####################################################";
cout << "\n";
cout << "\n";


{if (axisx == 0 && axisy == 0 )
{
          cout << "There's nothing around you. You can go north, and east.";
          cout << "What are you going to do?\nI'm going to ";
          cin >> action;
          if (action ==  "move" )
          {
                     cout << "Where would you like to move?";
                     cin >> direction;
                     }
                    
                     if (direction == "move" )
                     {axisy = axisy + 1;
                    
                     }
                     else if (direction == "east" )
                     axisx = axisx + 1;
                    
                     else
                     {cout << "Invalid input";
                     }
                     }
cin.get();
cin.get();
return 0;


}

« Last Edit: July 28, 2009, 06:28:29 PM by JFloyd »

********
Hungry
Rep:
Level 96
Mawbeast
2013 Best ArtistParticipant - GIAW 11Secret Santa 2013 ParticipantFor the great victory in the Breakfast War.2012 Best Game Creator (Non-RM Programs)~Bronze - GIAW 9Project of the Month winner for December 2009Project of the Month winner for August 20082011 Best Game Creator (Non RM)Gold - GIAW Halloween
the problem is you can't compare a string that way.

I could explain how to write a method to check the character array against a string, but it's so much easier to just #include <string> and make action a string object.

FCF3a A+ C- D H- M P+ R T W- Z- Sf RLCT a cmn+++ d++ e++ f h+++ iw+++ j+ p sf+
Follow my project: MBlok | Find me on: Bandcamp | Twitter | Patreon

***
Rep:
Level 88
Really NAM...you have to tell me I MUST use strings, the one thing I never learned to use xD
You're a horrible person!

*
A Random Custom Title
Rep:
Level 96
wah
Really NAM...you have to tell me I MUST use strings, the one thing I never learned to use xD
You're a horrible person!
... Then learn to use it?

***
Rep:
Level 88
Really NAM...you have to tell me I MUST use strings, the one thing I never learned to use xD
You're a horrible person!
... Then learn to use it?
Never did, because I couldn't understand them. xP
Anyways, I'll mark this as resolved.

********
Hungry
Rep:
Level 96
Mawbeast
2013 Best ArtistParticipant - GIAW 11Secret Santa 2013 ParticipantFor the great victory in the Breakfast War.2012 Best Game Creator (Non-RM Programs)~Bronze - GIAW 9Project of the Month winner for December 2009Project of the Month winner for August 20082011 Best Game Creator (Non RM)Gold - GIAW Halloween
strings are much simpler than trying to use character arrays and pointers for them.  most modern programs don't use character arrays anymore.  I hate to break it to you, but strings are something you'll have to learn.
  
Also, I forgot to mention that you'll want to look into the methods given to you by the string class.  You still can't do an == but there is a method to compare strings.  I'll update this later with some info for you.

also, I never said you MUST, you could find a way to keep using char arrays if you want, I'm just giving you the easy solution.

FCF3a A+ C- D H- M P+ R T W- Z- Sf RLCT a cmn+++ d++ e++ f h+++ iw+++ j+ p sf+
Follow my project: MBlok | Find me on: Bandcamp | Twitter | Patreon

*
Resident Cloud
Rep:
Level 91
God that seems strict.

********
Hungry
Rep:
Level 96
Mawbeast
2013 Best ArtistParticipant - GIAW 11Secret Santa 2013 ParticipantFor the great victory in the Breakfast War.2012 Best Game Creator (Non-RM Programs)~Bronze - GIAW 9Project of the Month winner for December 2009Project of the Month winner for August 20082011 Best Game Creator (Non RM)Gold - GIAW Halloween

FCF3a A+ C- D H- M P+ R T W- Z- Sf RLCT a cmn+++ d++ e++ f h+++ iw+++ j+ p sf+
Follow my project: MBlok | Find me on: Bandcamp | Twitter | Patreon

*
A Random Custom Title
Rep:
Level 96
wah
Comparing strings are easy (I was bored so I was looking through C++ stuff) :D

And, if you wanted, you could list your actions 1 to i and allow the player to choose. Although that doesn't seem as free form as just writing an action and may be more jumbled, it's an option if you don't want to learn strings.


If you plan to learn strings: (I found a place to learn so I'll link you to it :p)
http://www.cplusplus.com/doc/tutorial/variables/
Search for "Introduction to strings"
http://www.cplusplus.com/doc/tutorial/basic_io/
Search for "cin and strings"

And if you know how to work with if statements and stuff, it should be a breeze.

***
Rep:
Level 88
Lol, thanks moo. But I have cplusplus on my speed dial...
I've read through most of the tutorial, and skipped around as needed. I didn't know they covered that in there.

********
Hungry
Rep:
Level 96
Mawbeast
2013 Best ArtistParticipant - GIAW 11Secret Santa 2013 ParticipantFor the great victory in the Breakfast War.2012 Best Game Creator (Non-RM Programs)~Bronze - GIAW 9Project of the Month winner for December 2009Project of the Month winner for August 20082011 Best Game Creator (Non RM)Gold - GIAW Halloween
Lol, thanks moo. But I have cplusplus on my speed dial...
I've read through most of the tutorial, and skipped around as needed. I didn't know they covered that in there.

that might be why.
in any case, good luck with your game.

FCF3a A+ C- D H- M P+ R T W- Z- Sf RLCT a cmn+++ d++ e++ f h+++ iw+++ j+ p sf+
Follow my project: MBlok | Find me on: Bandcamp | Twitter | Patreon

*
Resident Cloud
Rep:
Level 91
C++ seems strict in terms of how you do things. Lua which i use most of the time these days is so laid back and doesnt even have strict variable typing theres none of this:
Code: [Select]
int axisx;
int axisy;
axisy = 0;
axisx= 0;
char direction [6];
char action [10];

***
Rep:
Level 88
C++ seems strict in terms of how you do things. Lua which i use most of the time these days is so laid back and doesnt even have strict variable typing theres none of this:
Code: [Select]
int axisx;
int axisy;
axisy = 0;
axisx= 0;
char direction [6];
char action [10];


Yeah it's pretty strict.

Oh, and NAM. I've run into a problem.
When I use || to have mupltiple options, I can type anything and it does one of my many options. It's not random though....
Here's my code currently ((My friend told my I should try to swtich it up to an RPG ))

Code: [Select]
#include <iostream>
#include <string>
using namespace std;

int main ()
{
   
string action_string;
string deci_string;
string shop_string;
    int hp;
    int maxhp;
    int mhp;
    int att;
    int matt;
    int currexp;
    int expg;
    int lv;
    int z;
    int gold;
    int potion;
    int herb;
    int weapon;
    potion = 0;
    gold = 25;
    z = 0;
     maxhp = 100;
     hp = 100;
     mhp = mhp + 3 + matt * 2;
     att = att + lv * 5;
     matt = matt + mhp / 3;
     currexp = currexp + expg;
     lv = 1;
     
     cout << "+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+";
     cout << "\n";
     cout << "+                          Dragon Hunter                        +";
     cout << "\n";
      cout << "+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+";
      cout << "\n\n\n\n";
      cout << "Controls:\n-Type the commands on the side. Case is imporatant.";
      cout << "\n-You gain a level when your experience is full. Level ups give \n you more health, and attack power. \n-Gold is used to by new equipment and sleep at the inn.";
      cout << "\nType OK to continue.";
      cout << "\n";
      z = z + 1;
     
     
      while (z = 1)
      {
      cin >> deci_string;
      if (deci_string == "OK")
      {
       cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nYour adventure starts in the town of Ravenhome. \nYou're a young man, whos destiny is woven into the fabric of time.\n";

       if (z = 2)
      {
         int Ravenhome;
          cout << "+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+" << endl;
    cout << " " << endl;
    cout << "You are in the town square of Ravenhome. Here are your options: " << endl;
    cout << "Hunt - Traverse the fields, to find monsters." << endl;
    cout << "Shop - Enter the market to buy equipment." << endl;
    cout << "Travel - Travel to a different town." << endl;
    cout << "Stats - Check statistics." << endl;
    cout << " " << endl;
    cout << "+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+" << endl;
    cout << " " << endl;
    cout << "Which do you choose? \n";
    cin >> action_string;
   
    if (action_string == "Stats"){
                      cout << "You're Health is ";
                      cout << hp;
                      cout << " out of ";
                      cout << maxhp;
                      cout << ".\n";
                      cout << "You have ";
                      cout << gold;
                      cout << " Gold.\n";
                      cout << "You're inventory contains:\n";
                      cout << potion;
                      cout << " Potions";
                      cout << z;
}


if (action_string == "Shop")
    { cout << "Here is a list of items you may purchase\n\n";
    cout << "     Items        \n" ;
    cout << "------------------\n" ;
    cout << "-Potion, Ten Gold.\n\n" ;
    cout << "      Weapons     \n" ;
    cout << "------------------\n" ;
    cout << "-Dagger, Twenty Five Gold\n" ;
    cout << "-Baselard, One Hundred Fifty Gold\n"; 
    cin >> shop_string;
   
   
if (shop_string == "Potion") {
   potion = potion + 1;
   gold = gold - 10;
   
}
if (shop_string == "Dagger") {
   if (lv >= 2)    {
                   
    weapon = 1;
    gold = gold - 25;
}
    else {
        cout << "You need to be a higher level\n" ;
        }
       
if (shop_string == "Baselard") {
   if (lv >= 4)    {
                   
    weapon = 2;
    gold = gold - 150;
}
    else {
        cout << "You need to be a higher level\n" ;
     
     
}
}
}
}
}
}


else {
         cout << "Unknown Command: ";
         cout << action_string;
         cout << "\nTry again.\n";
         
         cin >> action_string;
         }
}
    cin.get();
    return 0;

}

The code near the bottom for the shop is what I'm having the most problems with. Mostly the "Baselard" section. It just doesn't work. Also, I need a massive failsafe, so if the wrong thing is put in, it goes to the last section you were in to re-input. I tried doing that with the int z, but my friend said I could use labels. I have no idea how to use labels.

*
A Random Custom Title
Rep:
Level 96
wah
Code: [Select]
    if (action_string == "Stats"){
                      cout << "You're Health is " << hp << " out of " << maxhp; << ".\n";
                      cout << "You have " << gold << " Gold.\n";
                      cout << "You're inventory contains:\n";
                      cout << potion << " Potions";
                      cout << z;
}
For efficiency's and readability's sake.

Also, you should check if the person has enough gold to begin with, too. And it's hard to check things because, at least in here, the spacing is done pretty badly, and I can't see which brace ends which in the last with 6 consecutive brackets.

***
Rep:
Level 88
Yeah, I know I space things weird :P

Also, thats the problem with if statements, you need a metric crap ton of braces, and I know that the Shop needs to be revised.

********
Hungry
Rep:
Level 96
Mawbeast
2013 Best ArtistParticipant - GIAW 11Secret Santa 2013 ParticipantFor the great victory in the Breakfast War.2012 Best Game Creator (Non-RM Programs)~Bronze - GIAW 9Project of the Month winner for December 2009Project of the Month winner for August 20082011 Best Game Creator (Non RM)Gold - GIAW Halloween
I told you, you can't use == to compare strings, there's a method to do it.
you should also be using cin.getline rather than cin >> for strings, it's cleaner, and you won't get as many bugs doing it that way.

FCF3a A+ C- D H- M P+ R T W- Z- Sf RLCT a cmn+++ d++ e++ f h+++ iw+++ j+ p sf+
Follow my project: MBlok | Find me on: Bandcamp | Twitter | Patreon

***
Rep:
Level 88
I told you, you can't use == to compare strings, there's a method to do it.
you should also be using cin.getline rather than cin >> for strings, it's cleaner, and you won't get as many bugs doing it that way.

Lol, well thanks for telling me that now. :P


*
A Random Custom Title
Rep:
Level 96
wah
I told you, you can't use == to compare strings, there's a method to do it.
you should also be using cin.getline rather than cin >> for strings, it's cleaner, and you won't get as many bugs doing it that way.

Lol, well thanks for telling me that now. :P


Thanks for actually looking through the tutorials I linked you to. It's actually in there and it shows how to properly compare strings.

***
Rep:
Level 88
I did read them. :3
Also, I read the thing about using cin.getline, and it doesn't make sense what so ever. I attempted to re-write my code with it, and, I'm still finding my self having to use an if statement. You told me not to use ==. I don't know of anything else to compare anything with though. == works just fine with one string, it just gets a little screwy if you put || between them in the conditional. I'm guessing my question wasn't stated properly in the beggining.
"How do I make it so, the conditional has to match either the first case, or the second case, EXACTLY, or it goes to the else. When you put a || between it, if anything is put into the string, it chooses the closest one, and uses that. I want exact matches."

*
A Random Custom Title
Rep:
Level 96
wah
getline (cin,{name of string variable to store into});
EZ-PZ

Make sure you haven't been dumb enough to forget "#include <string>" at the beginning, too.

Also, you mean it has to be one or the other spelled exactly? It can't be both or neither or else it gets sent to the "else" function?

Also, at the website I was at, searching for 3 seconds got me this:
http://www.cplusplus.com/reference/string/string/compare/

********
Hungry
Rep:
Level 96
Mawbeast
2013 Best ArtistParticipant - GIAW 11Secret Santa 2013 ParticipantFor the great victory in the Breakfast War.2012 Best Game Creator (Non-RM Programs)~Bronze - GIAW 9Project of the Month winner for December 2009Project of the Month winner for August 20082011 Best Game Creator (Non RM)Gold - GIAW Halloween
I did read them. :3
Also, I read the thing about using cin.getline, and it doesn't make sense what so ever. I attempted to re-write my code with it, and, I'm still finding my self having to use an if statement. You told me not to use ==. I don't know of anything else to compare anything with though. == works just fine with one string, it just gets a little screwy if you put || between them in the conditional. I'm guessing my question wasn't stated properly in the beggining.
"How do I make it so, the conditional has to match either the first case, or the second case, EXACTLY, or it goes to the else. When you put a || between it, if anything is put into the string, it chooses the closest one, and uses that. I want exact matches."

that's exactly why you can't use == to compare strings.  it doesn't work.  if statements have nothing to do with the way you gather input, the getline method just makes your input cleaner and less likely to bug.

http://www.cplusplus.com/reference/string/string/compare/

there you go.

Also you're grammer iz herrendus.

FCF3a A+ C- D H- M P+ R T W- Z- Sf RLCT a cmn+++ d++ e++ f h+++ iw+++ j+ p sf+
Follow my project: MBlok | Find me on: Bandcamp | Twitter | Patreon

***
Rep:
Level 88
OK, to me, that just looks like people are being too lazy to say "A green apple is not a red apple.".
I'm seeing no way to compare that if the input of something is exactly equal to whats in the condition, it does as is in the If Statement.

********
Hungry
Rep:
Level 96
Mawbeast
2013 Best ArtistParticipant - GIAW 11Secret Santa 2013 ParticipantFor the great victory in the Breakfast War.2012 Best Game Creator (Non-RM Programs)~Bronze - GIAW 9Project of the Month winner for December 2009Project of the Month winner for August 20082011 Best Game Creator (Non RM)Gold - GIAW Halloween
OK, to me, that just looks like people are being too lazy to say "A green apple is not a red apple.".
I'm seeing no way to compare that if the input of something is exactly equal to whats in the condition, it does as is in the If Statement.


ok, I'll spell it out for you, since it doesn't appear that you're reading anything.

Code: [Select]
if (str1.compare(str2) != 0)
    // the strings are not the same
else if (str1.compare(str2) == 0)
    // the strings are the same

I can't tell you more than that without writing your program for you.  Take the info I posted above, and figure out how to apply it for your game.  And dont' forget to #include <string> or you'll get errors.

the link that moo posted explains it perfectly.  they aren't being "too lazy", they're making the program compare the input, as to teach how to use string.compare() rather than hardcoding everything, as that wouldn't teach anything.
« Last Edit: August 06, 2009, 06:51:19 PM by NAMKCOR »

FCF3a A+ C- D H- M P+ R T W- Z- Sf RLCT a cmn+++ d++ e++ f h+++ iw+++ j+ p sf+
Follow my project: MBlok | Find me on: Bandcamp | Twitter | Patreon

*
Rep:
Level 98
2010 Best Veteran2014 Best Use of Avatar and Signature Space2014 King of RMRK2014 Favorite Staff Member2014 Best Counsel2014 Best Writer2014 Most Mature Member2014 Best IRC Chatterbox2013 Favorite Staff MemberSecret Santa 2013 ParticipantFor the great victory in the Breakfast War.Secret Santa 2012 Participant2011 Best Writer2011 Best Counsel2010 Funniest Member2010 Best Writer
I have nothing to contribute to coding help,

but you could always use ADRIFT.
you awoke in a burning paperhouse
from the infinite fields of dreamless sleep

*
A Random Custom Title
Rep:
Level 96
wah
they aren't being "too lazy", they're making the program compare the input, as to teach how to use string.compare() rather than hardcoding everything, as that wouldn't teach anything.
That's why you could put in different strings into str1 and str2 (where the strings might or might not be the same) and the code would work almost the same.