summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/dice/ReferenceDiceExpression.java
diff options
context:
space:
mode:
authorbculkin2442 <bjculkin@mix.wvu.edu>2016-03-18 19:47:49 -0400
committerbculkin2442 <bjculkin@mix.wvu.edu>2016-03-18 19:50:05 -0400
commit1bff7e49ed64d74e36d901e84c594cf63b58350b (patch)
tree234ee635d36a5a4a26994e07ac2367f87b0cba3a /BJC-Utils2/src/main/java/bjc/utils/dice/ReferenceDiceExpression.java
parent8ffe41a3575e7d9e4602deeb5f878c4687f4e389 (diff)
General changes to the dice package
The biggest change is the addition of variables and assignment
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/dice/ReferenceDiceExpression.java')
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/dice/ReferenceDiceExpression.java60
1 files changed, 60 insertions, 0 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/dice/ReferenceDiceExpression.java b/BJC-Utils2/src/main/java/bjc/utils/dice/ReferenceDiceExpression.java
new file mode 100644
index 0000000..d8062da
--- /dev/null
+++ b/BJC-Utils2/src/main/java/bjc/utils/dice/ReferenceDiceExpression.java
@@ -0,0 +1,60 @@
+package bjc.utils.dice;
+
+import java.util.Map;
+
+/**
+ * A dice expression that refers to a variable bound in a mutable
+ * enviroment
+ *
+ * @author ben
+ *
+ */
+public class ReferenceDiceExpression implements IDiceExpression {
+ /**
+ * The name of the bound variable
+ */
+ private String name;
+
+ /**
+ * The enviroment to do variable dereferencing against
+ */
+ private Map<String, IDiceExpression> env;
+
+ /**
+ * Create a new reference dice expression referring to the given name
+ * in an enviroment
+ *
+ * @param name
+ * The name of the bound variable
+ * @param env
+ * The enviroment to resolve the variable against
+ */
+ public ReferenceDiceExpression(String name,
+ Map<String, IDiceExpression> env) {
+ this.name = name;
+ this.env = env;
+ }
+
+ @Override
+ public int roll() {
+ return env.get(name).roll();
+ }
+
+ @Override
+ public String toString() {
+ if (env.containsKey(name)) {
+ return env.get(name).toString();
+ } else {
+ return name;
+ }
+ }
+
+ /**
+ * Get the name of the referenced variable
+ *
+ * @return the name of the referenced variable
+ */
+ public String getName() {
+ return name;
+ }
+}