diff options
| author | Ben Culkin <scorpress@gmail.com> | 2020-11-14 22:07:50 -0500 |
|---|---|---|
| committer | Ben Culkin <scorpress@gmail.com> | 2020-11-14 22:07:50 -0500 |
| commit | 84f0803068ba50bcba67de42634a4bf3d0e9e170 (patch) | |
| tree | 6c489ae7d727558333664ac8ff834d398a424322 /dice/src/main/java/bjc/dicelang/neodice/DiePoolFactory.java | |
| parent | 45f36834bec138429abaecd37021e7d243d7506a (diff) | |
Begin new dice implementation
For various reasons, I'm not happy with the other implementations.
Here's hoping that this one is better
Diffstat (limited to 'dice/src/main/java/bjc/dicelang/neodice/DiePoolFactory.java')
| -rw-r--r-- | dice/src/main/java/bjc/dicelang/neodice/DiePoolFactory.java | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/dice/src/main/java/bjc/dicelang/neodice/DiePoolFactory.java b/dice/src/main/java/bjc/dicelang/neodice/DiePoolFactory.java new file mode 100644 index 0000000..6d9314d --- /dev/null +++ b/dice/src/main/java/bjc/dicelang/neodice/DiePoolFactory.java @@ -0,0 +1,82 @@ +package bjc.dicelang.neodice; + +import java.util.*; + +/** + * Various static functions which create instances of DiePool. + * + * @author Ben Culkin + * + */ +public class DiePoolFactory { + /** + * Create a die pool containing the provided dice. + * + * @param dice The dice to put into the pool. + * + * @return A pool which contains the provided dice. + */ + public static DiePool containing(Die... dice) { + return new FixedDiePool(dice); + } +} + +final class FixedDiePool implements DiePool { + private final Die[] dice; + + public FixedDiePool(Die[] dice) { + this.dice = dice; + } + + @Override + public int[] roll(Random rng) { + int[] results = new int[dice.length]; + + for (int index = 0; index < dice.length; index++) { + results[index] = dice[index].roll(rng); + } + + return results; + } + + @Override + public Die[] contained() { + return dice; + } + + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + + for (int i = 0; i < dice.length; i++) { + Die die = dice[i]; + + builder.append(die); + + // Don't add an extra trailing comma + if (i < dice.length - 1) builder.append(", "); + } + + return builder.toString(); + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + Arrays.hashCode(dice); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) return true; + if (obj == null) return false; + if (getClass() != obj.getClass()) return false; + + FixedDiePool other = (FixedDiePool) obj; + + return Arrays.equals(dice, other.dice); + } +}
\ No newline at end of file |
