Author Topic: Highest Ability Score  (Read 466 times)

Legacy_The Mad Poet

  • Hero Member
  • *****
  • Posts: 715
  • Karma: +0/-0
Highest Ability Score
« on: September 15, 2015, 08:46:14 pm »


               

I'm not sure why I'm blanking on this, but I'm trying to pull the highest of the six ability scores. Is there a way to do it easier than doing a bunch of >. < comparisons between them? 



               
               

               
            

Legacy_meaglyn

  • Hero Member
  • *****
  • Posts: 1451
  • Karma: +0/-0
Highest Ability Score
« Reply #1 on: September 15, 2015, 09:34:19 pm »


               

Something like this should work (untested for typos etc):



int nHighAbility = ABILITY_STRENGTH
int nHigh = GetAbilityScore(oPC, nHighAbility);
int i;
for ( i = ABILITY_DEXTERITY; i <= ABILITY_CHARISMA; i ++ ) {
       int nTemp = GetAbilityScore(oPC, i);
       if (nTemp > nHigh) {
             nHigh = nTemp;
            nHighAbility = i;
       }
}

// here nHigh = high ability score, nHighAbility = ABILITY_* which is highets. Favors lower numbered one on ties.

               
               

               
            

Legacy_The Mad Poet

  • Hero Member
  • *****
  • Posts: 715
  • Karma: +0/-0
Highest Ability Score
« Reply #2 on: September 15, 2015, 09:46:45 pm »


               

That's for comparing two of them. Useful, but is there a way to pull it off for all 6?



               
               

               
            

Legacy_Lightfoot8

  • Hero Member
  • *****
  • Posts: 4797
  • Karma: +0/-0
Highest Ability Score
« Reply #3 on: September 15, 2015, 10:49:39 pm »


               

He did compare all 6 of them in the loop.


               
               

               
            

Legacy_meaglyn

  • Hero Member
  • *****
  • Posts: 1451
  • Karma: +0/-0
Highest Ability Score
« Reply #4 on: September 16, 2015, 01:34:11 am »


               

What LF said...   It will work for all of them. I used the constants so that it would be easier to read. Read through what that for loop is doing again and see if  you can see it.



               
               

               
            

Legacy_The Mad Poet

  • Hero Member
  • *****
  • Posts: 715
  • Karma: +0/-0
Highest Ability Score
« Reply #5 on: September 16, 2015, 02:19:10 am »


               

Re-read. 


.


.


.


.


AH!


 


Thanks. Learn something new every day!



               
               

               
            

Legacy_The Mad Poet

  • Hero Member
  • *****
  • Posts: 715
  • Karma: +0/-0
Highest Ability Score
« Reply #6 on: September 16, 2015, 04:09:36 am »


               

Maybe I am reading this wrong, but here we go.


 


Since this works between the constants of Dex and Cha, wouldn't that be 1-5? Would not ABILITY_STRENGTH, which is 0, be ignored this way?


 


God I'm terrible at loops. I have no idea how I've avoided medication from using these.


 


Alright, the for works by cycling through the first expression, in this case 1 (for dexterity), until it reaches the second expression, which would be 5 (for charisma). The i++ means it goes up by one, which means that it would cycle 1, 2, 3, 4, right? If that's the case wouldn't it skip 0 and 6, Strength and Charisma? Unless that <= ABILITY_CHARISMA means that it also includes 5? Not sure there.


 


I see that it goes and checks it against the strength score and picks the highest though....


 


Should it be:



int nHighAbility = ABILITY_STRENGTH
int nHigh = GetAbilityScore(oPC, nHighAbility);
int i;
for ( i = ABILITY_STRENGTH; i <= ABILITY_CHARISMA; i ++ ) {
       int nTemp = GetAbilityScore(oPC, i);
       if (nTemp > nHigh) {
             nHigh = nTemp;
            nHighAbility = i;
       }
}

I ask because I looked up the lexicon for what numbers the constant pulls, and double checked the for loop, because I plan to convert the pulled int to determine a string that names the ability score. Like:



string sAbility;

switch (i)
{
case 0: sAbility = "Strength"; break;
case 1: sAbility = "Dexterity"; break;
.
.
.
.
 


               
               

               
            

Legacy_The Mad Poet

  • Hero Member
  • *****
  • Posts: 715
  • Karma: +0/-0
Highest Ability Score
« Reply #7 on: September 16, 2015, 04:29:50 am »


               

Ah heck... think I just answered my own question. Guessing starting at 0, then checking between 1-5 is right to find highest. I think that the <= ABILITY_CHARISMA was what was throwing me off.


 


Final code, haven't tested it yet.



//TMP - Returns the name of the highest ability score; help from meaglyn
string GetHighestAbility(object oTarget)

{
int nHighAbility = ABILITY_STRENGTH;
int nHigh = GetAbilityScore(oTarget, nHighAbility);
int i;
string sAbility;

for ( i = ABILITY_DEXTERITY; i <= ABILITY_CHARISMA; i ++ )
    {
       int nTemp = GetAbilityScore(oTarget, i);
       if (nTemp > nHigh)
       {
            nHigh = nTemp;
            nHighAbility = i;
       }
    }

switch (nHighAbility)
    {
    case 0: sAbility = "Strength"; break;
    case 1: sAbility = "Dexterity"; break;
    case 2: sAbility = "Constitution"; break;
    case 3: sAbility = "Intelligence"; break;
    case 4: sAbility = "Wisdom"; break;
    case 5: sAbility = "Charisma"; break;
    }

return sAbility;

}

               
               

               
            

Legacy_Proleric

  • Hero Member
  • *****
  • Posts: 1750
  • Karma: +0/-0
Highest Ability Score
« Reply #8 on: September 16, 2015, 08:48:20 am »


               

You could initialise nHigh to zero then loop from ABILITY_STRENGTH to ABILITY_CHARISMA.


 


Only reduces the number of constants quoted by one, though.


 


Arguably, looping from 0 to 5 might be clearer.



               
               

               
            

Legacy_meaglyn

  • Hero Member
  • *****
  • Posts: 1451
  • Karma: +0/-0
Highest Ability Score
« Reply #9 on: September 16, 2015, 02:04:33 pm »


               

Yes,  you could start with invalid values and loop through them all. The amount of code executed is about the same. I went with the approach that since I had to have an initial value for those variables I might as well use the first value we need to check and start the loop at the second.


 


As an aside,  from a software engineering point of view, I'd make that into two functions. One returns the int ABILITY_* that's the highest and a different one that gets the string, call it AbilityToString() or something. You may find other places where something like that will be useful by itself.


 


Also, why not just use the constant names in the case statements? That makes it easier to read as well. For the loop logic I did rely on knowing the actual values of the constants, but you would not even need to have looked them up to make the switch().


e.g.



 case ABILITY_STRENGTH: sAbility = "Strength"; break;
 etc