diff options
| author | bculkin2442 <bjculkin@mix.wvu.edu> | 2016-10-27 21:56:18 -0400 |
|---|---|---|
| committer | bculkin2442 <bjculkin@mix.wvu.edu> | 2016-10-27 22:12:47 -0400 |
| commit | e7413128ff4e376997de6e94e4bea5eca14811ef (patch) | |
| tree | 0749e270fdb754d04dc223abd95d47436508047f /dice-lang/src/bjc/dicelang/OperatorDiceExpression.java | |
| parent | e13a6981bd278c2cfc3b5ecb2517367b117f7a52 (diff) | |
Moved examples
Diffstat (limited to 'dice-lang/src/bjc/dicelang/OperatorDiceExpression.java')
| -rw-r--r-- | dice-lang/src/bjc/dicelang/OperatorDiceExpression.java | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/dice-lang/src/bjc/dicelang/OperatorDiceExpression.java b/dice-lang/src/bjc/dicelang/OperatorDiceExpression.java new file mode 100644 index 0000000..f86773d --- /dev/null +++ b/dice-lang/src/bjc/dicelang/OperatorDiceExpression.java @@ -0,0 +1,96 @@ +package bjc.dicelang; + +/** + * Implements a class for combining two dice with an operator + * + * @author ben + * + */ +public class OperatorDiceExpression implements IDiceExpression { + /** + * The operator to use for combining the dice + */ + private DiceExpressionType expressionType; + + /** + * The dice on the left side of the expression + */ + private IDiceExpression leftExpression; + + /** + * The dice on the right side of the expression + */ + private IDiceExpression rightExpression; + + /** + * Create a new compound expression using the specified parameters + * + * @param right + * The die on the right side of the expression + * @param left + * The die on the left side of the expression + * @param type + * The operator to use for combining the dices + */ + public OperatorDiceExpression(IDiceExpression right, + IDiceExpression left, DiceExpressionType type) { + this.rightExpression = right; + this.leftExpression = left; + this.expressionType = type; + } + + /* + * (non-Javadoc) + * + * @see bjc.utils.dice.IDiceExpression#roll() + */ + @Override + public int roll() { + /* + * Handle each operator + */ + switch (expressionType) { + case ADD: + return rightExpression.roll() + leftExpression.roll(); + case SUBTRACT: + return rightExpression.roll() - leftExpression.roll(); + case MULTIPLY: + return rightExpression.roll() * leftExpression.roll(); + case DIVIDE: + /* + * Round to keep results as integers. We don't really have + * any need for floating-point dice, and continuous + * probability is a pain + */ + try { + return rightExpression.roll() / leftExpression.roll(); + } catch (ArithmeticException aex) { + UnsupportedOperationException usex = new UnsupportedOperationException( + "Attempted to divide by zero." + + " Problematic expression is " + + leftExpression); + + usex.initCause(aex); + + throw usex; + } + default: + throw new IllegalArgumentException( + "Got passed a invalid ScalarExpressionType (" + + expressionType + "). WAT"); + + } + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + return "dice-exp[type=" + expressionType + ", l=" + + leftExpression.toString() + ", r=" + + rightExpression.toString() + "]"; + } +}
\ No newline at end of file |
