diff options
| author | Ben Culkin <scorpress@gmail.com> | 2020-11-21 23:11:43 -0500 |
|---|---|---|
| committer | Ben Culkin <scorpress@gmail.com> | 2020-11-21 23:11:43 -0500 |
| commit | eda9a86d8d48758e9982cfffd470c3b38a0a4b0b (patch) | |
| tree | fbe073650de751486725844ed41dfe70986a914d /dice/src/example/java/bjc/dicelang/neodice/commands | |
| parent | b5c2fb1ed923d43412694729b4445a66fa9f47fc (diff) | |
Make dice generic
Convert dice from dealing exclusively with ints, to deal with objects of
arbitrary types
Diffstat (limited to 'dice/src/example/java/bjc/dicelang/neodice/commands')
| -rw-r--r-- | dice/src/example/java/bjc/dicelang/neodice/commands/PolyhedralDieCommand.java | 6 | ||||
| -rw-r--r-- | dice/src/example/java/bjc/dicelang/neodice/commands/RollCommand.java | 9 |
2 files changed, 12 insertions, 3 deletions
diff --git a/dice/src/example/java/bjc/dicelang/neodice/commands/PolyhedralDieCommand.java b/dice/src/example/java/bjc/dicelang/neodice/commands/PolyhedralDieCommand.java index 95e9dff..6af495e 100644 --- a/dice/src/example/java/bjc/dicelang/neodice/commands/PolyhedralDieCommand.java +++ b/dice/src/example/java/bjc/dicelang/neodice/commands/PolyhedralDieCommand.java @@ -20,7 +20,11 @@ public class PolyhedralDieCommand implements Command { if (numSides < 0) throw new DieBoxException("Number of sides to polyhedral-die was not valid (must be less than 0, was %d)", numSides); - return new DieStatementValue(Die.polyhedral(numSides)); + IDie<StatementValue> die = IDie + .polyhedral(numSides) + .transform(IntegerStatementValue::new); + + return new DieStatementValue(INTEGER, die); } else { throw new DieBoxException("Number of sides to polyhedral-die wasn't an integer (was %s, of type %s)", sideValue, sideValue.type); diff --git a/dice/src/example/java/bjc/dicelang/neodice/commands/RollCommand.java b/dice/src/example/java/bjc/dicelang/neodice/commands/RollCommand.java index eb7b597..eb8beda 100644 --- a/dice/src/example/java/bjc/dicelang/neodice/commands/RollCommand.java +++ b/dice/src/example/java/bjc/dicelang/neodice/commands/RollCommand.java @@ -18,11 +18,16 @@ public class RollCommand implements Command { if (toRoll.type == DIE) { DieStatementValue die = (DieStatementValue) toRoll; - return new IntegerStatementValue(die.value.roll(state.rng)); + return die.value.roll(state.rng); } else if (toRoll.type == DIEPOOL) { DiePoolStatementValue pool = (DiePoolStatementValue) toRoll; - return new IntArrayStatementValue(pool.value.roll(state.rng)); + StatementValue[] values = pool.value + .roll(state.rng) + .toArray((sz) -> new StatementValue[sz]); + + return new ArrayStatementValue<>(pool.elementType, + values); } 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); |
