From 1cf770b4662fed523513a1c73b63c56d9552d8f1 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Wed, 27 Jul 2016 16:30:39 -0400 Subject: General cleanliness --- .../java/bjc/dicelang/BindingDiceExpression.java | 28 ++++---- .../src/main/java/bjc/dicelang/ComplexDice.java | 36 ++++++---- .../src/main/java/bjc/dicelang/CompoundDice.java | 4 +- .../java/bjc/dicelang/DiceExpressionBuilder.java | 12 ++-- .../main/java/bjc/dicelang/IDiceExpression.java | 25 +++++-- .../java/bjc/dicelang/OperatorDiceExpression.java | 4 +- .../java/bjc/dicelang/ScalarDiceExpression.java | 84 ---------------------- 7 files changed, 70 insertions(+), 123 deletions(-) delete mode 100644 dice-lang/src/main/java/bjc/dicelang/ScalarDiceExpression.java (limited to 'dice-lang/src/main/java') diff --git a/dice-lang/src/main/java/bjc/dicelang/BindingDiceExpression.java b/dice-lang/src/main/java/bjc/dicelang/BindingDiceExpression.java index 90ca5a4..6a030e3 100644 --- a/dice-lang/src/main/java/bjc/dicelang/BindingDiceExpression.java +++ b/dice-lang/src/main/java/bjc/dicelang/BindingDiceExpression.java @@ -29,20 +29,22 @@ public class BindingDiceExpression implements IDiceExpression { * ReferenceDiceExpression * @param right * The right side to bind to the name - * @param env + * @param enviroment * The enviroment to bind into */ public BindingDiceExpression(IDiceExpression left, - IDiceExpression right, Map env) { + IDiceExpression right, + Map enviroment) { if (!(left instanceof ReferenceDiceExpression)) { throw new UnsupportedOperationException( - "Binding to non-references is unsupported." + "Error: Binding an expression to something that is not a variable reference," + + " or array thereof. is unsupported." + " Problematic expression is " + left); } String varName = ((ReferenceDiceExpression) left).getName(); - initialize(varName, right, env); + initialize(varName, right, enviroment); } /** @@ -50,22 +52,22 @@ public class BindingDiceExpression implements IDiceExpression { * * @param name * The name of the variable to bind - * @param exp + * @param expression * The expression to bind to the variable - * @param env + * @param enviroment * The enviroment to bind it in */ - public BindingDiceExpression(String name, IDiceExpression exp, - Map env) { - initialize(name, exp, env); + public BindingDiceExpression(String name, IDiceExpression expression, + Map enviroment) { + initialize(name, expression, enviroment); } - private void initialize(String name, IDiceExpression exp, - Map env) { + private void initialize(String name, IDiceExpression expr, + Map enviroment) { this.variableName = name; - this.expression = exp; + this.expression = expr; - env.put(name, exp); + enviroment.put(name, expr); } /* diff --git a/dice-lang/src/main/java/bjc/dicelang/ComplexDice.java b/dice-lang/src/main/java/bjc/dicelang/ComplexDice.java index 9450584..9bf191f 100644 --- a/dice-lang/src/main/java/bjc/dicelang/ComplexDice.java +++ b/dice-lang/src/main/java/bjc/dicelang/ComplexDice.java @@ -16,27 +16,29 @@ public class ComplexDice implements IDiceExpression { * @return A dice group parsed from the string */ public static IDiceExpression fromString(String expression) { - /* - * Split it on the dice type marker - */ + // Handle the case where someone passes us a simple expression + // containing a single die + if (!expression.contains("d")) { + return new Die(Integer.parseInt(expression)); + } + // Split it on the dice type marker + String[] strangs = expression.split("d"); try { - /* - * Create the actual dice - */ + // Create the actual group of dice return new ComplexDice( new ScalarDie(Integer.parseInt(strangs[0])), new Die(Integer.parseInt(strangs[1]))); } catch (@SuppressWarnings("unused") NumberFormatException nfex) { // We don't care about details - /* - * Tell the user the expression is invalid - */ + + // Tell the user the expression is invalid throw new IllegalArgumentException( - "Attempted to create a dice using something that's not" - + " an integer: " + strangs[0] + " and " - + strangs[1] + " are likely culprits."); + "Attempted to create a set of dice using invalid arguments." + + " They must be integers. " + strangs[0] + + " and " + strangs[1] + + " are likely culprits."); } } @@ -46,7 +48,7 @@ public class ComplexDice implements IDiceExpression { private IDiceExpression die; /** - * The number of the specified die to roll + * The number of the particular die to roll */ private IDiceExpression nDice; @@ -78,6 +80,8 @@ public class ComplexDice implements IDiceExpression { @Override public boolean canOptimize() { + // Can only optimize this dice group if both components can be + // optimized and the die itself has only one value if (nDice.canOptimize() && die.canOptimize()) { return die.optimize() == 1; } @@ -89,7 +93,9 @@ public class ComplexDice implements IDiceExpression { public int optimize() { if (!canOptimize()) { throw new UnsupportedOperationException( - "This complex dice cannot be optimized"); + "This complex dice cannot be optimized. " + + "Both the dice to be rolled and the number of" + + " dice must be optimizable."); } return nDice.optimize(); @@ -115,6 +121,7 @@ public class ComplexDice implements IDiceExpression { + "The problematic expression is " + nDice); } + // Roll all the dice and combine them for (int i = 0; i < nRoll; i++) { res += die.roll(); } @@ -129,6 +136,7 @@ public class ComplexDice implements IDiceExpression { */ @Override public String toString() { + // Print simple dice groups in a much clearer manner if (nDice instanceof ScalarDie && die instanceof Die) { return nDice.toString() + die.toString(); } diff --git a/dice-lang/src/main/java/bjc/dicelang/CompoundDice.java b/dice-lang/src/main/java/bjc/dicelang/CompoundDice.java index 8d2aadd..704e4cd 100644 --- a/dice-lang/src/main/java/bjc/dicelang/CompoundDice.java +++ b/dice-lang/src/main/java/bjc/dicelang/CompoundDice.java @@ -65,7 +65,9 @@ public class CompoundDice implements IDiceExpression { public int optimize() { if (!canOptimize()) { throw new UnsupportedOperationException( - "Cannot optimize this compound dice"); + "Cannot optimize this compound dice. " + + "Both component dice must be optimizable" + + " to optimize a compound dice"); } return Integer diff --git a/dice-lang/src/main/java/bjc/dicelang/DiceExpressionBuilder.java b/dice-lang/src/main/java/bjc/dicelang/DiceExpressionBuilder.java index e76a23a..af856a6 100644 --- a/dice-lang/src/main/java/bjc/dicelang/DiceExpressionBuilder.java +++ b/dice-lang/src/main/java/bjc/dicelang/DiceExpressionBuilder.java @@ -59,7 +59,8 @@ public class DiceExpressionBuilder { * @return A dice expression adding a scalar to this */ public DiceExpressionBuilder add(int num) { - baking = new ScalarDiceExpression(baking, num, ADD); + baking = new OperatorDiceExpression(baking, new ScalarDie(num), + ADD); return this; } @@ -92,7 +93,8 @@ public class DiceExpressionBuilder { * @return A dice expression dividing a scalar from this */ public DiceExpressionBuilder divide(int num) { - baking = new ScalarDiceExpression(baking, num, DIVIDE); + baking = new OperatorDiceExpression(baking, new ScalarDie(num), + DIVIDE); return this; } @@ -116,7 +118,8 @@ public class DiceExpressionBuilder { * @return A dice expression multiplying a scalar to this */ public DiceExpressionBuilder multiply(int num) { - baking = new ScalarDiceExpression(baking, num, MULTIPLY); + baking = new OperatorDiceExpression(baking, new ScalarDie(num), + MULTIPLY); return this; } @@ -140,7 +143,8 @@ public class DiceExpressionBuilder { * @return A dice expression adding a scalar to this */ public DiceExpressionBuilder subtract(int num) { - baking = new ScalarDiceExpression(baking, num, SUBTRACT); + baking = new OperatorDiceExpression(baking, new ScalarDie(num), + SUBTRACT); return this; } } diff --git a/dice-lang/src/main/java/bjc/dicelang/IDiceExpression.java b/dice-lang/src/main/java/bjc/dicelang/IDiceExpression.java index 4585f75..0ee2127 100644 --- a/dice-lang/src/main/java/bjc/dicelang/IDiceExpression.java +++ b/dice-lang/src/main/java/bjc/dicelang/IDiceExpression.java @@ -11,26 +11,41 @@ import bjc.utils.funcutils.StringUtils; @FunctionalInterface public interface IDiceExpression { /** - * Parse this node into an expression + * Parse a string into an expression. + * + * It can accept the following types of expressions + *
    + *
  • Simple integers - '2'
  • + *
  • Simple dice - 'd6'
  • + *
  • Groups of simple dice - '2d6'
  • + *
  • Number concatenation - '2c6'
  • + *
  • Dice concatenation - '1d10c1d10
  • + *
+ * + * Dice concatenation is like using 2 d10s to emulate a d100, so + * instead of adding them, it reads them side by side. * * @param expression * The string to convert to an expression * - * @return The string in expression form + * @return The string, converted into expression form */ static IDiceExpression toExpression(String expression) { String literalData = expression; if (StringUtils.containsInfixOperator(literalData, "c")) { + // Parse a compound die String[] strangs = literalData.split("c"); return new CompoundDice(strangs); } else if (StringUtils.containsInfixOperator(literalData, "d")) { - /* - * Handle dice groups - */ + // Handle groups of similiar dice return ComplexDice.fromString(literalData); + } else if (literalData.startsWith("d")) { + // Handle people who put 'd6' instead of '1d6' + return new Die(Integer.parseInt(literalData.substring(1))); } else { + // Parse a scalar number try { return new ScalarDie(Integer.parseInt(literalData)); } catch (NumberFormatException nfex) { diff --git a/dice-lang/src/main/java/bjc/dicelang/OperatorDiceExpression.java b/dice-lang/src/main/java/bjc/dicelang/OperatorDiceExpression.java index 9230680..f86773d 100644 --- a/dice-lang/src/main/java/bjc/dicelang/OperatorDiceExpression.java +++ b/dice-lang/src/main/java/bjc/dicelang/OperatorDiceExpression.java @@ -76,8 +76,8 @@ public class OperatorDiceExpression implements IDiceExpression { } default: throw new IllegalArgumentException( - "Got passed a invalid ScalarExpressionType " - + expressionType + ". WAT"); + "Got passed a invalid ScalarExpressionType (" + + expressionType + "). WAT"); } } diff --git a/dice-lang/src/main/java/bjc/dicelang/ScalarDiceExpression.java b/dice-lang/src/main/java/bjc/dicelang/ScalarDiceExpression.java deleted file mode 100644 index 50315e9..0000000 --- a/dice-lang/src/main/java/bjc/dicelang/ScalarDiceExpression.java +++ /dev/null @@ -1,84 +0,0 @@ -package bjc.dicelang; - -/** - * A dice expression that combines a scalar and a dice - * - * @author ben - * - */ -public class ScalarDiceExpression implements IDiceExpression { - /** - * The operation to combine with - */ - private DiceExpressionType expressionType; - - /** - * The expression to be combined - */ - private IDiceExpression expression; - - /** - * The scalar to be combined - */ - private int scalar; - - /** - * Create a dice expression with a scalar - * - * @param expr - * The dice to use - * @param scalr - * The scalar to use - * @param type - * The operation to combine with - */ - public ScalarDiceExpression(IDiceExpression expr, int scalr, - DiceExpressionType type) { - expression = expr; - scalar = scalr; - expressionType = type; - } - - /* - * (non-Javadoc) - * - * @see bjc.utils.dice.IDiceExpression#roll() - */ - @Override - public int roll() { - switch (expressionType) { - case ADD: - return expression.roll() + scalar; - case SUBTRACT: - return expression.roll() - scalar; - case MULTIPLY: - return expression.roll() * scalar; - case DIVIDE: - try { - return expression.roll() / scalar; - } catch (ArithmeticException aex) { - UnsupportedOperationException usex = new UnsupportedOperationException( - "Attempted to divide by zero."); - - usex.initCause(aex); - - throw usex; - } - default: - throw new IllegalStateException( - "Got passed a invalid ScalarExpressionType " - + expressionType); - } - } - - /* - * (non-Javadoc) - * - * @see java.lang.Object#toString() - */ - @Override - public String toString() { - return "scalar-exp[type=" + expressionType + ", l=" + scalar - + ", r=" + expression.toString() + "]"; - } -} \ No newline at end of file -- cgit v1.2.3