dice fixes + updates

This commit is contained in:
Chen Asraf
2020-06-02 10:47:38 +03:00
parent 688decb927
commit d8bc4252e2
3 changed files with 34 additions and 6 deletions

View File

@@ -1 +1 @@
export 'package:dungeon_world_data/dw_data.dart' show Dice;
export 'package:dungeon_world_data/dw_data.dart' show Dice, DiceResult;

View File

@@ -39,7 +39,22 @@ class Dice {
}
if (obj is num) {
return Dice(sides, amount * obj);
return Dice(sides, (amount * obj).toInt());
}
return this;
}
Dice operator /(obj) {
if (obj is Dice) {
if (obj.sides != sides) {
throw ("Can't divide different sided die!");
}
return Dice(sides, obj.amount / amount);
}
if (obj is num) {
return Dice(sides, amount ~/ obj);
}
return this;
@@ -54,7 +69,7 @@ class Dice {
}
if (obj is num) {
return Dice(sides, amount + obj);
return Dice(sides, amount + obj.toInt());
}
return this;
@@ -69,7 +84,7 @@ class Dice {
}
if (obj is num) {
return Dice(sides, amount - obj);
return Dice(sides, amount - obj.toInt());
}
return this;
@@ -97,6 +112,9 @@ class Dice {
return DiceResult(this, results);
}
Dice get single => this / amount;
Dice multiple(int amount) => single * amount;
/// Roll arbitrary amount of (possibly) different sided dice.
static List<DiceResult> roll(List<Dice> dice) {
var results = <DiceResult>[];

View File

@@ -21,8 +21,18 @@ void main() {
});
test('Equality', () {
var dice = Dice(12);
expect(dice, equals(Dice.d12));
var compare = {
Dice(4): Dice.d4,
Dice(6): Dice.d6,
Dice(8): Dice.d8,
Dice(10): Dice.d10,
Dice(12): Dice.d12,
Dice(20): Dice.d20,
};
for (var d in compare.keys) {
expect(d, equals(compare[d]));
expect(compare.values.where((el) => el == d).length, equals(1));
}
});
test('Roll', () {