Sourceforge
JavaDoc
Homepage
Download
package contains:
Randomizer - gives every Object an equal chance to be returned from the group.

code example:
Randomizer<String> ran = new Randomizer<String>("Bear", "Duck", "Pig");
System.out.println(ran.next());
RandomizerSet - returns every Object only once, but in random sequence. The initial state can be restored by calling reset() method.

code example:
RandomizerSet<String> ran = new RandomizerSet<String>("Bear", "Duck", "Pig");
System.out.println(ran.next());
RandomizerMap - Object's chance to be returned is based on it's weight compared to the total sum of weights.

code example:
RandomizerMap<String> ran = new RandomizerMap<String>();
ran.add("Duck", 1);
ran.add("Bear", 5);
ran.add("Pig", 15);
System.out.println(ran.next());
All the classes above are implementing interface RandomSelector, so it supports polymorphism. You are welcome to write your own randomizer types and present them here, if none of the above fits your needs.
Useful Example:
Although, the basic functionality of the RandomizerMap is easy to understand, using it in a game needs a bit more work.
Below I've implemented a part of a loot system in a classical hack'n'slash game.
import java.util.LinkedList;
import ee.joonasvali.randomizer.RandomizerMap;
public class NPC {
private static LinkedList<Generator>
common = new LinkedList<Generator>(),
rare = new LinkedList<Generator>();
static{
common.add(new CoinGenerator());
rare.add(new CoinGenerator());
rare.add(new ItemGenerator());
rare.add(new ItemGenerator());
}
public static void main(String[] args) {
RandomizerMap<LinkedList<Generator>> ran = new RandomizerMap<LinkedList<Generator>>();
ran.add(common, 5);
ran.add(rare, 1);
printTreasure(ran.next());
}
public static void printTreasure(LinkedList<Generator> gen){
System.out.println("Player searched the body and found the following items: ");
for(Generator g: gen){
System.out.println(g);
}
}
}
The generators are needed to provide some alternation in the loot. RandomizerMap is used to select the generators, which generate the loot.
As you can see from above, I've assigned the value 1 to a list of generators "rare", which generates better loot than the "common" list of generators,
which generates only coins and has a value 5.
public interface Generator<T> {
public T next();
}
public class CoinGenerator implements Generator<Integer>{
@Override
public Integer next() {
return (int)(Math.random()*500)+1;
}
@Override
public String toString() {
return "coins "+next();
}
}
public class ItemGenerator implements Generator<String>{
private RandomSelector<String> ran = new Randomizer<String>(new String[]{"Helmet", "Armor", "Health Potion", "Necklace"});
@Override
public String next() {
return ran.next();
}
@Override
public String toString() {
return next();
}
}
Sample Output:
Player searched the body and found the following items: coins 218
Player searched the body and found the following items: coins 420
Loot from rare generator list:
Player searched the body and found the following items: coins 103 Necklace Health Potion
Loot from rare generator list:
Player searched the body and found the following items: coins 401 Armor Necklace
Player searched the body and found the following items: coins 314
Edited by Sinipull, 18 February 2012 - 01:54 AM.


Sign In
Create Account


Back to top









