I'm not entirely sure what it is you are trying to achieve, but if I was doing something like this I might start with a basic concept of a Die
and it's core functionality...
public interface Die {
public int getMinium();
public int getMaximum();
public int roll();
}
Then I would create an abstract
version which filled out the most common, basic functionality, so I wouldn't need to repeat myself all the time...
public abstract class AbstractDie implements Die {
private final int minimum;
private final int maximum;
public AbstractDie(int minimum, int maximum) {
this.minimum= minimum;
this.maximum = maximum;
}
@Override
public int getMinimum() {
return minimum;
}
@Override
public int getMaximum() {
return maximum;
}
@Override
public int roll() {
return getMinimum() + (int)(Math.random() * getMaximum());
}
}
And then finally some implementations, for example...
public class TwelveSidedDie extends AbstractDie {
public TwelveSidedDie() {
super(1, 12);
}
}
This would then allow me to use the class when ever I needed to, for example...
Die[] dice = new Die[] {new TwelveSidedDie(), new TwelveSidedDie(), new TwelveSidedDie(), new TwelveSidedDie()};
int sum = 0;
for (Die die : dice) {
int roll = die.roll();
System.out.println(roll);
sum += roll;
}
System.out.println("Total: " + sum);
It should be noted that the above could also be achieved by using something like...
Die die = new TwelveSidedDie();
int rolls = 4;
for (int roll = 0; roll < rolls; roll++) {
int result = die.roll();
System.out.println(result);
sum += result;
}
System.out.println("Sum: " + sum);
Which leads to the ability to write a method that can simulate a number of Die
rolls...
public static int[] diceRolls(Die die, int numberOfRolls) {
int[] results = new int[numberOfRolls];
for (int roll = 0; roll < numberOfRolls; roll++) {
results[roll] = die.roll();
}
return results;
}
public static int sumDiceRolls(Die die, int numberOfRolls) {
int[] results = diceRolls(die, numberOfRolls);
int sum = 0;
for (int result : results) {
sum += result;
}
return sum;
}
Which would allow you to pass in any implementation of Die
and have them rolled automatically as you needed...
For example...