mirror of
https://github.com/DungeonPaper/dungeon_world_data.git
synced 2026-05-17 18:08:01 +00:00
dice fixes + updates
This commit is contained in:
@@ -1 +1 @@
|
||||
export 'package:dungeon_world_data/dw_data.dart' show Dice;
|
||||
export 'package:dungeon_world_data/dw_data.dart' show Dice, DiceResult;
|
||||
|
||||
@@ -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>[];
|
||||
|
||||
@@ -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', () {
|
||||
|
||||
Reference in New Issue
Block a user