summaryrefslogtreecommitdiff
path: root/dice/src/main/java/bjc/dicelang/neodice/Die.java
diff options
context:
space:
mode:
authorBen Culkin <scorpress@gmail.com>2020-11-21 18:03:31 -0500
committerBen Culkin <scorpress@gmail.com>2020-11-21 18:03:31 -0500
commitd92c0ed766188f614ed4077395881549132305d0 (patch)
tree85466b1a7534c6faade13ac4cce427e3aefd554f /dice/src/main/java/bjc/dicelang/neodice/Die.java
parent84f0803068ba50bcba67de42634a4bf3d0e9e170 (diff)
Some more work on the new dice implementation
Diffstat (limited to 'dice/src/main/java/bjc/dicelang/neodice/Die.java')
-rw-r--r--dice/src/main/java/bjc/dicelang/neodice/Die.java75
1 files changed, 75 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
index a331e4e..a986ca2 100644
--- a/dice/src/main/java/bjc/dicelang/neodice/Die.java
+++ b/dice/src/main/java/bjc/dicelang/neodice/Die.java
@@ -1,8 +1,11 @@
package bjc.dicelang.neodice;
import java.util.*;
+import java.util.function.*;
import java.util.stream.*;
+import bjc.esodata.*;
+
/**
* Represents a single polyhedral die.
* @author Ben Culkin
@@ -31,6 +34,30 @@ public interface Die {
};
/**
+ * Returns a die which will reroll this die as long as the provided condition is true.
+ *
+ * @param condition The condition to reroll the die on.
+ *
+ * @return A die that rerolls when the given condition is met.
+ */
+ default Die reroll(IntPredicate condition) {
+ return new RerollDie(this, condition, (lst) -> lst.get(lst.size()));
+ }
+
+ /**
+ * Returns a die which will reroll this die up to a specified number of times,
+ * as long as the provided condition is true.
+ *
+ * @param condition The condition to reroll the die on.
+ * @param limit The maximum number of times to reroll the die.
+ *
+ * @return A die that rerolls when the given condition is met.
+ */
+ default Die reroll(IntPredicate condition, int limit) {
+ return new RerollDie(this, condition, (lst) -> lst.get(lst.size()), limit);
+ }
+
+ /**
* Create an iterator which gives rolls of this dice.
*
* @param rng The source for random numbers.
@@ -93,4 +120,52 @@ final class TimesDiePool implements DiePool {
return Objects.equals(contained, other.contained) && numDice == other.numDice;
}
+}
+
+final class RerollDie implements Die {
+ private final Die contained;
+
+ private final IntPredicate condition;
+ private final Function<MinMaxList<Integer>, Integer> chooser;
+
+ private int limit = Integer.MAX_VALUE;
+
+
+ public RerollDie(Die contained, IntPredicate condition,
+ Function<MinMaxList<Integer>, Integer> chooser) {
+ this.contained = contained;
+
+ this.condition = condition;
+ this.chooser = chooser;
+ }
+
+ public RerollDie(Die contained, IntPredicate condition,
+ Function<MinMaxList<Integer>, Integer> chooser, int limit) {
+ this.contained = contained;
+
+ this.condition = condition;
+ this.chooser = chooser;
+
+ this.limit = limit;
+ }
+
+ @Override
+ public int roll(Random rng) {
+ int roll = contained.roll(rng);
+
+ MinMaxList<Integer> newRolls = new MinMaxList<Integer>(
+ Comparator.naturalOrder(), roll);
+
+ int rerollCount = 0;
+ while (condition.test(roll) && rerollCount < limit) {
+ roll = contained.roll(rng);
+ newRolls.add(roll);
+
+ rerollCount += 1;
+ }
+
+ return chooser.apply(newRolls);
+ }
+
+ // No toString, because IntPredicate can't be converted to a string
} \ No newline at end of file