The RPG Maker Resource Kit

RMRK General => General Chat => Topic started by: JFloyd on July 28, 2009, 03:11:45 AM

Title: [Resolved] Text Based Adventure Help
Post by: JFloyd on July 28, 2009, 03:11:45 AM
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;


}

Title: Re: Text Based Adventure Help
Post by: tSwitch on July 28, 2009, 03:27:38 AM
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.
Title: Re: Text Based Adventure Help
Post by: JFloyd on July 28, 2009, 04:57:23 AM
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!
Title: Re: Text Based Adventure Help
Post by: Kokowam on July 28, 2009, 12:30:05 PM
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?
Title: Re: Text Based Adventure Help
Post by: JFloyd on July 28, 2009, 06:28:06 PM
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.
Title: Re: [Resolved] Text Based Adventure Help
Post by: tSwitch on July 28, 2009, 08:12:32 PM
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.
Title: Re: [Resolved] Text Based Adventure Help
Post by: ahref on July 29, 2009, 03:53:07 PM
God that seems strict.
Title: Re: [Resolved] Text Based Adventure Help
Post by: tSwitch on July 29, 2009, 06:31:43 PM
God that seems strict.

what?
Title: Re: [Resolved] Text Based Adventure Help
Post by: Kokowam on July 29, 2009, 10:10:10 PM
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.
Title: Re: [Resolved] Text Based Adventure Help
Post by: JFloyd on July 29, 2009, 11:06:13 PM
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.
Title: Re: [Resolved] Text Based Adventure Help
Post by: tSwitch on July 29, 2009, 11:33:51 PM
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.
Title: Re: [Resolved] Text Based Adventure Help
Post by: ahref on July 30, 2009, 12:19:02 PM
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];
Title: Re: [New Problem] Text Based Adventure Help
Post by: JFloyd on July 30, 2009, 07:36:04 PM
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.
Title: Re: [Resolved] Text Based Adventure Help
Post by: Kokowam on July 30, 2009, 07:54:22 PM
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.
Title: Re: [Resolved] Text Based Adventure Help
Post by: JFloyd on July 30, 2009, 08:42:56 PM
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.
Title: Re: [Resolved] Text Based Adventure Help
Post by: tSwitch on July 31, 2009, 07:18:08 PM
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.
Title: Re: [Resolved] Text Based Adventure Help
Post by: JFloyd on August 01, 2009, 01:47:17 AM
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

Title: Re: [Resolved] Text Based Adventure Help
Post by: Kokowam on August 01, 2009, 02:37:32 AM
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.
Title: Re: [Resolved] Text Based Adventure Help
Post by: JFloyd on August 01, 2009, 03:31:24 AM
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."
Title: Re: [Resolved] Text Based Adventure Help
Post by: Kokowam on August 01, 2009, 02:58:15 PM
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/
Title: Re: [Resolved] Text Based Adventure Help
Post by: tSwitch on August 01, 2009, 03:25:03 PM
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.
Title: Re: [Resolved] Text Based Adventure Help
Post by: JFloyd on August 01, 2009, 08:09:17 PM
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.
Title: Re: [Resolved] Text Based Adventure Help
Post by: tSwitch on August 06, 2009, 06:48:30 PM
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.
Title: Re: [Resolved] Text Based Adventure Help
Post by: Sophist on August 06, 2009, 08:38:18 PM
I have nothing to contribute to coding help,

but you could always use ADRIFT.
Title: Re: [Resolved] Text Based Adventure Help
Post by: Kokowam on August 06, 2009, 09:26:09 PM
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.
Title: Re: [Resolved] Text Based Adventure Help
Post by: tSwitch on August 09, 2009, 04:52:20 AM
ALSO: You need to get in a habit of organizing your code better. If you are learning C++ from a tutorial look at how the examples have their text organized and use that style in your own code. Also try and break up the code inside of your main function into separate functions just so this is all a bit easier to manage.

no need to use functions just yet, he needs a better understanding of the basics before moving on to that.
Title: Re: [Resolved] Text Based Adventure Help
Post by: JFloyd on August 09, 2009, 05:30:41 AM
Note: I really haven't read any tutorials. Most of this was told to my by ear, and I've figured it out by myself through trial and error.

Also, I'm probably going to take a break from coding until school starts. I'm sure I'll fill many a notebook with hand written codes. So, if someone want's to remove this topic, I'm fine with it.
Title: Re: [Resolved] Text Based Adventure Help
Post by: Kokowam on August 09, 2009, 12:15:32 PM
Note: I really haven't read any tutorials. Most of this was told to my by ear, and I've figured it out by myself through trial and error.
Trying to work from scratch is cool and all, but it's completely retarded. Coding is based on a library that someone else made. Go for tutorials. Whoever made them 99% has a good grasp on the language and has lots of things to teach. If you're working completely alone, you may be ignorant to a certain built-in function or style or coding that could be much more efficient.