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/Die.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/Die.java')
| -rw-r--r-- | dice/src/main/java/bjc/dicelang/neodice/Die.java | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/dice/src/main/java/bjc/dicelang/neodice/Die.java b/dice/src/main/java/bjc/dicelang/neodice/Die.java new file mode 100644 index 0000000..a331e4e --- /dev/null +++ b/dice/src/main/java/bjc/dicelang/neodice/Die.java @@ -0,0 +1,96 @@ +package bjc.dicelang.neodice; + +import java.util.*; +import java.util.stream.*; + +/** + * Represents a single polyhedral die. + * @author Ben Culkin + * + */ +@FunctionalInterface +public interface Die { + /** + * Rolls this die. + * + * @param rng The source for random numbers + * + * @return The result of rolling the die. + */ + public int roll(Random rng); + + /** + * Returns a die pool which rolls this die the specified number of times. + * + * @param numTimes The number of times to roll this die. + * + * @return A die pool that rolls this die the specified number of times. + */ + default DiePool times(int numTimes) { + return new TimesDiePool(this, numTimes); + }; + + /** + * Create an iterator which gives rolls of this dice. + * + * @param rng The source for random numbers. + * + * @return An iterator which gives rolls of this dice. + */ + default Iterator<Integer> iterator(Random rng) { + return IntStream.generate(() -> this.roll(rng)).iterator(); + } +} + +final class TimesDiePool implements DiePool { + private final Die contained; + private final int numDice; + + public TimesDiePool(Die contained, int numDice) { + this.contained = contained; + this.numDice = numDice; + } + + @Override + public int[] roll(Random rng) { + int[] results = new int[numDice]; + + for (int index = 0; index < numDice; index++) { + results[index] = contained.roll(rng); + } + + return results; + } + + @Override + public Die[] contained() { + Die[] results = new Die[numDice]; + + for (int index = 0; index < numDice; index++) { + results[index] = contained; + } + + return results; + } + + @Override + public String toString() { + return String.format("%d%s", numDice, contained); + } + + @Override + public int hashCode() { + return Objects.hash(contained, numDice); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) return true; + if (obj == null) return false; + if (getClass() != obj.getClass()) return false; + + TimesDiePool other = (TimesDiePool) obj; + + return Objects.equals(contained, other.contained) && numDice == other.numDice; + } +}
\ No newline at end of file |
