My Rpg system(Text)

This is a discussion on My Rpg system(Text) within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by kevinawad Yes it is...When player enters the shop. It's better declaring all the functions into the player ...

  1. #31
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,681
    Quote Originally Posted by kevinawad View Post
    Yes it is...When player enters the shop.


    It's better declaring all the functions into the player class...Then it will make sense since the player can go to the main, to the battleground or whatever.
    I think what Elysia is trying to say is that a place (shop, battlefield, or whatever) is not a PART of a player - it is a place that one or more players can be. So it's a "has-a" rather than "is-a". Those are concepts in object oriented programming that you need to understand and figure out how it applies to your object design.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  2. #32
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    21,121
    A short example:
    Code:
    class CShop
    {
    public:
        // Place some enums with what items the shop has here, maybe.
        enum Items
        {
              ULTIMATE_WEAPON
        }
        void Enter(Player& plr) { cout << plr->name << " has entered the shop.\n"; plr->bInShop = true; }
        void Purchase(Items Item, Player& plr)
        {
            if (!plr->is_in_shop())
                throw std::error("Player not inside shop");
            std::map<Items, int>::iterator i = m_ItemQuantity.find(Item);
            assert(i != m_ItemQuantity.end()); // throw std::error("Item does not exist in shop");
            if (i->second == 0)
                throw std::error("Quantity of item is 0");
            i->second--;
            plr->AddItem(Item);
        }
    private:
        std::map<Items, int> m_ItemQuantity;
    };
    
    class Player
    {
    private:
        friend void CShop::Enter(Player& plr);
        bool m_bInShop; // Init to false
        std::string m_strName;
    public:
        std::string name() { return m_strName; }
        bool is_in_shop() { return m_bInShop; }
        void AddItem(Shop::Items Item) { /* TODO: Implement */ }
    };
    Last edited by Elysia; 06-25-2008 at 09:44 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  3. #33
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    847
    you're going about this in just about the hardest way possible.

    there's absolutely no reason to have a function for "enter shop".

    again, look at the examples i posted. in the class room, there is a string "desc", short for description.

    what you might want to do is have a function like this:
    Code:
    player::setLocation(room r)
    {
        cout << r.desc;
        location = r;
    }

  4. #34
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,681
    Quote Originally Posted by m37h0d View Post
    you're going about this in just about the hardest way possible.

    there's absolutely no reason to have a function for "enter shop".

    again, look at the examples i posted. in the class room, there is a string "desc", short for description.

    what you might want to do is have a function like this:
    Code:
    player::setLocation(room r)
    {
        cout << r.desc;
        location = r;
    }
    Overall, I agree. I would personally split the "displayLocation" (or "describeLocation") and "setLocation" - one shows the description of the current location, and one sets what the current location is.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #35
    kevinawad
    Guest
    What does friend stand for?

  6. #36
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    21,121
    Friend means that a specific function or class can access a class's private and protected members.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  7. #37
    kevinawad
    Guest
    Elysia , your code is giving me a headache. But it looks clear to me that, this will take the items from the enum and print them into the shop while players can buy it.

  8. #38
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    21,121
    Not quite. But if you don't understand it, I do suggest you ask questions about what you don't understand. It is critical that you learn the basics, which is what this code is compromised of.

    The CShop class represents a shop. A shop would then contain a number of items. The sortiment is stored inside the Items enum.

    The function Enter is called where you pass in the player who is to enter the shop. This will make sure the user is "flagged" as having entered the shop. The function will print that the player's name has entered the shop.

    The function Purchase will allow a player inside the shop to purchase an item from the store.
    It will make sure the player is inside the shop, it will make sure that the requested item is part of the shop sortiment and it will also check that the quantity of the item is larger than 1 (ie, the shop must at least have one of the items so the player can purchase it).
    It will then decrement the quantity by one and add that items to the player's inventory.
    The m_ItemQuantity variable will contain the quantity of each item.

    The player class has a couple of things, as well.
    The friend declaration is there to make sure CShop::Enter can access the private variable m_bInShop.
    The m_bInShop variable is there to indicate if a player is inside a shop or not.
    m_strName should hold the player's name.
    Function name() will return the player's name.
    Function is_in_shop will return true if player is inside a shop.
    AddItem will add an item to the player's inventory.


    Note, of course, that this was written in a hurry and not checked if correct. There is at least 1 bug in the example.
    Last edited by Elysia; 06-25-2008 at 10:01 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  9. #39
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    847
    Quote Originally Posted by matsp View Post
    Overall, I agree. I would personally split the "displayLocation" (or "describeLocation") and "setLocation" - one shows the description of the current location, and one sets what the current location is.

    --
    Mats
    yes, definitely. a newline-delimited list of the names of the room's occupants traditionally follows the room's description

  10. #40
    kevinawad
    Guest
    Code:
    int Player::getName()
    {
         
    	return mainname;
    	
    }

    Return cannot convert a string into an int...

    How am i supposed to get the name then?

    I have tryed

    Code:
    cout <<mainname;
    return true;
    Let's say

    Code:
    name = "None"
    Then it will write None1 because it will return true and print the true on the screen. Can anyone help me thank you.

  11. #41
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    21,121
    Make the return type to the same that it actually returns.
    If "mainname" is a std::string, then the function must return a std::strung.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  12. #42
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,162
    Again... concentrate on the concepts. Build minimal applications that use the concepts you are learning and nothing more. In due time you will have the opportunity to apply your knowledge and study the integration and applicability of those concepts in a full fledged problem. Trust me, you will. And it will come naturally and more easily than you are experiencing now, exactly because you will have a better C++ baggage.

    You are taking a step larger than your leg by not dividing your objectives in meaningful tasks. Yes, you want to be a games programmers. Fine... but for that you want to learn C++. So that is your current and only objective. Nothing less, nothing more.

    You cannot go from a if..else if structure with cin and cout as you demonstrated in your first code yesterday, to a class structure in one day. You will skip important details and fundamental concepts in between. By avoiding real life examples and instead build minimal applications that address the concept at hand you will also restrict that code to the thing you are learning at that moment.

    An example is exactly that by trying to build this inventory system you are being forced into other concepts like friend, exception handling, inheritance, composition... all things you are yet to properly study. You are diluting your ability to take your apprenticeship as a process made of clear steps.

    Keep creating code, by all means! That's the best way to learn. But avoid applicate that code to a real project. The project comes later and will be a lot more fun then.

    Anyways... I shouldn't even be here. Bye. And best of luck.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  13. #43
    kevinawad
    Guest
    I still cannot return a string...

    How am i supposed to return mainname?

  14. #44
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    21,121
    We're not mind readers, so you need to show code and compile errors.
    But consider what Mario says, as well. Learn new concepts, try examples, then when you have the grasp around them, try integrating them into your RPG project.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  15. #45
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    847
    Code:
    string player::getName()
    {
    return mainname;
    }

Page 3 of 4 FirstFirst 1234 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. here it is again folks...news on my rpg
    By agerealm in forum Game Programming
    Replies: 3
    Last Post: 05-31-2010, 06:58 PM
  2. problem
    By ExDHaos in forum C++ Programming
    Replies: 12
    Last Post: 05-22-2009, 04:50 AM
  3. (dev) C++ Ascii Rpg
    By kevinawad in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 08-10-2008, 11:10 AM
  4. Char Variable Probelm
    By Krak in forum C++ Programming
    Replies: 1
    Last Post: 01-26-2003, 11:34 AM
  5. text rpg
    By c++.prog.newbie in forum Game Programming
    Replies: 48
    Last Post: 12-30-2001, 10:24 PM

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