Text based game..

This is a discussion on Text based game.. within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm having some problems structuring my program. How to make it. I want to make a game where a ...

  1. #1
    the Wizard
    Join Date
    Aug 2004
    Posts
    109

    Text based game..

    Hi,

    I'm having some problems structuring my program. How to make it. I want to make a game where a player can select between 3 different types og character, select name and so on, and after that venture through the game. But, how can I make it? I think I'm going to make the 3 different character types in 3 different classes. If that's the best way of doing it?
    If I should use that way, then how can I make it so only the class that the user selected is included?

    I can't figure out if it's the best way og doing it? Maybe some of you already have found a solution to this problem and maybe have some tips, that would be useful for my consideration?

    Thx in advance..
    -//Marc Poulsen -//MipZhaP

    He sat down, he programmed, he got an error...

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    First I'd forget about writing code for a while and focus on developing the game in your head and putting down your thoughts on paper or your favorite text editor. Decide on whether you different players to have same capabilities(attributes and actions) or not? What capablities could they have? Who decides? Are there any overlapping capabilities (age, health, wealth, etc or speaking, walking, shooting, swordfighting, etc). If there are overlapping capabilities then using class inheritance may be to your advantage (if you know about it or are willing to learn as you go). Once you have all that data pretty well digested then starting to write pseudocode/code becomes easier.
    You're only born perfect.

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    13
    hey im only a begginer proggramer in c++ but i have been trying at game design for 2 years or sometin like that and i got some advice:
    class inheritance is a MUST
    know 1 type of gui proggraming well, be it direct x/ openGL ext.
    as he said, get your ideas down
    dont start of with text based, get bigger. nobody wants text based anymore unless its online like a MUD.
    if u want it to be online at least learn some basic network proggraming
    read ANY tutorial you can on AI ( artificial intelligence ) like the 3 on this site
    KNOW THESE CHAPTERS OF CPROGGRAMINGS TUTORIAL BY HEART:
    loops ( repeat attack )
    class inheritance ( to design you classes base like they all have health, defence ext.)
    if statements ( if ( player health = 0 ) cout<< "you died"; )
    the basics ( display in game messages with cout and manipulate strings easily with cin )
    switch case ( useful to cut out long if statements, if you want to do like 10 or 15 if's)

    anyway just my 2 cents, oh yeh also read all 23 getting started tutorials at least im just sayin know those ones cold.
    classes
    file I/o

  4. #4
    the Wizard
    Join Date
    Aug 2004
    Posts
    109
    Thx both
    Well I can hear that I'm on the right track, but I'm still having the problem figuring out how I can include/use only the class for the specific character type.
    I was thinking about at start having 4 classes, one for each character type, and one called player. Where player, has the current HP, MP, LEVEL, EXP stored. Is that a good way to do it?

    Darkcode: Now the world is not just to make a game and get it out. You've have to learn and get better, especially if you want to live by it. I'm making a text based game because I want my programming skills to get better. Remember the sentence: You learn by doing.. If I had a break from C++, I wouldn't get better, would I But thx for the good advice.
    Last edited by MipZhaP; 02-27-2005 at 09:59 AM.
    -//Marc Poulsen -//MipZhaP

    He sat down, he programmed, he got an error...

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    basically you would have a CharacterBase class.
    which could look like this:
    Code:
    class CharacterBase {
    protected:
      std::string name;
    
      int health;
      int experience;
      int level;
    
      std::set< Items > inventory;
    };
    then all stuff that "is a" character would inherit from that base:
    e.g
    Code:
    class OrcCharacter : public CharacterBase {
      protected:
      // orc specific stuff e.g which clan it belongs to
      std::string clan;
    
    }
    let the player chose his character and then you can e.g.
    CharacterBase *p_character = new OrcCharacter("some_name", "some_clan");

    note that polymorphism is used here: that means a pointer to some class is assigned to a pointer to one of its base classes.

    that is useful when you create a function which deals with all characters - not just one special kind.
    e.g.

    Code:
    void steal_item(Character *p_thief, Character *p_victim) {
      Item *p_item = p_victim->get_random_item();
      if(NULL != p_item) {
        p_victim->remove_item(p_item);
        p_thief->insert_item(p_item);
      }
    }
    preceding function works with all objects that are derived from character.

    if some functions only works with a special character you can use typeid to check for its original type:
    Character *p_character = new Orc(...);
    if(typeid(*p_character) == typeid(Orc)) { // this should evaluate to true here

    }

    "unfortunately" typeid only works if the class has at least one virtual function.
    and you might need to specify a special compiler option (e.g. /GR in vc)

    well there you have some stuff to start with


    note that i have not included member declarations - even though they would be there in the actual code
    Last edited by Raven Arkadon; 02-27-2005 at 10:51 AM.
    signature under construction

  6. #6
    the Wizard
    Join Date
    Aug 2004
    Posts
    109
    I only have one question about your beautiful example Raven Arkadon..
    Where do you have the "("some_name", "some_clan");" from in line:
    CharacterBase *p_character = new OrcCharacter("some_name", "some_clan");

    Is it the constructer in the class??
    -//Marc Poulsen -//MipZhaP

    He sat down, he programmed, he got an error...

  7. #7
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    yes - it would be the constructor.

    the idea is to gather data from what the user wants his character to be
    and then pass that data to the constructor of the desired character class.

    in case there are many attributes for a character then the constructor would have to accept like 10 arguments - which looks pretty ugly - also youd have 10 variables.
    that can be solved by using a struct which stores all data that is required to instanciate the character class

    Code:
    struct CharacterDescription {
      int desired_health;
      int desired_strength;
    
    };
    
    struct OrcCharacterDescription : public CharacterDescription {
      string desired_clan;
      ...
    };
    
    
    class OrcCharacter {
    public:
      // constructor to instanciate from a desciption
      // if something with wrong with the description it might
      // raise some exception
      OrcCharacter(const OrcCharacterDescription &r_desc) {
        if(r_desc.desired_health > 20)
          throw InvalidDescrption();
      ...  
      }
    }
    where InvalidDescription is another class.

    exception handling is another thing you might use.
    maybe you want to read this:
    http://www.parashift.com/c++-faq-lit....html#faq-17.2
    signature under construction

  8. #8
    Registered User
    Join Date
    Feb 2005
    Posts
    13
    to put it simply you would do something like this for ALL classes:
    Code:
    class playerclass {
    protected:
    int health;
    char name;
    int attack;
    int defence;
    int stamina;
    };
    then you would do this for say, a sorcerer?

    Code:
     sorcerer::protected playerclass {
    public:
    int mana;
    int magic skill;
    char spell 1; // ( do however many spells you want if you like. )
    char spell 2;
    char spell 3;
    }
    that way, the mage would get all the normal qualitys every class has, but also its own unique ones.MUCH easier than writing everything its own classes. ( by the way i used public on sorecrer because outside code will need to enter to affect the health amount and stats increase.)

  9. #9
    the Wizard
    Join Date
    Aug 2004
    Posts
    109
    Thx Raven Arkadon, I understand it very well
    Darkcoder: Well I don't want a 1-chared name/spell But know what you mean But btw. thx for your example
    Last edited by MipZhaP; 02-28-2005 at 10:51 AM.
    -//Marc Poulsen -//MipZhaP

    He sat down, he programmed, he got an error...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Text based game
    By wipeout4wh in forum C Programming
    Replies: 12
    Last Post: 03-26-2009, 05:39 PM
  2. Text based game
    By beene in forum Game Programming
    Replies: 10
    Last Post: 05-12-2008, 08:52 PM
  3. New Project, text game, design stage.
    By Shamino in forum Game Programming
    Replies: 9
    Last Post: 05-23-2007, 07:39 AM
  4. Saving a text game
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 03-27-2002, 01:33 PM
  5. Text Based Game
    By drdroid in forum C++ Programming
    Replies: 2
    Last Post: 02-18-2002, 06:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21