diff --git a/lib/dice.dart b/lib/dice.dart index 767cbb0..d86af52 100644 --- a/lib/dice.dart +++ b/lib/dice.dart @@ -1 +1 @@ -export 'package:dungeon_world_data/dw_data.dart' show Dice; +export 'package:dungeon_world_data/dw_data.dart' show Dice, DiceResult; diff --git a/lib/src/dice.dart b/lib/src/dice.dart index 68dd208..4efa4dc 100644 --- a/lib/src/dice.dart +++ b/lib/src/dice.dart @@ -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 roll(List dice) { var results = []; diff --git a/test/dice_test.dart b/test/dice_test.dart index a345856..3087fb6 100644 --- a/test/dice_test.dart +++ b/test/dice_test.dart @@ -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', () {