summaryrefslogtreecommitdiff
path: root/dice
diff options
context:
space:
mode:
Diffstat (limited to 'dice')
-rw-r--r--dice/pom.xml10
-rw-r--r--dice/src/main/java/bjc/dicelang/dice/CompoundDie.java175
-rw-r--r--dice/src/main/java/bjc/dicelang/dice/CompoundingDie.java283
-rw-r--r--dice/src/main/java/bjc/dicelang/dice/DiceBox.java90
-rw-r--r--dice/src/main/java/bjc/dicelang/dice/Die.java78
-rw-r--r--dice/src/main/java/bjc/dicelang/dice/ExplodingDice.java302
-rw-r--r--dice/src/main/java/bjc/dicelang/dice/FudgeDie.java23
-rw-r--r--dice/src/main/java/bjc/dicelang/dice/ListDiceExpression.java19
-rw-r--r--dice/src/main/java/bjc/dicelang/dice/MathDie.java46
-rw-r--r--dice/src/main/java/bjc/dicelang/dice/ScalarDiceExpression.java17
-rw-r--r--dice/src/main/java/bjc/dicelang/dice/ScalarDie.java14
-rw-r--r--dice/src/main/java/bjc/dicelang/dice/SimpleDie.java51
-rw-r--r--dice/src/main/java/bjc/dicelang/dice/SimpleDieList.java42
-rw-r--r--dice/src/test/java/bjc/dicelang/AppTest.java48
14 files changed, 573 insertions, 625 deletions
diff --git a/dice/pom.xml b/dice/pom.xml
index cf53f8a..da9d618 100644
--- a/dice/pom.xml
+++ b/dice/pom.xml
@@ -1,6 +1,7 @@
<?xml version="1.0"?>
-<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
+<project
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
+ xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
@@ -26,5 +27,10 @@
<version>3.8.1</version>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>bjc</groupId>
+ <artifactId>BJC-Utils2</artifactId>
+ <version>1.0.0</version>
+ </dependency>
</dependencies>
</project>
diff --git a/dice/src/main/java/bjc/dicelang/dice/CompoundDie.java b/dice/src/main/java/bjc/dicelang/dice/CompoundDie.java
index b66022c..2c6cec2 100644
--- a/dice/src/main/java/bjc/dicelang/dice/CompoundDie.java
+++ b/dice/src/main/java/bjc/dicelang/dice/CompoundDie.java
@@ -1,91 +1,84 @@
-package bjc.dicelang.dice;
-
-/**
- * A die whose rolls result from concatenating two other rolls together.
- *
- * @author Ben Culkin
- */
-public class CompoundDie implements Die {
- /* The dice that form this die */
- private final Die left;
- private final Die right;
-
- /**
- * Create a new compound die.
- *
- * @param lft
- * The left die
- * @param rght
- * The right die
- */
- public CompoundDie(final Die lft, final Die rght) {
- left = lft;
- right = rght;
- }
-
- @Override
- public boolean canOptimize() {
- return left.canOptimize() && right.canOptimize();
- }
-
- @Override
- public long optimize() {
- long leftOpt = left.optimize();
- long rightOpt = right.optimize();
-
- return Long.parseLong(String.format("%d%d", leftOpt, rightOpt));
- }
-
- @Override
- public long roll() {
- long leftRoll = left.optimize();
- long rightRoll = right.optimize();
-
- return Long.parseLong(String.format("%d%d", leftRoll, rightRoll));
- }
-
- @Override
- public long rollSingle() {
- /* Actually one dice built using two, can't be split. */
- return roll();
- }
-
- @Override
- public String toString() {
- String leftString = left.toString();
- String rightString = right.toString();
-
- return String.format("%sc%s", leftString, rightString);
- }
-
- @Override
- public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result + ((left == null) ? 0 : left.hashCode());
- result = prime * result + ((right == null) ? 0 : right.hashCode());
- return result;
- }
-
- @Override
- public boolean equals(Object obj) {
- if (this == obj)
- return true;
- if (obj == null)
- return false;
- if (getClass() != obj.getClass())
- return false;
- CompoundDie other = (CompoundDie) obj;
- if (left == null) {
- if (other.left != null)
- return false;
- } else if (!left.equals(other.left))
- return false;
- if (right == null) {
- if (other.right != null)
- return false;
- } else if (!right.equals(other.right))
- return false;
- return true;
- }
-}
+package bjc.dicelang.dice;
+
+/**
+ * A die whose rolls result from concatenating two other rolls together.
+ *
+ * @author Ben Culkin
+ */
+public class CompoundDie implements Die {
+ /* The dice that form this die */
+ private final Die left;
+ private final Die right;
+
+ /**
+ * Create a new compound die.
+ *
+ * @param lft
+ * The left die
+ * @param rght
+ * The right die
+ */
+ public CompoundDie(final Die lft, final Die rght) {
+ left = lft;
+ right = rght;
+ }
+
+ @Override
+ public boolean canOptimize() {
+ return left.canOptimize() && right.canOptimize();
+ }
+
+ @Override
+ public long optimize() {
+ long leftOpt = left.optimize();
+ long rightOpt = right.optimize();
+
+ return Long.parseLong(String.format("%d%d", leftOpt, rightOpt));
+ }
+
+ @Override
+ public long roll() {
+ long leftRoll = left.optimize();
+ long rightRoll = right.optimize();
+
+ return Long.parseLong(String.format("%d%d", leftRoll, rightRoll));
+ }
+
+ @Override
+ public long rollSingle() {
+ /* Actually one dice built using two, can't be split. */
+ return roll();
+ }
+
+ @Override
+ public String toString() {
+ String leftString = left.toString();
+ String rightString = right.toString();
+
+ return String.format("%sc%s", leftString, rightString);
+ }
+
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + ((left == null) ? 0 : left.hashCode());
+ result = prime * result + ((right == null) ? 0 : right.hashCode());
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if(this == obj) return true;
+ if(obj == null) return false;
+ if(getClass() != obj.getClass()) return false;
+ CompoundDie other = (CompoundDie) obj;
+ if(left == null) {
+ if(other.left != null) return false;
+ } else if(!left.equals(other.left)) return false;
+ if(right == null) {
+ if(other.right != null) return false;
+ } else if(!right.equals(other.right)) return false;
+ return true;
+ }
+}
diff --git a/dice/src/main/java/bjc/dicelang/dice/CompoundingDie.java b/dice/src/main/java/bjc/dicelang/dice/CompoundingDie.java
index f4ab510..99a8fb0 100644
--- a/dice/src/main/java/bjc/dicelang/dice/CompoundingDie.java
+++ b/dice/src/main/java/bjc/dicelang/dice/CompoundingDie.java
@@ -1,145 +1,138 @@
-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);
- }
-
- @Override
- public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result + ((compoundPattern == null) ? 0 : compoundPattern.hashCode());
- result = prime * result + ((source == null) ? 0 : source.hashCode());
- return result;
- }
-
- @Override
- public boolean equals(Object obj) {
- if (this == obj)
- return true;
- if (obj == null)
- return false;
- if (getClass() != obj.getClass())
- return false;
- CompoundingDie other = (CompoundingDie) obj;
- if (compoundPattern == null) {
- if (other.compoundPattern != null)
- return false;
- } else if (!compoundPattern.equals(other.compoundPattern))
- return false;
- if (source == null) {
- if (other.source != null)
- return false;
- } else if (!source.equals(other.source))
- return false;
- return true;
- }
-}
+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);
+ }
+
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + ((compoundPattern == null) ? 0 : compoundPattern.hashCode());
+ result = prime * result + ((source == null) ? 0 : source.hashCode());
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if(this == obj) return true;
+ if(obj == null) return false;
+ if(getClass() != obj.getClass()) return false;
+ CompoundingDie other = (CompoundingDie) obj;
+ if(compoundPattern == null) {
+ if(other.compoundPattern != null) return false;
+ } else if(!compoundPattern.equals(other.compoundPattern)) return false;
+ if(source == null) {
+ if(other.source != null) return false;
+ } else if(!source.equals(other.source)) return false;
+ return true;
+ }
+}
diff --git a/dice/src/main/java/bjc/dicelang/dice/DiceBox.java b/dice/src/main/java/bjc/dicelang/dice/DiceBox.java
index b8e1b3d..1bf2f63 100644
--- a/dice/src/main/java/bjc/dicelang/dice/DiceBox.java
+++ b/dice/src/main/java/bjc/dicelang/dice/DiceBox.java
@@ -1,5 +1,7 @@
package bjc.dicelang.dice;
+import bjc.utils.funcutils.StringUtils;
+
import java.util.Random;
import java.util.function.Predicate;
import java.util.regex.Pattern;
@@ -16,20 +18,21 @@ public class DiceBox {
* Parse a die expression from a string.
*
* @param expString
- * The string to parse.
+ * The string to parse.
*
* @return The die expression from the string, or null if it wasn't one
*/
public static DiceExpression parseExpression(final String expString) {
try {
return doParseExpression(expString);
- } catch (Exception ex) {
+ } catch(Exception ex) {
/*
* @TODO 10/08/17 Ben Culkin :DieErrors
*
* :ErrorRefactor
*
- * Use different types of exceptions to provide better error messages.
+ * Use different types of exceptions to provide better
+ * error messages.
*/
String exMessage = ex.getMessage();
@@ -42,34 +45,27 @@ public class DiceBox {
private static DiceExpression doParseExpression(final String expString) {
/* Only bother with valid expressions. */
- if (!isValidExpression(expString)) {
+ if(!isValidExpression(expString)) {
return null;
}
- if (scalarDiePattern.matcher(expString).matches()) {
+ if(scalarDiePattern.matcher(expString).matches()) {
/* Parse scalar die. */
- /*
- * @TODO 10/08/17 Ben Culkin :SubstringIndexOf
- *
- * This substring/index of call should be abstracted into its own method so as
- * to make the code more explanatory and ensure that things like the return code
- * of indexOf are correctly checked.
- */
- final String dieString = expString.substring(0, expString.indexOf('s'));
+ final String dieString = StringUtils.substringTo(expString, "s");
final long lar = Long.parseLong(dieString);
final Die scal = new ScalarDie(lar);
return new ScalarDiceExpression(scal);
- } else if (simpleDiePattern.matcher(expString).matches()) {
+ } else if(simpleDiePattern.matcher(expString).matches()) {
/* Parse simple die groups. */
final String[] dieParts = expString.split("d");
final long right = Long.parseLong(dieParts[1]);
final long left;
- if (dieParts[0].equals("")) {
+ if(dieParts[0].equals("")) {
/* Handle short-form expressions. */
left = 1;
} else {
@@ -79,33 +75,33 @@ public class DiceBox {
final Die scal = new SimpleDie(left, right);
return new ScalarDiceExpression(scal);
- } else if (fudgeDiePattern.matcher(expString).matches()) {
+ } else if(fudgeDiePattern.matcher(expString).matches()) {
/* Parse fudge dice. */
- /* :SubstringIndexOf */
- final String nDice = expString.substring(0, expString.indexOf('d'));
+ final String nDice = StringUtils.substringTo(expString, "d");
final Die fudge = new FudgeDie(Long.parseLong(nDice));
return new ScalarDiceExpression(fudge);
- } else if (compoundDiePattern.matcher(expString).matches()) {
+ } else if(compoundDiePattern.matcher(expString).matches()) {
/* Parse compound die expressions. */
final String[] dieParts = expString.split("c");
/*
* @TODO 10/08/17 :SplitParse
*
- * Should this split string/parse split parts be abstracted into something else
- * that handles doing the splitting correctly, as well as making sure that the
+ * Should this split string/parse split parts be
+ * abstracted into something else that handles doing the
+ * splitting correctly, as well as making sure that the
* resulting DieExpressions are of the right type?
*/
final DiceExpression left = parseExpression(dieParts[0]);
final DiceExpression right = parseExpression(dieParts[1]);
/* :ErrorRefactor */
- if (left.isList()) {
+ if(left.isList()) {
System.out.printf(
"ERROR: Expected a scalar dice expression for lhs of compound die, got a list expression instead (%s)\n",
left);
- } else if (right.isList()) {
+ } else if(right.isList()) {
System.out.printf(
"ERROR: Expected a scalar dice expression for rhs of compound die, got a list expression instead (%s)\n",
right);
@@ -117,7 +113,7 @@ public class DiceBox {
final Die compound = new CompoundDie(lhs, rhs);
return new ScalarDiceExpression(compound);
- } else if (compoundingDiePattern.matcher(expString).matches()) {
+ } else if(compoundingDiePattern.matcher(expString).matches()) {
/* Parse compounding die expressions. */
final String[] dieParts = expString.split("!!");
@@ -129,7 +125,7 @@ public class DiceBox {
final Die scal = new CompoundingDie(die, right, dieParts[1]);
return new ScalarDiceExpression(scal);
- } else if (explodingDiePattern.matcher(expString).matches()) {
+ } else if(explodingDiePattern.matcher(expString).matches()) {
/* Parse exploding die expressions. */
final String[] dieParts = expString.split("!");
@@ -141,7 +137,7 @@ public class DiceBox {
final DieList lst = new ExplodingDice(lhs, right, dieParts[1], false);
return new ListDiceExpression(lst);
- } else if (penetratingDiePattern.matcher(expString).matches()) {
+ } else if(penetratingDiePattern.matcher(expString).matches()) {
/* Parse penetrating die expressions. */
final String[] dieParts = expString.split("p!");
@@ -153,7 +149,7 @@ public class DiceBox {
final DieList lst = new ExplodingDice(lhs, right, dieParts[1], true);
return new ListDiceExpression(lst);
- } else if (diceListPattern.matcher(expString).matches()) {
+ } else if(diceListPattern.matcher(expString).matches()) {
/* Parse simple die lists. */
final String[] dieParts = expString.split("dl");
@@ -177,8 +173,9 @@ public class DiceBox {
/*
* @TODO 10/08/17 Ben Culkin :RegexResource
*
- * These regexes and patterns should be moved to something external, probably
- * using the SimpleProperties-based system that BJC-Utils2 uses.
+ * These regexes and patterns should be moved to something external,
+ * probably using the SimpleProperties-based system that BJC-Utils2
+ * uses.
*/
/* Defines a comparison predicate. */
private static final String comparePoint = "[<>=]\\d+";
@@ -202,7 +199,8 @@ public class DiceBox {
/*
* Defines a fudge die.
*
- * This is like a simple die, but all the die give -1, 0, or 1 as results.
+ * This is like a simple die, but all the die give -1, 0, or 1 as
+ * results.
*/
private static final String fudgeDie = "(?:\\d+)?dF";
private static final Pattern fudgeDiePattern = Pattern.compile("\\A" + fudgeDie + "\\Z");
@@ -226,7 +224,8 @@ public class DiceBox {
/*
* Defines a compounding die.
*
- * This is like an exploding die, but is a single die, not a group of them.
+ * This is like an exploding die, but is a single die, not a group of
+ * them.
*/
private static final String compoundingDie = compoundGroup + "!!" + comparePoint;
private static final Pattern compoundingDiePattern = Pattern.compile("\\A" + compoundingDie + "\\Z");
@@ -243,7 +242,8 @@ public class DiceBox {
/*
* Defines a penetrating die.
*
- * This is like an exploding die, but the exploded result gets a -1 penalty.
+ * This is like an exploding die, but the exploded result gets a -1
+ * penalty.
*/
private static final String penetratingDie = compoundGroup + "!" + comparePoint;
private static final Pattern penetratingDiePattern = Pattern.compile("\\A" + penetratingDie + "\\Z");
@@ -260,29 +260,30 @@ public class DiceBox {
* Check if a given string is a valid die expression.
*
* @param exp
- * The string to check validity of.
+ * The string to check validity of.
*
* @return Whether or not the string is a valid command.
*/
public static boolean isValidExpression(final String exp) {
/*
- * @NOTE Should this matcher/matches expression be abstracted in some way?
+ * @NOTE Should this matcher/matches expression be abstracted in
+ * some way?
*/
- if (scalarDiePattern.matcher(exp).matches()) {
+ if(scalarDiePattern.matcher(exp).matches()) {
return true;
- } else if (simpleDiePattern.matcher(exp).matches()) {
+ } else if(simpleDiePattern.matcher(exp).matches()) {
return true;
- } else if (fudgeDiePattern.matcher(exp).matches()) {
+ } else if(fudgeDiePattern.matcher(exp).matches()) {
return true;
- } else if (compoundDiePattern.matcher(exp).matches()) {
+ } else if(compoundDiePattern.matcher(exp).matches()) {
return true;
- } else if (compoundingDiePattern.matcher(exp).matches()) {
+ } else if(compoundingDiePattern.matcher(exp).matches()) {
return true;
- } else if (explodingDiePattern.matcher(exp).matches()) {
+ } else if(explodingDiePattern.matcher(exp).matches()) {
return true;
- } else if (penetratingDiePattern.matcher(exp).matches()) {
+ } else if(penetratingDiePattern.matcher(exp).matches()) {
return true;
- } else if (diceListPattern.matcher(exp).matches()) {
+ } else if(diceListPattern.matcher(exp).matches()) {
return true;
} else {
return false;
@@ -294,9 +295,10 @@ public class DiceBox {
final long num = Long.parseLong(patt.substring(1));
/*
- * @NOTE Should this be extended in some way, to provide other operators?
+ * @NOTE Should this be extended in some way, to provide other
+ * operators?
*/
- switch (patt.charAt(0)) {
+ switch(patt.charAt(0)) {
case '<':
return (roll) -> (roll < num);
case '=':
diff --git a/dice/src/main/java/bjc/dicelang/dice/Die.java b/dice/src/main/java/bjc/dicelang/dice/Die.java
index 80d9458..31a5903 100644
--- a/dice/src/main/java/bjc/dicelang/dice/Die.java
+++ b/dice/src/main/java/bjc/dicelang/dice/Die.java
@@ -1,39 +1,39 @@
-package bjc.dicelang.dice;
-
-/**
- * Represents one or more dice that produce a scalar result.
- *
- * @author Ben Culkin
- */
-public interface Die {
- /**
- * Can this die be optimized to a single number?
- *
- * @return Whether this die can be optimized or not.
- */
- boolean canOptimize();
-
- /**
- * Optimize this die to a single number.
- *
- * Calling optimize on dice that return false for canOptimize produces undefined
- * behavior
- *
- * @return The optimized form of this die
- */
- long optimize();
-
- /**
- * Roll this die.
- *
- * @return A possible roll of this die
- */
- long roll();
-
- /**
- * Roll only a single portion of this die.
- *
- * @return A possible roll of a single portion of this die.
- */
- long rollSingle();
-}
+package bjc.dicelang.dice;
+
+/**
+ * Represents one or more dice that produce a scalar result.
+ *
+ * @author Ben Culkin
+ */
+public interface Die {
+ /**
+ * Can this die be optimized to a single number?
+ *
+ * @return Whether this die can be optimized or not.
+ */
+ boolean canOptimize();
+
+ /**
+ * Optimize this die to a single number.
+ *
+ * Calling optimize on dice that return false for canOptimize produces
+ * undefined behavior
+ *
+ * @return The optimized form of this die
+ */
+ long optimize();
+
+ /**
+ * Roll this die.
+ *
+ * @return A possible roll of this die
+ */
+ long roll();
+
+ /**
+ * Roll only a single portion of this die.
+ *
+ * @return A possible roll of a single portion of this die.
+ */
+ long rollSingle();
+}
diff --git a/dice/src/main/java/bjc/dicelang/dice/ExplodingDice.java b/dice/src/main/java/bjc/dicelang/dice/ExplodingDice.java
index 2dad73a..6893eb6 100644
--- a/dice/src/main/java/bjc/dicelang/dice/ExplodingDice.java
+++ b/dice/src/main/java/bjc/dicelang/dice/ExplodingDice.java
@@ -1,154 +1,148 @@
-package bjc.dicelang.dice;
-
-import java.util.LinkedList;
-import java.util.List;
-import java.util.function.Predicate;
-
-/**
- * An exploding die.
- *
- * Represents a die list that keeps getting another added die as long as a
- * condition is met.
- *
- * @author Ben Culkin
- */
-public class ExplodingDice implements DieList {
- /* The source die to use. */
- private final Die source;
-
- /* The conditions for exploding. */
- private final Predicate<Long> explodeOn;
- private final String explodePattern;
- /* Whether or not to apply a -1 penalty to explosions. */
- private final boolean explodePenetrates;
-
- /**
- * Create a new exploding die.
- *
- * @param src
- * The source die for exploding.
- * @param explode
- * The condition to explode on.
- */
- public ExplodingDice(final Die src, final Predicate<Long> explode) {
- this(src, explode, null, false);
- }
-
- /**
- * Create a new exploding die that may penetrate.
- *
- * @param src
- * The source die for exploding.
- * @param explode
- * The condition to explode on.
- * @param penetrate
- * Whether or not for explosions to penetrate (-1 to exploded die).
- */
- public ExplodingDice(final Die src, final Predicate<Long> explode, final boolean penetrate) {
- this(src, explode, null, penetrate);
- }
-
- /**
- * Create a new exploding die that may penetrate.
- *
- * @param src
- * The source die for exploding.
- * @param explode
- * The condition to explode on.
- * @param penetrate
- * Whether or not for explosions to penetrate (-1 to exploded die).
- * @param patt
- * The string the condition came from, for printing.
- */
- public ExplodingDice(final Die src, final Predicate<Long> explode, final String patt, final boolean penetrate) {
- source = src;
- explodeOn = explode;
- explodePattern = patt;
- explodePenetrates = penetrate;
- }
-
- @Override
- public boolean canOptimize() {
- return false;
- }
-
- @Override
- public long[] optimize() {
- return new long[0];
- }
-
- @Override
- public long[] roll() {
- final long res = source.roll();
- long oldRes = res;
-
- final List<Long> resList = new LinkedList<>();
- resList.add(res);
-
- while (explodeOn.test(oldRes)) {
- oldRes = source.rollSingle();
-
- if (explodePenetrates) {
- oldRes -= 1;
- }
-
- resList.add(oldRes);
- }
-
- final long resArr[] = new long[resList.size()];
-
- int i = 0;
- for (long rll : resList) {
- resArr[i] = rll;
- i += 1;
- }
-
- return resArr;
- }
-
- @Override
- public String toString() {
- String penString = explodePenetrates ? "p" : "";
- String sourceString = source.toString();
-
- if (explodePattern == null) {
- return String.format("%s%s!<complex-pred>", sourceString, penString);
- }
-
- return String.format("%s%s!%s", sourceString, penString, explodePattern);
- }
-
- @Override
- public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result + ((explodePattern == null) ? 0 : explodePattern.hashCode());
- result = prime * result + (explodePenetrates ? 1231 : 1237);
- result = prime * result + ((source == null) ? 0 : source.hashCode());
- return result;
- }
-
- @Override
- public boolean equals(Object obj) {
- if (this == obj)
- return true;
- if (obj == null)
- return false;
- if (getClass() != obj.getClass())
- return false;
- ExplodingDice other = (ExplodingDice) obj;
- if (explodePattern == null) {
- if (other.explodePattern != null)
- return false;
- } else if (!explodePattern.equals(other.explodePattern))
- return false;
- if (explodePenetrates != other.explodePenetrates)
- return false;
- if (source == null) {
- if (other.source != null)
- return false;
- } else if (!source.equals(other.source))
- return false;
- return true;
- }
-}
+package bjc.dicelang.dice;
+
+import java.util.LinkedList;
+import java.util.List;
+import java.util.function.Predicate;
+
+/**
+ * An exploding die.
+ *
+ * Represents a die list that keeps getting another added die as long as a
+ * condition is met.
+ *
+ * @author Ben Culkin
+ */
+public class ExplodingDice implements DieList {
+ /* The source die to use. */
+ private final Die source;
+
+ /* The conditions for exploding. */
+ private final Predicate<Long> explodeOn;
+ private final String explodePattern;
+ /* Whether or not to apply a -1 penalty to explosions. */
+ private final boolean explodePenetrates;
+
+ /**
+ * Create a new exploding die.
+ *
+ * @param src
+ * The source die for exploding.
+ * @param explode
+ * The condition to explode on.
+ */
+ public ExplodingDice(final Die src, final Predicate<Long> explode) {
+ this(src, explode, null, false);
+ }
+
+ /**
+ * Create a new exploding die that may penetrate.
+ *
+ * @param src
+ * The source die for exploding.
+ * @param explode
+ * The condition to explode on.
+ * @param penetrate
+ * Whether or not for explosions to penetrate (-1 to exploded
+ * die).
+ */
+ public ExplodingDice(final Die src, final Predicate<Long> explode, final boolean penetrate) {
+ this(src, explode, null, penetrate);
+ }
+
+ /**
+ * Create a new exploding die that may penetrate.
+ *
+ * @param src
+ * The source die for exploding.
+ * @param explode
+ * The condition to explode on.
+ * @param penetrate
+ * Whether or not for explosions to penetrate (-1 to exploded
+ * die).
+ * @param patt
+ * The string the condition came from, for printing.
+ */
+ public ExplodingDice(final Die src, final Predicate<Long> explode, final String patt, final boolean penetrate) {
+ source = src;
+ explodeOn = explode;
+ explodePattern = patt;
+ explodePenetrates = penetrate;
+ }
+
+ @Override
+ public boolean canOptimize() {
+ return false;
+ }
+
+ @Override
+ public long[] optimize() {
+ return new long[0];
+ }
+
+ @Override
+ public long[] roll() {
+ final long res = source.roll();
+ long oldRes = res;
+
+ final List<Long> resList = new LinkedList<>();
+ resList.add(res);
+
+ while(explodeOn.test(oldRes)) {
+ oldRes = source.rollSingle();
+
+ if(explodePenetrates) {
+ oldRes -= 1;
+ }
+
+ resList.add(oldRes);
+ }
+
+ final long resArr[] = new long[resList.size()];
+
+ int i = 0;
+ for(long rll : resList) {
+ resArr[i] = rll;
+ i += 1;
+ }
+
+ return resArr;
+ }
+
+ @Override
+ public String toString() {
+ String penString = explodePenetrates ? "p" : "";
+ String sourceString = source.toString();
+
+ if(explodePattern == null) {
+ return String.format("%s%s!<complex-pred>", sourceString, penString);
+ }
+
+ return String.format("%s%s!%s", sourceString, penString, explodePattern);
+ }
+
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + ((explodePattern == null) ? 0 : explodePattern.hashCode());
+ result = prime * result + (explodePenetrates ? 1231 : 1237);
+ result = prime * result + ((source == null) ? 0 : source.hashCode());
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if(this == obj) return true;
+ if(obj == null) return false;
+ if(getClass() != obj.getClass()) return false;
+ ExplodingDice other = (ExplodingDice) obj;
+ if(explodePattern == null) {
+ if(other.explodePattern != null) return false;
+ } else if(!explodePattern.equals(other.explodePattern)) return false;
+ if(explodePenetrates != other.explodePenetrates) return false;
+ if(source == null) {
+ if(other.source != null) return false;
+ } else if(!source.equals(other.source)) return false;
+ return true;
+ }
+}
diff --git a/dice/src/main/java/bjc/dicelang/dice/FudgeDie.java b/dice/src/main/java/bjc/dicelang/dice/FudgeDie.java
index 9ebbee1..088ce2e 100644
--- a/dice/src/main/java/bjc/dicelang/dice/FudgeDie.java
+++ b/dice/src/main/java/bjc/dicelang/dice/FudgeDie.java
@@ -14,7 +14,7 @@ public class FudgeDie implements Die {
* Create a new fudge die.
*
* @param nDice
- * The number of dice to roll.
+ * The number of dice to roll.
*/
public FudgeDie(final long nDice) {
numDice = new ScalarDie(nDice);
@@ -24,7 +24,7 @@ public class FudgeDie implements Die {
* Create a new fudge die.
*
* @param nDice
- * The number of dice to roll.
+ * The number of dice to roll.
*/
public FudgeDie(final Die nDice) {
numDice = nDice;
@@ -46,7 +46,7 @@ public class FudgeDie implements Die {
final long nDice = numDice.roll();
- for (int i = 0; i < nDice; i++) {
+ for(int i = 0; i < nDice; i++) {
res += rollSingle();
}
@@ -75,18 +75,13 @@ public class FudgeDie implements Die {
@Override
public boolean equals(Object obj) {
- if (this == obj)
- return true;
- if (obj == null)
- return false;
- if (getClass() != obj.getClass())
- return false;
+ if(this == obj) return true;
+ if(obj == null) return false;
+ if(getClass() != obj.getClass()) return false;
FudgeDie other = (FudgeDie) obj;
- if (numDice == null) {
- if (other.numDice != null)
- return false;
- } else if (!numDice.equals(other.numDice))
- return false;
+ if(numDice == null) {
+ if(other.numDice != null) return false;
+ } else if(!numDice.equals(other.numDice)) return false;
return true;
}
}
diff --git a/dice/src/main/java/bjc/dicelang/dice/ListDiceExpression.java b/dice/src/main/java/bjc/dicelang/dice/ListDiceExpression.java
index f8ff588..33c1e51 100644
--- a/dice/src/main/java/bjc/dicelang/dice/ListDiceExpression.java
+++ b/dice/src/main/java/bjc/dicelang/dice/ListDiceExpression.java
@@ -11,7 +11,7 @@ public class ListDiceExpression implements DiceExpression {
* Create a list die expression.
*
* @param lst
- * The list value of this expression.
+ * The list value of this expression.
*/
public ListDiceExpression(final DieList lst) {
list = lst;
@@ -42,18 +42,13 @@ public class ListDiceExpression implements DiceExpression {
@Override
public boolean equals(Object obj) {
- if (this == obj)
- return true;
- if (obj == null)
- return false;
- if (getClass() != obj.getClass())
- return false;
+ if(this == obj) return true;
+ if(obj == null) return false;
+ if(getClass() != obj.getClass()) return false;
ListDiceExpression other = (ListDiceExpression) obj;
- if (list == null) {
- if (other.list != null)
- return false;
- } else if (!list.equals(other.list))
- return false;
+ if(list == null) {
+ if(other.list != null) return false;
+ } else if(!list.equals(other.list)) return false;
return true;
}
}
diff --git a/dice/src/main/java/bjc/dicelang/dice/MathDie.java b/dice/src/main/java/bjc/dicelang/dice/MathDie.java
index 348c2d3..e78dc3c 100644
--- a/dice/src/main/java/bjc/dicelang/dice/MathDie.java
+++ b/dice/src/main/java/bjc/dicelang/dice/MathDie.java
@@ -8,9 +8,11 @@ package bjc.dicelang.dice;
*/
public class MathDie implements Die {
/*
- * @TODO 10/08/17 Ben Culkin :MathGeneralize Why do we have the operator types
- * hardcoded, instead of just having a general thing for applying a binary
- * operator to dice? Fix this by changing it to the more general form.
+ * @TODO 10/08/17 Ben Culkin :MathGeneralize
+ *
+ * Why do we have the operator types hardcoded, instead of just having a
+ * general thing for applying a binary operator to dice? Fix this by
+ * changing it to the more general form.
*/
/**
* The types of a math operator.
@@ -28,7 +30,7 @@ public class MathDie implements Die {
@Override
public String toString() {
- switch (this) {
+ switch(this) {
case ADD:
return "+";
@@ -53,13 +55,13 @@ public class MathDie implements Die {
* Create a new math die.
*
* @param op
- * The operator to apply.
+ * The operator to apply.
*
* @param lft
- * The left operand.
+ * The left operand.
*
* @param rght
- * The right operand.
+ * The right operand.
*/
public MathDie(final MathDie.MathOp op, final Die lft, final Die rght) {
type = op;
@@ -74,7 +76,7 @@ public class MathDie implements Die {
}
private long performOp(final long lft, final long rght) {
- switch (type) {
+ switch(type) {
case ADD:
return lft + rght;
@@ -130,25 +132,17 @@ public class MathDie implements Die {
@Override
public boolean equals(Object obj) {
- if (this == obj)
- return true;
- if (obj == null)
- return false;
- if (getClass() != obj.getClass())
- return false;
+ if(this == obj) return true;
+ if(obj == null) return false;
+ if(getClass() != obj.getClass()) return false;
MathDie other = (MathDie) obj;
- if (left == null) {
- if (other.left != null)
- return false;
- } else if (!left.equals(other.left))
- return false;
- if (right == null) {
- if (other.right != null)
- return false;
- } else if (!right.equals(other.right))
- return false;
- if (type != other.type)
- return false;
+ if(left == null) {
+ if(other.left != null) return false;
+ } else if(!left.equals(other.left)) return false;
+ if(right == null) {
+ if(other.right != null) return false;
+ } else if(!right.equals(other.right)) return false;
+ if(type != other.type) return false;
return true;
}
}
diff --git a/dice/src/main/java/bjc/dicelang/dice/ScalarDiceExpression.java b/dice/src/main/java/bjc/dicelang/dice/ScalarDiceExpression.java
index 6951f49..5f57979 100644
--- a/dice/src/main/java/bjc/dicelang/dice/ScalarDiceExpression.java
+++ b/dice/src/main/java/bjc/dicelang/dice/ScalarDiceExpression.java
@@ -25,18 +25,13 @@ public class ScalarDiceExpression implements DiceExpression {
@Override
public boolean equals(Object obj) {
- if (this == obj)
- return true;
- if (obj == null)
- return false;
- if (getClass() != obj.getClass())
- return false;
+ if(this == obj) return true;
+ if(obj == null) return false;
+ if(getClass() != obj.getClass()) return false;
ScalarDiceExpression other = (ScalarDiceExpression) obj;
- if (scalar == null) {
- if (other.scalar != null)
- return false;
- } else if (!scalar.equals(other.scalar))
- return false;
+ if(scalar == null) {
+ if(other.scalar != null) return false;
+ } else if(!scalar.equals(other.scalar)) return false;
return true;
}
diff --git a/dice/src/main/java/bjc/dicelang/dice/ScalarDie.java b/dice/src/main/java/bjc/dicelang/dice/ScalarDie.java
index af6fd60..de15507 100644
--- a/dice/src/main/java/bjc/dicelang/dice/ScalarDie.java
+++ b/dice/src/main/java/bjc/dicelang/dice/ScalarDie.java
@@ -14,7 +14,7 @@ public class ScalarDie implements Die {
* Create a new scalar die with a set value.
*
* @param vl
- * The value to use.
+ * The value to use.
*/
public ScalarDie(final long vl) {
val = vl;
@@ -55,15 +55,11 @@ public class ScalarDie implements Die {
@Override
public boolean equals(Object obj) {
- if (this == obj)
- return true;
- if (obj == null)
- return false;
- if (getClass() != obj.getClass())
- return false;
+ if(this == obj) return true;
+ if(obj == null) return false;
+ if(getClass() != obj.getClass()) return false;
ScalarDie other = (ScalarDie) obj;
- if (val != other.val)
- return false;
+ if(val != other.val) return false;
return true;
}
}
diff --git a/dice/src/main/java/bjc/dicelang/dice/SimpleDie.java b/dice/src/main/java/bjc/dicelang/dice/SimpleDie.java
index de31a6d..34ec059 100644
--- a/dice/src/main/java/bjc/dicelang/dice/SimpleDie.java
+++ b/dice/src/main/java/bjc/dicelang/dice/SimpleDie.java
@@ -14,8 +14,8 @@ public class SimpleDie implements Die {
*
* Rolled once per role, not once for each dice rolled.
*
- * @NOTE Would having some way to roll it once for each dice rolled be useful in
- * any sort of case?
+ * @NOTE Would having some way to roll it once for each dice rolled be
+ * useful in any sort of case?
*/
private final Die diceSize;
@@ -23,10 +23,10 @@ public class SimpleDie implements Die {
* Create a new dice group.
*
* @param nDice
- * The number of dice.
+ * The number of dice.
*
* @param size
- * The size of the dice.
+ * The size of the dice.
*/
public SimpleDie(final long nDice, final long size) {
this(new ScalarDie(nDice), new ScalarDie(size));
@@ -36,10 +36,10 @@ public class SimpleDie implements Die {
* Create a new dice group.
*
* @param nDice
- * The number of dice.
+ * The number of dice.
*
* @param size
- * The size of the dice.
+ * The size of the dice.
*/
public SimpleDie(final Die nDice, final long size) {
this(nDice, new ScalarDie(size));
@@ -49,10 +49,10 @@ public class SimpleDie implements Die {
* Create a new dice group.
*
* @param nDice
- * The number of dice.
+ * The number of dice.
*
* @param size
- * The size of the dice.
+ * The size of the dice.
*/
public SimpleDie(final long nDice, final Die size) {
this(new ScalarDie(nDice), size);
@@ -62,10 +62,10 @@ public class SimpleDie implements Die {
* Create a new dice group.
*
* @param nDice
- * The number of dice.
+ * The number of dice.
*
* @param size
- * The size of the dice.
+ * The size of the dice.
*/
public SimpleDie(final Die nDice, final Die size) {
numDice = nDice;
@@ -74,7 +74,7 @@ public class SimpleDie implements Die {
@Override
public boolean canOptimize() {
- if (diceSize.canOptimize() && diceSize.optimize() <= 1) {
+ if(diceSize.canOptimize() && diceSize.optimize() <= 1) {
return numDice.canOptimize();
}
@@ -85,7 +85,7 @@ public class SimpleDie implements Die {
public long optimize() {
final long optSize = diceSize.optimize();
- if (optSize == 0) {
+ if(optSize == 0) {
return 0;
}
@@ -99,7 +99,7 @@ public class SimpleDie implements Die {
final long nDice = numDice.roll();
final long dSize = diceSize.roll();
- for (int i = 0; i < nDice; i++) {
+ for(int i = 0; i < nDice; i++) {
total += Math.abs(DiceBox.rng.nextLong()) % dSize + 1;
}
@@ -127,23 +127,16 @@ public class SimpleDie implements Die {
@Override
public boolean equals(Object obj) {
- if (this == obj)
- return true;
- if (obj == null)
- return false;
- if (getClass() != obj.getClass())
- return false;
+ if(this == obj) return true;
+ if(obj == null) return false;
+ if(getClass() != obj.getClass()) return false;
SimpleDie other = (SimpleDie) obj;
- if (diceSize == null) {
- if (other.diceSize != null)
- return false;
- } else if (!diceSize.equals(other.diceSize))
- return false;
- if (numDice == null) {
- if (other.numDice != null)
- return false;
- } else if (!numDice.equals(other.numDice))
- return false;
+ if(diceSize == null) {
+ if(other.diceSize != null) return false;
+ } else if(!diceSize.equals(other.diceSize)) return false;
+ if(numDice == null) {
+ if(other.numDice != null) return false;
+ } else if(!numDice.equals(other.numDice)) return false;
return true;
}
}
diff --git a/dice/src/main/java/bjc/dicelang/dice/SimpleDieList.java b/dice/src/main/java/bjc/dicelang/dice/SimpleDieList.java
index 4e8c51a..f3af08b 100644
--- a/dice/src/main/java/bjc/dicelang/dice/SimpleDieList.java
+++ b/dice/src/main/java/bjc/dicelang/dice/SimpleDieList.java
@@ -5,10 +5,13 @@ package bjc.dicelang.dice;
*
* @author EVE
*
+ */
+
+/*
* @TODO 10/08/17 Ben Culkin :DieListGeneralize
*
- * DieList in general should be changed to be able to be constructed from
- * an arbitrary die using rollSingle and things like that.
+ * DieList in general should be changed to be able to be constructed from an
+ * arbitrary die using rollSingle and things like that.
*/
public class SimpleDieList implements DieList {
/* The number of dice to roll. */
@@ -24,10 +27,10 @@ public class SimpleDieList implements DieList {
* Create a new list of dice.
*
* @param nDice
- * The number of dice in the list.
+ * The number of dice in the list.
*
* @param sze
- * The size of dice in the list.
+ * The size of dice in the list.
*/
public SimpleDieList(final Die nDice, final Die sze) {
numDice = nDice;
@@ -36,7 +39,7 @@ public class SimpleDieList implements DieList {
@Override
public boolean canOptimize() {
- if (size.canOptimize() && size.optimize() <= 1) {
+ if(size.canOptimize() && size.optimize() <= 1) {
return numDice.canOptimize();
}
@@ -50,7 +53,7 @@ public class SimpleDieList implements DieList {
final long[] ret = new long[sze];
- for (int i = 0; i < sze; i++) {
+ for(int i = 0; i < sze; i++) {
ret[i] = res;
}
@@ -63,7 +66,7 @@ public class SimpleDieList implements DieList {
final long[] ret = new long[num];
- for (int i = 0; i < num; i++) {
+ for(int i = 0; i < num; i++) {
ret[i] = size.roll();
}
@@ -86,23 +89,16 @@ public class SimpleDieList implements DieList {
@Override
public boolean equals(Object obj) {
- if (this == obj)
- return true;
- if (obj == null)
- return false;
- if (getClass() != obj.getClass())
- return false;
+ if(this == obj) return true;
+ if(obj == null) return false;
+ if(getClass() != obj.getClass()) return false;
SimpleDieList other = (SimpleDieList) obj;
- if (numDice == null) {
- if (other.numDice != null)
- return false;
- } else if (!numDice.equals(other.numDice))
- return false;
- if (size == null) {
- if (other.size != null)
- return false;
- } else if (!size.equals(other.size))
- return false;
+ if(numDice == null) {
+ if(other.numDice != null) return false;
+ } else if(!numDice.equals(other.numDice)) return false;
+ if(size == null) {
+ if(other.size != null) return false;
+ } else if(!size.equals(other.size)) return false;
return true;
}
}
diff --git a/dice/src/test/java/bjc/dicelang/AppTest.java b/dice/src/test/java/bjc/dicelang/AppTest.java
index 6ffe98a..210aec7 100644
--- a/dice/src/test/java/bjc/dicelang/AppTest.java
+++ b/dice/src/test/java/bjc/dicelang/AppTest.java
@@ -7,32 +7,28 @@ import junit.framework.TestSuite;
/**
* Unit test for simple App.
*/
-public class AppTest
- extends TestCase
-{
- /**
- * Create the test case
- *
- * @param testName name of the test case
- */
- public AppTest( String testName )
- {
- super( testName );
- }
+public class AppTest extends TestCase {
+ /**
+ * Create the test case
+ *
+ * @param testName
+ * name of the test case
+ */
+ public AppTest(String testName) {
+ super(testName);
+ }
- /**
- * @return the suite of tests being tested
- */
- public static Test suite()
- {
- return new TestSuite( AppTest.class );
- }
+ /**
+ * @return the suite of tests being tested
+ */
+ public static Test suite() {
+ return new TestSuite(AppTest.class);
+ }
- /**
- * Rigourous Test :-)
- */
- public void testApp()
- {
- assertTrue( true );
- }
+ /**
+ * Rigourous Test :-)
+ */
+ public void testApp() {
+ assertTrue(true);
+ }
}