From 1bff7e49ed64d74e36d901e84c594cf63b58350b Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Fri, 18 Mar 2016 19:47:49 -0400 Subject: General changes to the dice package The biggest change is the addition of variables and assignment --- .../bjc/utils/dice/ReferenceDiceExpression.java | 60 ++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 BJC-Utils2/src/main/java/bjc/utils/dice/ReferenceDiceExpression.java (limited to 'BJC-Utils2/src/main/java/bjc/utils/dice/ReferenceDiceExpression.java') 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 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 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; + } +} -- cgit v1.2.3