diff options
| author | Benjamin J. Culkin <bjculkin@mix.wvu.edu> | 2017-10-25 12:30:48 -0300 |
|---|---|---|
| committer | Benjamin J. Culkin <bjculkin@mix.wvu.edu> | 2017-10-25 12:30:48 -0300 |
| commit | 6b76d2ff5a3df3931c0983d915eed33e83e892e0 (patch) | |
| tree | e2d959c0d59a6cae412dafee307cbd25b99a1a9d /base/src/bjc/dicelang/dice/CompoundingDie.java | |
| parent | 80b48c9fcba9c7c9d64501a6e2ac7c5b98fcd1d2 (diff) | |
Move dice to new module
Diffstat (limited to 'base/src/bjc/dicelang/dice/CompoundingDie.java')
| -rw-r--r-- | base/src/bjc/dicelang/dice/CompoundingDie.java | 115 |
1 files changed, 0 insertions, 115 deletions
diff --git a/base/src/bjc/dicelang/dice/CompoundingDie.java b/base/src/bjc/dicelang/dice/CompoundingDie.java deleted file mode 100644 index 023282e..0000000 --- a/base/src/bjc/dicelang/dice/CompoundingDie.java +++ /dev/null @@ -1,115 +0,0 @@ -package bjc.dicelang.dice;
-
-import java.util.function.Predicate;
-
-/**
- * Implements a compounding die.
- *
- * This means that the source will be rolled, and then more single rolls will be
- * added while it meets a qualification.
- *
- * @author Ben Culkin
- */
-public class CompoundingDie implements Die {
- /* The source die to compound. */
- private final Die source;
-
- /* The predicate that marks when to compound. */
- private final Predicate<Long> compoundOn;
- /* The string version of the predicate, if one exists. */
- private final String compoundPattern;
-
- /**
- * Create a new compounding die with no pattern.
- *
- * @param src
- * The die to compound from
- * @param compound
- * The conditions to compound on
- */
- public CompoundingDie(final Die src, final Predicate<Long> compound) {
- this(src, compound, null);
- }
-
- /**
- * Create a new compounding die with a specified pattern.
- *
- * @param src
- * The die to compound from
- * @param compound
- * The conditions to compound on
- * @param patt
- * The string pattern the condition came from, for
- * printing
- */
- public CompoundingDie(final Die src, final Predicate<Long> compound, final String patt) {
- source = src;
-
- compoundOn = compound;
- compoundPattern = patt;
- }
-
- @Override
- public boolean canOptimize() {
- if (source.canOptimize()) {
- /* We can only be optimized for a result of zero. */
- return source.optimize() == 0;
- }
-
- return false;
- }
-
- @Override
- public long optimize() {
- /* If we can be optimized, its to zero. */
- return 0;
- }
-
- @Override
- public long roll() {
- /* The current result. */
- long res = source.roll();
- /* The last result. */
- long oldRes = res;
-
- while (compoundOn.test(oldRes)) {
- /* Compound while the result should be compounded. */
- oldRes = source.rollSingle();
-
- /* Accumulate. */
- res += oldRes;
- }
-
- return res;
- }
-
- @Override
- public long rollSingle() {
- /* Just compound on an initial single role. */
- long res = source.rollSingle();
- /* The last result. */
- long oldRes = res;
-
- while (compoundOn.test(oldRes)) {
- /* Compound while the result should be compounded. */
- oldRes = source.rollSingle();
-
- /* Accumulate. */
- res += oldRes;
- }
-
- return res;
- }
-
- @Override
- public String toString() {
- String sourceString = source.toString();
-
- /* Can't print a parseable version. */
- if (compoundPattern == null) {
- return String.format("%s!!<complex-pattern>", sourceString);
- }
-
- return String.format("%s!!%s", sourceString, compoundPattern);
- }
-}
|
