Skip to content

Commit 143b958

Browse files
committed
pass dieType as param to DiceRoller
1 parent 2a346e9 commit 143b958

File tree

4 files changed

+32
-14
lines changed

4 files changed

+32
-14
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ Future<void> main() async {
5050
* `2d6` -- roll `2` dice of `6` sides
5151
* special dice variations:
5252
* `4dF` -- roll `4` fudge dice (sides: `[-1, -1, 0, 0, 1, 1]`)
53-
* `1d%` -- roll `1` percentile dice (equivalent to `1d100`)
53+
* `1d%` -- roll `1` percentile dice (equivalent to `1d100` or `1d10 * 10 + 1d10`)
5454
* `1D66` -- roll `1` D66, aka `1d6*10 + 1d6`
5555
* **_NOTE_**: you _must_ use uppercase `D66`, lowercase `d66` will be interpreted as a 66-sided die
5656
* `2d[2,3,5,7]`-- roll 2 dice with values `[2,3,5,7]`

packages/dart_dice_parser/lib/src/dice_roller.dart

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,21 @@ abstract class DiceRoller {
2727

2828
/// return an Stream of ints. length == ndice, range: [min,nsides]
2929
/// duplicates allowed.
30-
Stream<int> roll({required int ndice, required int nsides, int min = 1});
30+
Stream<int> roll({
31+
required int ndice,
32+
required int nsides,
33+
int min = 1,
34+
DieType dieType = DieType.polyhedral,
35+
});
3136

32-
/// return an Stream of ints selected from the given vals. length = ndice.
37+
/// return an Stream of results selected from the given vals. length == ndice.
3338
/// results should be selected at random from vals.
34-
/// duplicates allowed.
35-
Stream<int> rollVals(int ndice, List<int> vals);
39+
/// duplicates are allowed.
40+
Stream<T> rollVals<T>(
41+
int ndice,
42+
List<T> vals, {
43+
DieType dieType = DieType.polyhedral,
44+
});
3645
}
3746

3847
/// a dice roller that uses an RNG
@@ -42,7 +51,7 @@ class RNGRoller extends DiceRoller {
4251
final Random _random;
4352

4453
/// select n items from the list of values. duplicates are possible
45-
Iterable<int> selectNFromVals<int>(num ndice, List<int> vals) => [
54+
Iterable<T> selectNFromVals<T>(num ndice, List<T> vals) => [
4655
for (var i = 0; i < ndice; i++) vals[_random.nextInt(vals.length)],
4756
];
4857

@@ -57,6 +66,7 @@ class RNGRoller extends DiceRoller {
5766
required int ndice,
5867
required int nsides,
5968
int min = 1,
69+
DieType dieType = DieType.polyhedral,
6070
}) async* {
6171
RangeError.checkValueInInterval(
6272
ndice,
@@ -76,7 +86,11 @@ class RNGRoller extends DiceRoller {
7686
}
7787

7888
@override
79-
Stream<int> rollVals(int ndice, List<int> vals) async* {
89+
Stream<T> rollVals<T>(
90+
int ndice,
91+
List<T> vals, {
92+
DieType dieType = DieType.polyhedral,
93+
}) async* {
8094
RangeError.checkValueInInterval(
8195
ndice,
8296
DiceRoller.minDice,
@@ -120,7 +134,9 @@ class DiceResultRoller with LoggingMixin {
120134
final results = <RolledDie>[];
121135
final discarded = <RolledDie>[];
122136
for (var i = 0; i < ndice; i++) {
123-
final digits = await _diceRoller.roll(ndice: 2, nsides: 6).toList();
137+
final digits = await _diceRoller
138+
.roll(ndice: 2, nsides: 6, dieType: DieType.d66)
139+
.toList();
124140
logger.finest(() => 'roll ${ndice}D66 => $digits $msg');
125141
final tens = digits[0];
126142
final ones = digits[1];
@@ -163,7 +179,7 @@ class DiceResultRoller with LoggingMixin {
163179
/// Roll N fudge dice, return results
164180
Future<RollResult> rollFudge(int ndice, [String msg = '']) async {
165181
final results = await _diceRoller
166-
.rollVals(ndice, DiceRoller.defaultFudgeVals)
182+
.rollVals(ndice, DiceRoller.defaultFudgeVals, dieType: DieType.fudge)
167183
.toList();
168184

169185
logger.finest(() => 'roll ${ndice}dF => $results $msg');
@@ -182,7 +198,11 @@ class DiceResultRoller with LoggingMixin {
182198
String msg = '',
183199
]) async {
184200
final results = await _diceRoller
185-
.rollVals(ndice, sideVals.toList(growable: false))
201+
.rollVals(
202+
ndice,
203+
sideVals.toList(growable: false),
204+
dieType: DieType.nvals,
205+
)
186206
.toList();
187207

188208
logger.finest(

packages/dart_dice_parser/lib/src/enums.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ enum DieType implements Comparable<DieType> {
99
// 1d[1,3,5,7,9]
1010
nvals(requirePotentialValues: true),
1111
// single value (e.g. a sum or count of dice)
12-
singleVal(explodable: false, requirePotentialValues: true)
13-
//penetration(explodable: false)
14-
;
12+
singleVal(explodable: false, requirePotentialValues: true);
1513

1614
const DieType({
1715
this.explodable = true,

packages/dart_dice_parser/lib/src/parser.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Parser<DiceExpression> parserBuilder(DiceResultRoller roller) {
1212
builder.primitive(
1313
digit().star().flatten('integer expected').trim().map(SimpleValue.new),
1414
);
15-
// parens
15+
// parens & curlies
1616
builder.group()
1717
..wrapper(char('(').trim(), char(')').trim(), (left, value, right) => value)
1818
..wrapper(

0 commit comments

Comments
 (0)