If you want to avoid a scripting language like Groovy, then you might want to consider using a configuration syntax that permits you to specify name=list-of-values. Let's assume the syntax is:
name = [ "list", "of", "values" ];
With such a syntax, you could specify pairs of values to provide a mapping from character level to probability of event occurring. If the character levels go from 1 to 10 and the default probabilities mirror the character level, then the entry in the configuration file would be:
level_to_probability_mapping = [
# character level --> probability
#----------------------------------
"1", "1",
"2", "2",
"3", "3",
"4", "4",
"5", "5",
"6", "6",
"7", "7",
"8", "8",
"9", "9",
"10", "10",
];
If your config-file syntax supports only name=single-value syntax, then you can emulate name=list-of-values by setting the value to be a string that contains comma-separated items, and then use java.util.StringTokenizer
or String.split()
to break up the string to gain access to the individual items.
A different approach that is much more flexible but dangerous because it allows a person to execute arbitrary code is as follows...
Step 1. Define a Java interface (or abstract base class) that defines one method. For example:
interface ProbabilityCalculator {
public int calculateProbabilityForLevel(int level);
}
Step 2. Write a "default" class that implements that interface using a simple algorithm.
Step 3. Permit administrators to write their own class that implements the above interface, and add a jar file containing that class to the application's CLASSPATH
.
Step 4. Have a variable in the configuration file specify the name of a class that implements the above interface. If this variable is not present in the configuration file, then fall back to using the name of the default implementation class.
Step 5. Use Java's reflection API to create an instance of the class specified in the configuration file, and then invoke its calculateProbabilityForLevel()
method.
If you are not already familiar with Java reflection, then have a look at my Java Reflection Explained Simply presentation to learn what you will need to know.