Thread: Level-Up

  1. #1
    Registered Loser nickodonnell's Avatar
    Join Date
    Sep 2005
    Location
    United States
    Posts
    33

    Level-Up

    Alright, in order to make myself better with C++ I'm developing the basics for a text-based RPG (which if/when I get good enough, I'll probably use it to convert to a Windows app when I learn the language better).

    First of all, thanks for all the help so far with the monster thing (another thread.)

    Now, I've got something a little different. I better just show you the codes...

    Code:
    #include <iostream>
    #include <string>
    #include "levels.h"
    
    using namespace std;
    
    //global variables
    int char_level=1;
    int char_exp =0;
    int classcount=0;
    string char_class;
    string char_name;
    //global variables
    
    void char_selection();
    
    int main()
    {
        if (char_exp<28)
        {
                        char_level==1;
        }
        else if ((char_exp<52) && (char_level==1))
        {
                        char_level++;
        }
        else if ((char_exp<165) && (char_level==2))
        {
             char_level++;
        }
        
        cout<<"*Warrior*\n";
        cout<<"Warriors are strong, fierce.  They focus on physical fighting.\n\n";
        cout<<"*Mage*\n";
        cout<<"Mages are wise, powerful.  They use magic to win.\n\n";
        cout<<"*Rogue*\n";
        cout<<"Rogues are quick, witty.  They are more balanced.\n\n";
    
        level character;
    
        while (classcount<1)
        {
              cout<<"Choose your class --> ";
              cin>>char_class;
              cin.ignore();
              if ((char_class=="Warrior") || (char_class=="warrior"))
              {
                                          classcount++;
                switch(char_level)
                {
                             case 1:
                                  character.strength(7);
                                  character.intelligence(1);
                                  character.defense(6);
                                  character.health(130);
                                  character.mana(4);
                                  break;
                             case 2:
                                  character.strength(10);
                                  character.intelligence(3);
                                  character.defense(9);
                                  character.health(150);
                                  character.mana(7);
                                  break;
                             case 3:
                                  character.strength(14);
                                  character.intelligence(5);
                                  character.defense(11);
                                  character.health(165);
                                  character.mana(10);
                                  break;
                }
              }
              else if ((char_class=="Mage") || (char_class=="mage"))
              {
                                       classcount++;
                switch(char_level)
                {
                                          case 1:                
                                               character.strength(2);
                                               character.intelligence(20);
                                               character.defense(2);
                                               character.health(100);
                                               character.mana(30);
                                               break;
                                          case 2:
                                                character.strength(3);
                                                character.intelligence(25);
                                                character.defense(4);
                                                character.health(125);
                                                character.mana(40);
                                                break;
                                          case 3:
                                                character.strength(6);
                                                character.intelligence(32);
                                                character.defense(7);
                                                character.health(155);
                                                character.mana(55);
                                                break;
                }
              }
              else if ((char_class=="Rogue") || (char_class=="rogue"))
              {
                                        classcount++;
                switch(char_level)
                {          
                           case 1:      
                               character.strength(5);
                               character.intelligence(8);
                               character.defense(5);
                               character.health(200);
                               character.mana(6);
                               break;
                           case 2:
                               character.strength(7);
                               character.intelligence(11);
                               character.defense(8);
                               character.health(220);
                               character.mana(10);
                               break;
                            case 3:
                               character.strength(10);
                               character.intelligence(16);
                               character.defense(10);
                               character.health(235);
                               character.mana(14);
                               break;
                  }
                }
                else
                {
                    cout<<"\nThat is not a valid class.\n\n";
                }
        }
        cout<<"Okay, now what is your name?\n";
        cout<<"Name: ";
        cin>>char_name;
        cin.ignore();
        
        cout<<"\nName: "<<char_name<<endl;
        cout<<"Strength: "<<character.displaystrength()<<endl;
        cout<<"Intelligence: "<<character.displayintelligence()<<endl;
        cout<<"Defense: "<<character.displaydefense()<<endl;
        cout<<"Health: "<<character.displayhealth()<<endl;
        cout<<"Mana: "<<character.displaymana()<<endl;
        cout<<"Exp: "<<char_exp<<endl;
        
        cout<<"\n\n\n\tReady to level up?\n\n";
        char_exp=char_exp+30;;
        
        cout<<"\nName: "<<char_name<<endl;
        cout<<"Strength: "<<character.displaystrength()<<endl;
        cout<<"Intelligence: "<<character.displayintelligence()<<endl;
        cout<<"Defense: "<<character.displaydefense()<<endl;
        cout<<"Health: "<<character.displayhealth()<<endl;
        cout<<"Mana: "<<character.displaymana()<<endl;
        cout<<"Exp: "<<char_exp<<endl;
        
        cin.get();
    }
    Code:
    class level
    {
          public:
                 level();
                 ~level();
                 
                 void strength(int str);
                 int displaystrength();
                 void intelligence(int intel);
                 int displayintelligence();
                 void defense(int def);
                 int displaydefense();
                 void health(int hea);
                 int displayhealth();
                 void mana(int man);
                 int displaymana();
                 
          protected:
                    int power;
                    int magic;
                    int vitality;
                    int hitpoints;
                    int magicpoints;
    };
    
    level::level() //construct
    {}
    level::~level() //destruct
    {}
    void level::strength(int str)
    {
         power=str;
    }
    int level::displaystrength()
    {
         return power;
    }
    void level::intelligence(int intel)
    {
       magic=intel;
    }
    int level::displayintelligence()
    {
        return magic;
    }
    void level::defense(int def)
    {
         vitality=def;
    }
    int level::displaydefense()
    {
        return vitality;
    }
    void level::health(int hea)
    {
         hitpoints=hea;
    }
    int level::displayhealth()
    {
        return hitpoints;
    }
    void level::mana(int man)
    {
         magicpoints=man;
    }
    int level::displaymana()
    {
        return magicpoints;
    }
    If you compile that, you'll see that you don't actually level up... and it's because I'm already out of the while loop. I need to simplify the code, maybe put the whole class and name selection on another .h file, and get the leveling up right. Any help?

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    In your class you should make a levelup member function. It'll keep your code cleaner and help isolate the problem.

  3. #3
    Registered Loser nickodonnell's Avatar
    Join Date
    Sep 2005
    Location
    United States
    Posts
    33
    Any tips on how I do that? I've only been programming for three days, so I'm not exactly familliar with the language yet. Also, eager to learn!

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    I would definatly do a lot more reading before trying to get to far into game programming. When done right inheritance can make this stuff a lot easier.

    Since you are new let me give you a big tip:
    Don't use global variables. Yes they make things seem easier but its a bad practice to get into and it will come back to bite you.

    When you are defining your classes try to keep a concept in mind: The object should know everything about itself. So you can have 1 hero object that knows what level it is, what class it is, what attributes it has, etc. Then all you have to do is call the levelup() member function which would be something like:
    Code:
    void heroes::levelup() {
      switch ( class ) // class would be a private member variable
      {
        case 0:  // warrior
          power += A;
          magic += B;
          vitality += C;
          hitpoints += D;
          magicpoints += E;
          break;
        case 1: // mage and repeat the above stuff
        case 2: // rogue and repeat
      }

  5. #5
    Registered Loser nickodonnell's Avatar
    Join Date
    Sep 2005
    Location
    United States
    Posts
    33
    Thanks, yeah, I'm really getting into classes and inheritance. Thanks for the tips.

    Edit: Just so I understand a little better...

    Code:
    class Stats
    {
          public:
                 //stuff
          protected:
                    //stuff
          private:
                  //stuff
    }
    
    class Hero : public Stats
    {
          public:
                 
          protected:
                    
          private:
                  
    }
    ...in the Stats class I would define strength, defense, etc., and in the hero subclass I would define level_up() as well as the character's class attributes? Let's say I save this as stats.h ; how would the player choose his/her character's class?

    Oh, and I'm not really making a game, I'm doing this because hands-on actually coding helps me udnerstand a ton more than just reading.
    Last edited by nickodonnell; 10-01-2005 at 01:11 PM.

  6. #6
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    You forgot the little ';' at the end of your class defs

    Code:
    class Stats
    {
    	public:
    
    	protected:
    
    	private:
    
    };
    Yes it sounds like a reasonable idea to me. For example:

    Code:
    class Stats
    {
    	public:
    		Stats () {}
    		~Stats () {}
      
    	protected:
    		unsigned int hp;
    		unsigned int mp;
    		unsigned int str;
    		unsigned int def;
    		unsigned int atk;
    		// etc...
    };
    
    class Hero :public Stats
    {
    	public:
    		Hero () {}
    		~Hero () {}
    
    		void LevelUp ();
    	private:
    		// hero class info
    		// not too sure what that'd be though :)
    };
    Yep you probably knew that, just seeing how much of C++ I remember
    Last edited by cboard_member; 10-01-2005 at 02:12 PM.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  7. #7
    Registered Loser nickodonnell's Avatar
    Join Date
    Sep 2005
    Location
    United States
    Posts
    33
    OK sounds good. I know it may be overdoing it a little, but is there anyway I could do a subclass of class Hero for each character class? (hehe, confusing)

    Like this:
    Code:
    class Stats:
     class Hero
       class Warrior
       class Rogue
       class Wizzard
     class Monster
       ...
    Could I still do that, and make the level_up() function work with each character class? And if I did that, there would be no need for the whole warrior.strength(x) thing; I could just store the values in unsigned variables (like ahluka had), making it alot easier to change them.

  8. #8
    mov.w #$1337,D0 Jeremy G's Avatar
    Join Date
    Nov 2001
    Posts
    704
    Code:
    class person:
         class Heros:
              class Warrior
              class Rogue
              class Wizzard
         class Monster:
              class orc
              class goblin
              ....
    Classes with : should be abstract interfaces, that way you dont have to develop code in your game area for handling specific types of classes.

    So your class person should have virtual functions like levelup, getmeleebonus, getmagicbonus, getracialbonus, getspecialpower etc. Then in your classes at the end of the line, is where you actually give the functions code. The code will be specific to the type of class it is. For instance Wizzards wouldnt get melee bonuses, so you might just define the
    Code:
    function Wizzard::getmeleebonus() { return 0; }
    etc.
    c++->visualc++->directx->opengl->c++;
    (it should be realized my posts are all in a light hearted manner. And should not be taken offense to.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Monitoring soundcard line-out level
    By moose in forum C Programming
    Replies: 4
    Last Post: 04-10-2009, 05:46 PM
  2. Diablo's random level generation
    By glo in forum Game Programming
    Replies: 7
    Last Post: 07-19-2008, 03:04 AM
  3. Listing binary tree numbers by level
    By Nazgulled in forum C Programming
    Replies: 5
    Last Post: 06-16-2008, 10:36 AM
  4. Replies: 5
    Last Post: 05-23-2003, 01:11 PM
  5. level up 6 times with 40 EXP!?!?!?!
    By Blizzarddog in forum Game Programming
    Replies: 15
    Last Post: 03-05-2003, 12:56 PM