summaryrefslogtreecommitdiff
path: root/dice-lang/src/bjc/dicelang/CompoundDice.java
diff options
context:
space:
mode:
authorbculkin2442 <bjculkin@mix.wvu.edu>2016-10-27 21:56:18 -0400
committerbculkin2442 <bjculkin@mix.wvu.edu>2016-10-27 22:12:47 -0400
commite7413128ff4e376997de6e94e4bea5eca14811ef (patch)
tree0749e270fdb754d04dc223abd95d47436508047f /dice-lang/src/bjc/dicelang/CompoundDice.java
parente13a6981bd278c2cfc3b5ecb2517367b117f7a52 (diff)
Moved examples
Diffstat (limited to 'dice-lang/src/bjc/dicelang/CompoundDice.java')
-rw-r--r--dice-lang/src/bjc/dicelang/CompoundDice.java100
1 files changed, 100 insertions, 0 deletions
diff --git a/dice-lang/src/bjc/dicelang/CompoundDice.java b/dice-lang/src/bjc/dicelang/CompoundDice.java
new file mode 100644
index 0000000..704e4cd
--- /dev/null
+++ b/dice-lang/src/bjc/dicelang/CompoundDice.java
@@ -0,0 +1,100 @@
+package bjc.dicelang;
+
+/**
+ * Implements a "compound dice"
+ *
+ * To explain, a compound dice is something like a d100 composed from two
+ * d10s instead of a hundred sided die.
+ *
+ * @author ben
+ *
+ */
+public class CompoundDice implements IDiceExpression {
+ /**
+ * The left die of the expression
+ */
+ private IDiceExpression leftDice;
+
+ /**
+ * The right die of the expression
+ */
+ private IDiceExpression rightDice;
+
+ /**
+ * Create a new compound dice using the specified dice
+ *
+ * @param left
+ * The die to use on the left
+ * @param right
+ * The die to use on the right
+ */
+ public CompoundDice(IDiceExpression left, IDiceExpression right) {
+ this.leftDice = left;
+ this.rightDice = right;
+ }
+
+ /**
+ * Create a new compound dice from two dice strings
+ *
+ * @param leftExp
+ * The left side dice as a string
+ * @param rightExp
+ * The right side dice as a string
+ */
+ public CompoundDice(String leftExp, String rightExp) {
+ this(ComplexDice.fromString(leftExp),
+ ComplexDice.fromString(rightExp));
+ }
+
+ /**
+ * Create a new compound dice from an array of dice strings
+ *
+ * @param exps
+ * An array of two dice strings
+ */
+ public CompoundDice(String[] exps) {
+ this(exps[0], exps[1]);
+ }
+
+ @Override
+ public boolean canOptimize() {
+ return leftDice.canOptimize() && rightDice.canOptimize();
+ }
+
+ @Override
+ public int optimize() {
+ if (!canOptimize()) {
+ throw new UnsupportedOperationException(
+ "Cannot optimize this compound dice. "
+ + "Both component dice must be optimizable"
+ + " to optimize a compound dice");
+ }
+
+ return Integer
+ .parseInt(leftDice.optimize() + "" + rightDice.optimize());
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see bjc.utils.dice.IDiceExpression#roll()
+ */
+ @Override
+ public int roll() {
+ /*
+ * Make the combination of the two dice
+ */
+ return Integer.parseInt(leftDice.roll() + "" + rightDice.roll());
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see java.lang.Object#toString()
+ */
+ @Override
+ public String toString() {
+ return "compound[l=" + leftDice.toString() + ", r="
+ + rightDice.toString() + "]";
+ }
+} \ No newline at end of file