summaryrefslogtreecommitdiff
path: root/dice/src/main/java/bjc/dicelang/dicev2/ComputedDie.java
diff options
context:
space:
mode:
authorBenjamin J. Culkin <bjculkin@mix.wvu.edu>2018-06-02 17:32:24 -0300
committerBenjamin J. Culkin <bjculkin@mix.wvu.edu>2018-06-02 17:32:24 -0300
commit775394cf6b4a3c6a7e7345affeb45bb34f6e4517 (patch)
treea193472620f796b3701fa979cba84d4361eef0c0 /dice/src/main/java/bjc/dicelang/dicev2/ComputedDie.java
parent09f161528619f9396aa07c784c15bc1d33f5c047 (diff)
Finish die rewrite
This adds all of the remaining missing die modifiers, as well as a few base die types that were missing.
Diffstat (limited to 'dice/src/main/java/bjc/dicelang/dicev2/ComputedDie.java')
-rw-r--r--dice/src/main/java/bjc/dicelang/dicev2/ComputedDie.java75
1 files changed, 75 insertions, 0 deletions
diff --git a/dice/src/main/java/bjc/dicelang/dicev2/ComputedDie.java b/dice/src/main/java/bjc/dicelang/dicev2/ComputedDie.java
new file mode 100644
index 0000000..89405fe
--- /dev/null
+++ b/dice/src/main/java/bjc/dicelang/dicev2/ComputedDie.java
@@ -0,0 +1,75 @@
+package bjc.dicelang.dicev2;
+
+import java.util.Random;
+import java.util.function.IntSupplier;
+
+public class ComputedDie extends Die {
+ public final IntSupplier numDice;
+ public final IntSupplier numSides;
+
+ public final boolean rerollSides;
+
+ public ComputedDie(IntSupplier numDice, IntSupplier numSides) {
+ this(numDice, numSides, false);
+ }
+
+ public ComputedDie(IntSupplier numDice, IntSupplier numSides, boolean rerollSides) {
+ super();
+
+ this.numDice = numDice;
+ this.numSides = numSides;
+
+ this.rerollSides = rerollSides;
+ }
+
+ public ComputedDie(Random rnd, IntSupplier numDice, IntSupplier numSides) {
+ this(rnd, numDice, numSides, false);
+ }
+
+ public ComputedDie(Random rnd, IntSupplier numDice, IntSupplier numSides, boolean rerollSides) {
+ super(rnd);
+
+ this.numDice = numDice;
+ this.numSides = numSides;
+
+ this.rerollSides = rerollSides;
+ }
+
+ public long[] roll() {
+ int target = numDice.getAsInt();
+ int sides = numSides.getAsInt();
+
+ long[] res = new long[target];
+
+ for(int i = 0; i < target; i++) {
+ res[i] = rng.nextInt(sides) + 1;
+
+ if(rerollSides) sides = numSides.getAsInt();
+ }
+
+ return res;
+ }
+
+ public long rollSingle() {
+ return rng.nextInt(numSides.getAsInt());
+ }
+
+ /*
+ * @NOTE
+ *
+ * :UnoptimizableDice
+ *
+ * Here, we assume that we can't optimize because we have no way
+ * of knowing that our suppliers will always return the same
+ * thing. As a matter of fact, they almost always will have that
+ * behavior, otherwise you wouldn't need ComputedDie.
+ */
+
+ public boolean canOptimize() {
+ return false;
+ }
+
+ public long optimize() {
+ throw new UnsupportedOperationException("ComputedDie cannot be optimized");
+ }
+}