1

This is not homework. I'm designing an RPG, and for ability scores I am rolling 4d6, removing the lowest roll, and then adding the remaining total. What I have is below, and I was just curious if anyone had any other better suggestions. For brevity I've removed the roll actions, and I just plugged in four integers.

int[] rolls = { 6, 3, 2, 5 };
int abilityScore = rolls[0] + rolls[1] + rolls[2] + rolls[3];
int low = rolls[0];

for (int i = 1; i < rolls.length; i++)
{
    if (rolls[i] < low)
    {
        low = rolls[i];
    }
}

return abilityScore -= low;
0

4 Answers 4

5

I'd do it like this - no array needed and calculating sum inside the loop:

int get_total_without_lowest_roll(int roll_count) {
  int abilityScore = 0;
  int low = MAX_INTEGER;
  int roll;
  for (int i = 0; i < roll_count; i++)
  {
    roll = roll_dice(); // Roll your dice here
    if (roll < low)
    {
        low = roll;     
    } // By the way, good alternative would be low = min(low,roll);
    abilityScore += roll;
  }

  return (abilityScore - low);
}
Sign up to request clarification or add additional context in comments.

5 Comments

you have to initialize low to MAX_INTEGER, and start i at 0. a better style would be to use the for loop as described here: leepoint.net/notes-java/flow/loops/foreach.html
This looks to give me the flexibility of the array not having to be fixed if I want to use this for something else. Is that correct?
@Dan: Thanks, fixed. @hal10001: Well, you don't even need an array anymore, so you're both more flexible (max_rolls can be chosen) and you need less memory than with an array.
could you illustrate with an example.quite confusing
@Deepak: Edited the code a bit to illustrate that this is a ready-to-use function that rolls roll_count times and returns the total without the lowest roll.
1

you can use java's sorting mechanisms as explained here

After sorting, then remove the lowest element and add the rest of the array together.

6 Comments

he also wants to compute the total sum.
sort = O(n log n), remove + sum = O(n)... your approach is expensive.
@Dan huh? im not sure why we are referring to log(n) here
@Neal: sorting an array of n elements takes at best O( n log n) for comparison algorithms (en.wikipedia.org/wiki/…)
Ok... so what? its a tiny array
|
0
int[] rolls = { 6, 3, 2, 5 };
int abilityScore = 0
int low = MAX_INTEGER;

for (int i = 1; i < rolls.length; i++)
{    
    total+=rolls[i];
    if (rolls[i] < low)    
        low = rolls[i];

}

return abilityScore -= low;

not too different, but competly dynamic

Comments

-1

This was my solution to remove the lowest roll:

    static void removeLowest(ref List<int> rolls)
    {
        int lowest = 0;
        for (int i = 1; i < rolls.Count; i++)
            if (rolls[i] < rolls[lowest])
                lowest = i;
        rolls.RemoveAt(lowest);
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.