summaryrefslogtreecommitdiff
path: root/dice-lang/src/bjc/utils/dice/ComplexDice.java
diff options
context:
space:
mode:
authorbculkin2442 <bjculkin@mix.wvu.edu>2016-03-28 08:44:54 -0400
committerbculkin2442 <bjculkin@mix.wvu.edu>2016-03-28 08:44:54 -0400
commit78d9c539e25f16fd15f06c2b2c48c0ad37a21540 (patch)
tree52d5db559d390c2fb83d4a4cd3bbfa4344c482e8 /dice-lang/src/bjc/utils/dice/ComplexDice.java
parent1a020352bdcbb6b5279b703f93e42a1eb7071e36 (diff)
Imported dice stuff from general utils into dedicated project
Diffstat (limited to 'dice-lang/src/bjc/utils/dice/ComplexDice.java')
-rw-r--r--dice-lang/src/bjc/utils/dice/ComplexDice.java113
1 files changed, 113 insertions, 0 deletions
diff --git a/dice-lang/src/bjc/utils/dice/ComplexDice.java b/dice-lang/src/bjc/utils/dice/ComplexDice.java
new file mode 100644
index 0000000..226f9fd
--- /dev/null
+++ b/dice-lang/src/bjc/utils/dice/ComplexDice.java
@@ -0,0 +1,113 @@
+package bjc.utils.dice;
+
+/**
+ * Implements a collection of one or more of a particular die, where the
+ * number of dice in the group is variable.
+ *
+ * @author ben
+ *
+ */
+public class ComplexDice implements IDiceExpression {
+ /**
+ * Create a dice from a string expression
+ *
+ * @param dice
+ * The string to parse the dice from
+ * @return A dice group parsed from the string
+ */
+ public static ComplexDice fromString(String dice) {
+ /*
+ * Split it on the dice type marker
+ */
+ String[] strangs = dice.split("d");
+
+ try {
+ /*
+ * Create the actual dice
+ */
+ return new ComplexDice(
+ new ScalarDie(Integer.parseInt(strangs[0])),
+ new Die(Integer.parseInt(strangs[1])));
+ } catch (NumberFormatException nfex) {
+ /*
+ * 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.");
+ }
+ }
+
+ /**
+ * The die being rolled
+ */
+ private IDiceExpression die;
+
+ /**
+ * The number of the specified die to roll
+ */
+ private IDiceExpression nDice;
+
+ /**
+ * Create a new collection of dice
+ *
+ * @param nDce
+ * The number of dice in the collection
+ * @param de
+ * The type of dice the collection is composed of
+ */
+ public ComplexDice(IDiceExpression nDce, IDiceExpression de) {
+ nDice = nDce;
+ die = de;
+ }
+
+ /**
+ * Create a new collection of dice
+ *
+ * @param nSides
+ * The number of dice in the collection
+ * @param de
+ * The type of dice the collection is composed of
+ */
+ public ComplexDice(int nSides, int de) {
+ nDice = new ScalarDie(nSides);
+ die = new Die(de);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see bjc.utils.dice.IDiceExpression#roll()
+ */
+ @Override
+ public int roll() {
+ int res = 0;
+
+ /*
+ * Add the results of rolling each die
+ */
+ int nRoll = nDice.roll();
+
+ for (int i = 0; i < nRoll; i++) {
+ res += die.roll();
+ }
+
+ return res;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see java.lang.Object#toString()
+ */
+ @Override
+ public String toString() {
+ if (nDice instanceof ScalarDie && die instanceof Die) {
+ return nDice.toString() + die.toString();
+ } else {
+ return "complex[n=" + nDice.toString() + ", d="
+ + die.toString() + "]";
+ }
+ }
+}