Class and monsters

This is a discussion on Class and monsters within the C++ Programming forums, part of the General Programming Boards category; Hello, I'm pretty new to C++, but so far I have managed to figure things out from the tutorials on ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    2

    Class and monsters

    Hello, I'm pretty new to C++, but so far I have managed to figure things out from the tutorials on this site. I'm glad they were written, they have been quite helpful. But now I've ran into a problem.

    I haven't actually done it yet, but I'm going to have a menu where a player selects what area they want to explore from a list. After they pick an area there is a random function that goes off, and based on the result of the random, an event will happen. Such events include finding a chest with gold in it, eating a bad berry, and running into monsters. All that I'm sure I can figure out on my own, except for the monster part.

    I want to set up attributes for monsters, such as they're name, hp, level and things like that. And then when a player comes into contact with the monster function, it will run another random function and based on the result(say there are 5 monster types in the area, if the result is 3, "Crockile" or something will show up), a monster will appear. This is where the problem lies. I figured a class could be used to call the monster data that goes with the number, but I have no idea how to save the monster data or to call it with the class. I think I can figure out how to call it, but not really sure how I should save it.

    I'm sorry if my description is not all that clear, I don't know the technical terms for these things. If anyone gets what I'm saying, a nudge in the right direction would be great. Thanks.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If I understand what you're wanting to do (and who knows; I'll give even money odds and take either side), then that sounds like something to be done in the constructor. The class can keep an array (or several arrays) of data, and the constructor can then call a PRNG and set up the current monster based on that result.

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I thought it sounded more like a question about creating a Monster base class & specific derived monster classes... or at least that's the way it should be written.
    Code:
    class Monster
    {
    ...
    protected:
        std::string m_Name;
        unsigned int m_hp;
        unsigned int m_Level;
    };
    
    class Crockile  :  public Monster
    {
    ...
    };
    
    class Alligor  :  public Monster
    {
    ...
    };
    Last edited by cpjust; 10-26-2008 at 01:23 PM. Reason: Changed private to protected.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I guess the difference between the two approaches depends on how different the monsters are. If Monster1.attack() is going to be very different from Monster2.attack() etc., then inheritance (and all the associated virtual polymorphism stuff) is definitely the way to go. Although, cp, wouldn't variables we want to inherit have to be anything but private?

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,014
    I am betting those variable inside Monster should be protected, yes, cpjust?
    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.

  6. #6
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    "Crockile the Public Monster".... I like it

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    2
    I thought I posted this last night, but I guess I didn't.

    I really have no idea how classes work. I did the tutorial one on this site, and was able to change it so that it printed a name to the screen. But I'm not really what all is required for a class to work or what ways you can set it up.

    cpjust, when you posted that I was like "woah" because it was a bit different than the tutorial and it kind of threw me off. But after looking at it a bit, I'm pretty sure I understand how it works. Do I need to create constructors for the two monster classes, and functions for them to display the information and then call those functions?

    I'm posting this from my friends PC and wont be home for a few hours, so I cant try anything right now. That's why I'm asking these questions.

  8. #8
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Elysia View Post
    I am betting those variable inside Monster should be protected, yes, cpjust?
    Oops, yes. It was very late when I posted that -- fixed.

    Quote Originally Posted by mtndewguy View Post
    cpjust, when you posted that I was like "woah" because it was a bit different than the tutorial and it kind of threw me off. But after looking at it a bit, I'm pretty sure I understand how it works. Do I need to create constructors for the two monster classes, and functions for them to display the information and then call those functions?
    If you need something other than the default constructors (which it sounds like you probably do) then you need constructors for all the derived Monster classes.
    You should also have functions like Attack(), RunAway(), GetInjured()... in the base Monster class and then declare them as virtual functions if you need to override them in the derived classes.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,014
    Actually, how about some tutorial on classes?
    It sounds to me like you need to understand how classes work, and that you do not have a simple question about something you do not understand.
    After you have read tutorials, then perhaps you can come back for design/implementation questions? Just so you are sure that you understand how classes work and can interpret code posted by other members... Especially cpjust now.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 60
    Last Post: 12-20-2005, 11:36 PM
  2. RPG ( practice with classes )
    By nickodonnell in forum Game Programming
    Replies: 3
    Last Post: 10-01-2005, 08:22 AM
  3. Classes\Good Usage...
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-20-2005, 12:32 PM
  4. viewing a text string for a member of a class....
    By o0obruceleeo0o in forum C++ Programming
    Replies: 24
    Last Post: 04-13-2003, 09:13 PM
  5. Class Name "monster" not allowed??
    By napkin111 in forum C++ Programming
    Replies: 9
    Last Post: 01-23-2003, 09:39 PM

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