summaryrefslogtreecommitdiff
path: root/dice-lang/src/bjc/dicelang/v1/CompoundDice.java
diff options
context:
space:
mode:
authorbjculkin <bjculkin@WIT-136XG42.wvu-ad.wvu.edu>2017-02-27 10:08:50 -0500
committerbjculkin <bjculkin@WIT-136XG42.wvu-ad.wvu.edu>2017-02-27 10:08:50 -0500
commit79ee129fc0d36ad10bceb942262f2842419c030c (patch)
treed1298fdb8b81726f4b9012d7a29c3029a55a3aa7 /dice-lang/src/bjc/dicelang/v1/CompoundDice.java
parentc50a0744269ce22604c0604cc69e6d5e5ce8a3fc (diff)
Pacakge reorganization
Diffstat (limited to 'dice-lang/src/bjc/dicelang/v1/CompoundDice.java')
-rw-r--r--dice-lang/src/bjc/dicelang/v1/CompoundDice.java90
1 files changed, 90 insertions, 0 deletions
diff --git a/dice-lang/src/bjc/dicelang/v1/CompoundDice.java b/dice-lang/src/bjc/dicelang/v1/CompoundDice.java
new file mode 100644
index 0000000..2d53540
--- /dev/null
+++ b/dice-lang/src/bjc/dicelang/v1/CompoundDice.java
@@ -0,0 +1,90 @@
+package bjc.dicelang.v1;
+
+/**
+ * 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 left;
+
+ /*
+ * The right die of the expression
+ */
+ private IDiceExpression right;
+
+ /**
+ * Create a new compound dice using the specified dice
+ *
+ * @param lft
+ * The die to use on the left
+ * @param rght
+ * The die to use on the right
+ */
+ public CompoundDice(IDiceExpression lft, IDiceExpression rght) {
+ this.left = lft;
+ this.right = rght;
+ }
+
+ /**
+ * Create a new compound dice from two dice strings
+ *
+ * @param lft
+ * The left side dice as a string
+ * @param rght
+ * The right side dice as a string
+ */
+ public CompoundDice(String lft, String rght) {
+ this(ComplexDice.fromString(lft),
+ ComplexDice.fromString(rght));
+ }
+
+ /**
+ * 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 left.canOptimize() && right.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(left.optimize() + "" + right.optimize());
+ }
+
+ @Override
+ public int roll() {
+ /*
+ * Make the combination of the two dice
+ */
+ return Integer.parseInt(left.roll() + "" + right.roll());
+ }
+
+ @Override
+ public String toString() {
+ return "compound[l=" + left.toString() + ", r="
+ + right.toString() + "]";
+ }
+}