-1

I'm very new to Java and have been self-teaching myself. I'm creating an RPG game where skills reflect a "modifier", you gain or lose modifiers based on your overall skill in that field.

For example, if your "strength" skill is at 10, you gain a +0 modifier towards anything that requires strength. If it's 12, you gain a +1 modifier, 14 is +2 and so forth.

Is there some way I can do without having to create a huge table listing all the increments as if statements?

3
  • That would depend on your logic for handling modifiers. From your example, it could be assumed that any skill over 10 gets a 1 point modifier for every 2 skill points. You should clarify that logic.
    – Arman H
    Feb 14, 2014 at 1:26
  • That is precisely what I want! However I can not think of a way (with my limited knowledge) to state that in the code. Feb 14, 2014 at 1:30
  • Welcome to Stack Overflow. Please refer to the definition of each tag on this site before using it. As a site intended for Programmers, our usage of "RPG" means the programming language, not the vague class of games. Questions dealing with concepts of game development often belong on our sister site gamedev.stackexchange.com
    – WarrenT
    Feb 14, 2014 at 4:59

3 Answers 3

3

It sounds like you just want some formula to calculate the modifiers. Guessing from your question, the formula would be something along the lines of:

int modifier = Math.floor((skillLevel - 10) / 2);

If you want different formulas for different types of skills, then you're probably going to want a Skill class with different constants stored in it, used to calculate the formula. For instance, if strength is calculated as above, but intelligence needs to be at 15 before you start gaining a modifier, you could do this:

public class Skill {
    int baseLevel;

    public Skill(int level) {
        this.baseLevel = level;
    }

    public int getModifier(int skillLevel) {
        return Math.floor((skillLevel - baseLevel) / 2);
    }
}

Create the strength instance of Skill with a baseLevel of 10 and the intelligence instance of Skill with a baseLevel of 15. If your formula for calculating the modifier is more complicated, you can change this class accordingly.

2
  • Thanks for the fast response! Say I wanted to give -1 modifiers for every 2 below 10, how would I go about that? Feb 14, 2014 at 1:34
  • The formula works for that as well, but it might not give you quite what you're looking for. Because Math.floor always rounds down, a strength of 8 or 9 would get a modifier of -1, while a strength of 7 or 6 would get a modifier of -2. If you'd prefer to give 9 a modifier of 0, you'd need to write a separate formula for skill levels that are smaller than the base level. Feb 14, 2014 at 1:44
0

For the example you gave you could do this:

modifier = (skill - 10) / 2;

Which says every two points > 10 gives +1 modifier. This is a simplistic solution as it doesnt account for skills < 10 but you get the idea.

0
0

Look at Java Collections Framework and choose a class to store your tables indicating what modifier applies to given scores. You want to be able to load the data initially, then lookup the answer for a score as needed. Therefore you may want to look at some class implementing java.util.map. I'll leave that learning exercise to you ;-)

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.