Thread: Multiple Questions

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    70

    Multiple Questions

    Hello. I am very new to C++. About a week to be exact. Either way, I have been studying it alot, and have come to know quite a bit for one week. Yet, I have some questions to ask.

    #1 If I was going to make a game for example, how would I randomize the damage according to the item. For example if i would have int knife = 1; just to say that the person has a knife, how would I make the damage ranging from about 1-3 damage. I know this may be a little hard to understand, so I'd be more than happy to try to explain again if you can't understand me.

    #2 How would I create a class so that when i skip from function to function, or even bracket to bracket, the same integers AND values would stay with the flow of the game. For example. If I would have int knife = 1; and i skipped functions how would i make knife still = 1? Also, along this same line, how would i "save" per say an item so that when i skip brackets or functions the person would still have a knife. I do realise this is not a simple 1 line code also. But help would be very helpful.

    #3 How would I save items to a file that can be called when the .exe is run once again.

    #4 How do put images and audio into script.


    Phew, I think that's it for me, Thanks.

  2. #2
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Location
    Montreal, Quebec
    Posts
    669

    ...

    #1
    int damage=rand()2;
    damage=damge+1;

    //don't forget to make it 2 and not three because its range starts at zero that means you can't forget the damage+1 thing.

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Re: Multiple Questions

    #2 How would I create a class so that when i skip from function to function, or even bracket to bracket, the same integers AND values would stay with the flow of the game. For example. If I would have int knife = 1; and i skipped functions how would i make knife still = 1? Also, along this same line, how would i "save" per say an item so that when i skip brackets or functions the person would still have a knife. I do realise this is not a simple 1 line code also. But help would be very helpful.
    You could make it global, like this:
    Code:
    class ITEM
    {
       public:
          int Strength;
          int Defense;
    
          //Constructor
          ITEM(int Str, int Def)
          {
             Strength = Str;
             Defense = Def;
          }
    };
    
    ITEM Knife(4, 0);
    ITEM Dagger(12, 0);
    ITEM Helmet(0, 5);
    
    void DoWhatever()
    {
       InflictDamage(Knife.Strength);
       ShieldAttack(Helmet.Defense);
    }
    
    int main()
    {
       while(GameIsRunning)
       {
          DoWhatever();
       }
       return 0;
    }
    #3 How would I save items to a file that can be called when the .exe is run once again.
    There is a tutorial on file saving on this site.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    #1......
    rand()%3 +1 will give random number in range 1-3

    #2.....
    common scenario is to pass the variables to functions as parameters. If you really have variables that are used everywhere then they could possibly be made global or even better namespace scope. Read about namespaces and see how they can help you. Too early to be worrying about classes.

    #3.....
    Been covered many times before on these boards. My advice is dont worry about files yet. Learn the more important things first like arrays,structures,classes,pointers,templates, functions,etc.
    Until you understand those there is no point in talking about files.

    #4.....
    Ditto #3.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    70

    #1

    Alright, how would i decrement a variable according to the random number, without making if statements such as
    Code:
    if(damage==1)
    {
    ehealth--;
    }
    caz that's how i would do it, but i'm sure there is a much better way to do it.


    Also, what compilier should i be using for C++ that will work good. As of right now I'm using Microsoft Visual C++, which I can assume is a little different...
    Last edited by Extol; 09-15-2002 at 12:43 PM.

  6. #6
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Location
    Montreal, Quebec
    Posts
    669

    ...

    actually, to stoned coder... what would happen if you got 3, then? the outcome would be 4 because you added one so it should be a random 2 number + 1.

  7. #7
    Registered User
    Join Date
    Sep 2002
    Posts
    70
    [CODE]int damage=rand()2;
    damage=damge+1;CODE] is not working in my compilier. Is it because of Visual C++?

  8. #8
    Registered User Dr. Bebop's Avatar
    Join Date
    Sep 2002
    Posts
    96
    You wouldn't get 3, rand()%3 gives you a range between 0 and 2 but not including 3, add one to that and you get the range 1 to 3.
    Processing error: Stupidity detected.
    ------------------------------
    Dr. Bebop
    Windows XP Professional Ed.
    Microsoft Visual Studio 6

  9. #9
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    well drdroid wins the "being a complete plum award".
    Learn to use rand properly before u carp !
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  10. #10
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Re: ...

    Originally posted by drdroid
    actually, to stoned coder... what would happen if you got 3, then? the outcome would be 4 because you added one so it should be a random 2 number + 1.
    Try this code:

    cout << (3 % 3);

    And see for yourself if it becomes 3 or 0.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions on multiple thread programming
    By lehe in forum C Programming
    Replies: 11
    Last Post: 03-27-2009, 07:44 AM
  2. Questions about Multiple Threads and Sleep
    By BENCHMARKMAN in forum C Programming
    Replies: 13
    Last Post: 02-24-2008, 04:57 PM
  3. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  4. Linker errors - Multiple Source files
    By nkhambal in forum C Programming
    Replies: 3
    Last Post: 04-24-2005, 02:41 AM
  5. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 09:44 AM