summaryrefslogtreecommitdiff
path: root/dice/src/main/java/bjc/dicelang/neodice/die
diff options
context:
space:
mode:
Diffstat (limited to 'dice/src/main/java/bjc/dicelang/neodice/die')
-rw-r--r--dice/src/main/java/bjc/dicelang/neodice/die/PolyhedralDie.java46
-rw-r--r--dice/src/main/java/bjc/dicelang/neodice/die/RerollDie.java55
2 files changed, 101 insertions, 0 deletions
diff --git a/dice/src/main/java/bjc/dicelang/neodice/die/PolyhedralDie.java b/dice/src/main/java/bjc/dicelang/neodice/die/PolyhedralDie.java
new file mode 100644
index 0000000..c1bb2ac
--- /dev/null
+++ b/dice/src/main/java/bjc/dicelang/neodice/die/PolyhedralDie.java
@@ -0,0 +1,46 @@
+package bjc.dicelang.neodice.die;
+
+import java.util.*;
+
+import bjc.dicelang.neodice.*;
+
+public class PolyhedralDie implements Die {
+ private final int sides;
+
+ public PolyhedralDie(int sides) {
+ this.sides = sides;
+ }
+
+ @Override
+ public int roll(Random rng) {
+ // Dice are one-based, not zero-based.
+ return rng.nextInt(sides) + 1;
+ }
+
+ @Override
+ public String toString() {
+ return String.format("d%d", sides);
+ }
+
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + sides;
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) return true;
+ if (obj == null) return false;
+ if (getClass() != obj.getClass()) return false;
+
+ PolyhedralDie other = (PolyhedralDie) obj;
+
+ if (sides != other.sides) return false;
+ else return true;
+ }
+
+
+} \ No newline at end of file
diff --git a/dice/src/main/java/bjc/dicelang/neodice/die/RerollDie.java b/dice/src/main/java/bjc/dicelang/neodice/die/RerollDie.java
new file mode 100644
index 0000000..3cd369f
--- /dev/null
+++ b/dice/src/main/java/bjc/dicelang/neodice/die/RerollDie.java
@@ -0,0 +1,55 @@
+package bjc.dicelang.neodice.die;
+
+import java.util.*;
+import java.util.function.*;
+
+import bjc.dicelang.neodice.*;
+import bjc.esodata.*;
+
+public 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