summaryrefslogtreecommitdiff
path: root/dice/src/example/java/bjc/dicelang/neodice/commands/RollCommand.java
diff options
context:
space:
mode:
authorBen Culkin <scorpress@gmail.com>2020-11-21 18:04:20 -0500
committerBen Culkin <scorpress@gmail.com>2020-11-21 18:04:20 -0500
commit3ddd062d60d621971af59b480ba70e8bf9e705f1 (patch)
tree8d24f91e4f695a526777b5fe75fe268669b8c9a6 /dice/src/example/java/bjc/dicelang/neodice/commands/RollCommand.java
parent2afb54eecd8e8b5d663a05131c07c6b8d15e65ba (diff)
Rudimentary CLI for new die implementation
Diffstat (limited to 'dice/src/example/java/bjc/dicelang/neodice/commands/RollCommand.java')
-rw-r--r--dice/src/example/java/bjc/dicelang/neodice/commands/RollCommand.java43
1 files changed, 43 insertions, 0 deletions
diff --git a/dice/src/example/java/bjc/dicelang/neodice/commands/RollCommand.java b/dice/src/example/java/bjc/dicelang/neodice/commands/RollCommand.java
new file mode 100644
index 0000000..eb7b597
--- /dev/null
+++ b/dice/src/example/java/bjc/dicelang/neodice/commands/RollCommand.java
@@ -0,0 +1,43 @@
+package bjc.dicelang.neodice.commands;
+
+import static bjc.dicelang.neodice.statements.StatementValue.Type.*;
+
+import java.util.*;
+
+import bjc.dicelang.neodice.*;
+import bjc.dicelang.neodice.statements.*;
+
+public class RollCommand implements Command {
+ @Override
+ public StatementValue execute(Iterator<String> words, DieBoxCLI state) {
+ if (!words.hasNext()) {
+ throw new DieBoxException("Roll must be provided an argument to roll");
+ } else {
+ StatementValue toRoll = state.runStatement(words);
+
+ if (toRoll.type == DIE) {
+ DieStatementValue die = (DieStatementValue) toRoll;
+
+ return new IntegerStatementValue(die.value.roll(state.rng));
+ } else if (toRoll.type == DIEPOOL) {
+ DiePoolStatementValue pool = (DiePoolStatementValue) toRoll;
+
+ return new IntArrayStatementValue(pool.value.roll(state.rng));
+ } else {
+ throw new DieBoxException("Roll was provided something that wasn't rollable (only DIE and DIEPOOL objects are rollable) (was %s, of type %s)",
+ toRoll, toRoll.type);
+ }
+ }
+ }
+
+ @Override
+ public String shortHelp() {
+ return "rolls a die-like object";
+ }
+
+ @Override
+ public String longHelp() {
+ return "Rolls a die-like object, and yields the result of rolling it."
+ + " What is returned can differ based on what is rolled";
+ }
+} \ No newline at end of file