summaryrefslogtreecommitdiff
path: root/dice-lang/src/main/java/bjc/dicelang/CompoundDiceExpression.java
diff options
context:
space:
mode:
authorbculkin2442 <bjculkin@mix.wvu.edu>2016-03-28 08:47:23 -0400
committerbculkin2442 <bjculkin@mix.wvu.edu>2016-03-28 08:47:23 -0400
commit62e94ef994a59e87543445bb3c0ce0a37017a70a (patch)
treedfa1a488d3929e3f93e2a91669cdaf97c6d707f3 /dice-lang/src/main/java/bjc/dicelang/CompoundDiceExpression.java
parent78d9c539e25f16fd15f06c2b2c48c0ad37a21540 (diff)
Renamed packages to suit updated project
Diffstat (limited to 'dice-lang/src/main/java/bjc/dicelang/CompoundDiceExpression.java')
-rw-r--r--dice-lang/src/main/java/bjc/dicelang/CompoundDiceExpression.java83
1 files changed, 83 insertions, 0 deletions
diff --git a/dice-lang/src/main/java/bjc/dicelang/CompoundDiceExpression.java b/dice-lang/src/main/java/bjc/dicelang/CompoundDiceExpression.java
new file mode 100644
index 0000000..162317b
--- /dev/null
+++ b/dice-lang/src/main/java/bjc/dicelang/CompoundDiceExpression.java
@@ -0,0 +1,83 @@
+package bjc.dicelang;
+
+/**
+ * Implements a class for combining two dice with an operator
+ *
+ * @author ben
+ *
+ */
+public class CompoundDiceExpression implements IDiceExpression {
+ /**
+ * The operator to use for combining the dice
+ */
+ private DiceExpressionType det;
+
+ /**
+ * The dice on the left side of the expression
+ */
+ private IDiceExpression left;
+
+ /**
+ * The dice on the right side of the expression
+ */
+ private IDiceExpression right;
+
+ /**
+ * 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 det
+ * The operator to use for combining the dices
+ */
+ public CompoundDiceExpression(IDiceExpression right,
+ IDiceExpression left, DiceExpressionType det) {
+ this.right = right;
+ this.left = left;
+ this.det = det;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see bjc.utils.dice.IDiceExpression#roll()
+ */
+ @Override
+ public int roll() {
+ /*
+ * Handle each operator
+ */
+ switch (det) {
+ case ADD:
+ return right.roll() + left.roll();
+ case SUBTRACT:
+ return right.roll() - left.roll();
+ case MULTIPLY:
+ return right.roll() * left.roll();
+ case DIVIDE:
+ /*
+ * Round to keep results as integers. We don't really have
+ * any need for floating-point dice
+ */
+ return Math.round(right.roll() / left.roll());
+ default:
+ throw new IllegalArgumentException(
+ "Got passed a invalid ScalarExpressionType " + det
+ + ". WAT");
+
+ }
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see java.lang.Object#toString()
+ */
+ @Override
+ public String toString() {
+ return "dice-exp[type=" + det + ", l=" + left.toString() + ", r="
+ + right.toString() + "]";
+ }
+}