damage dealt algorithm

This is a discussion on damage dealt algorithm within the C++ Programming forums, part of the General Programming Boards category; All the variables associated with the bottom code. Code: char combat; int mhp1=20; int fight; int fcom=((strength*3)+agility/fight); int finalcom=(mhp1-fcom); The ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    465

    damage dealt algorithm

    All the variables associated with the bottom code.
    Code:
        char combat;
        int mhp1=20;
        int fight;
        int fcom=((strength*3)+agility/fight);
        int finalcom=(mhp1-fcom);

    The evil stuff giving me trouble.
    Code:
    	case 'f':
    	cout<<"You encounter one dumb sunava..........";
    	do{
    	fight=rand()%5;
    	cin>>combat;
    	if(combat=='a'){
    	cout<<finalcom;
    	cout<<fcom;
    	}
    	}while(finalcom>0);
    	break;
    It works fine except I get some insane numbers like -130593985794. My mud is just over 100 lines so I don't want to post the entire thing.
    My computer is awesome.

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    What's getting you insane numbers where?

  3. #3
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    How can you divide agility by fight when you haven't even initialized fight yet?

  4. #4
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Heck, how can you print finalcom and fcom before you've put values in them?
    Code:
    fight=rand()%5;
    cin>>combat;
    
    if(combat=='a'){
      //You need to put your calculations of finalcom and fcom here.
      cout<<finalcom;
      cout<<fcom;
    C++ is not a symbolic language (or whatever that's called), so even if you say "x = 3a + 2", if you change 'a' later in the program, x won't change.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  5. #5
    Registered User Finchie_88's Avatar
    Join Date
    Aug 2004
    Posts
    154
    If I were you, I wouldn't use the rand() as you might find it a little predictable. Try using srand(). If you don't know how to use that then read the tutorial at the cprogramming website.

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    1,205
    he should use rand. srand only seeds the randomizer and should only be called once each program.

  7. #7
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    I am using srand I just didn't post it. Since I'm way off how should I do it?
    My computer is awesome.

  8. #8
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>Since I'm way off how should I do it?
    Quote Originally Posted by Hunter2
    //You need to put your calculations of finalcom and fcom here.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  9. #9
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    I changed it to this, but it just terminates when it gets to calculating these values.
    Code:
    	do{
    	fight=rand()%5;
    	cin>>combat;
    	if(combat=='a'){
    	return ((strength*3)+agility/fight)==finalcom;
    	cout<<finalcom;
    	}
    	}while(finalcom>0);
    What do I change now?
    My computer is awesome.

  10. #10
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>return ((strength*3)+agility/fight)==finalcom;
    You really ought to go over your basics of C++ syntax again.

    'return' quits the function, returning the value specified.
    == is the equality comparison operator, i.e. it gives you true if the left-hand side is equal to the right-hand side and false otherwise.

    What you want is the = (assignment) operator, which takes the value on the right-hand side and stores it in the variable specified on the left-hand side.

    See if you can figure out what to do. Hint: Study my previous comment very carefully, then go back to your first post and read over the first code sample very carefully.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  11. #11
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    Code:
    finalcom=((strength*3)+agility/fight);
    Gosh so easy to figure out. Before now I've never done something like that in code. I plugged my numbers in though I entered 7 for strength and 7 for agility and my random number is 1-5, but my program returned 24. Any idea why?
    My computer is awesome.

  12. #12
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>and my random number is 1-5
    Just to make sure you realize this, rand() % 5 will give you from 0 to 4.

    (strength*3) + agility/fight
    = 7*3 + 7/(random from 1 to 5)

    If fight is 1, agility/fight is 7/1, which is 7.
    If fight is 5, agility/fight is 7/5, which is truncated to 1.

    Therefore, (strength*3) + agility/fight
    is
    21 + (some number from 1 to 7)
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  13. #13
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    I need another set of parentheses I want it to go from left to right.
    Code:
    finalcom=(((strength*3)+agility)/fight);
    Doesn't using srand like this stop it from generating 0?
    Code:
    srand( (unsigned int) time (0));
    My computer is awesome.

  14. #14
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    No, it just makes sure that the random numbers you generate will be different each time you run the program. Otherwise, you'll get the same sequence of numbers every time.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  15. #15
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    This works fine if I take out the cout at the end, but while its in it has an error. Should I put a for loop in the while loop so when the monsters hp goes down it stays down?

    Code:
    	case 'f':
    	cout<<"You encounter one dumb sunava..........";
    	while(finalcom>0){              //fight
    	fight=rand()%6;
    	cin>>combat;
    	if(combat=='a'){                //attack
    	fcom=(((strength*3)+agility)/fight);
    	finalcom=mhp1-fcom;                   //calculations
    	}
    	if(finalcom>0){
    	cout<<finalcom;
    	}
    	}
    	cout<<"He is dead";
    No, it just makes sure that the random numbers you generate will be different each time you run the program.
    Then how can I stop it from generating 0.
    My computer is awesome.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Implement of a Fast Time Series Evaluation Algorithm
    By BiGreat in forum C Programming
    Replies: 7
    Last Post: 12-04-2007, 01:30 AM
  2. In a game Engine...
    By Shamino in forum Game Programming
    Replies: 28
    Last Post: 02-19-2006, 10:30 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

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