diff --git a/dice.ts b/dice.ts index 6a16afa..d337b01 100644 --- a/dice.ts +++ b/dice.ts @@ -90,7 +90,7 @@ class D10 implements Dice { */ } -class Asphodice extends D10 { +export class Asphodice extends D10 { readonly type: string = "asphodice"; readonly passCrit: number = 10; readonly failCrit: number = 1; @@ -250,15 +250,50 @@ class Asphodice extends D10 { } /** - * Roll an Asphodie or Asphodice + * 'Cancel out' reroll dice * - * @param numToRoll - * @returns a {@link DiceResult} with additional properties: - * - reroll: boolean - whether we rerolled - * - olddice: Array - original roll - * - dice: Array - final outcome - * - balance: number - +/- of outcomes (successes/failures) + * @remarks + * + * Helper function. + * + * **In the context of deciding a reroll only**, the reroll dice can + * 'cancel' each other out. The dice themselves remain intact for the + * purposes of determining the final outcome. + * + * @returns Filtered dice */ + cancelRerollDice (resultDice: Array): Array { + // Naive approach for now: + // - count 10s and 1s + // - remove all 10s and 1s from input array + // - re-add 10s/1s after 'cancelling' + let tens = 0; + let ones = 0; + + // count + for (let die of resultDice) { + if (die === 10) { + tens++; + } else if (die === 1) { + ones++; + } + } + + // remove any reroll dice + let outputDice = resultDice.filter( die => (die != 1 && die != 10) ); + + let balance = tens - ones; + + if (balance < 0) { + // add balance of ones + outputDice.push(...Array(Math.abs(balance)).fill(1)); + } else if (balance > 0) { + // add balance of tens + outputDice.push(...Array(Math.abs(balance)).fill(10)); + } + + return outputDice; + } /** * Determines if a reroll is needed @@ -297,11 +332,22 @@ class Asphodice extends D10 { return false; } + // We should re-roll return true; } + /** + * Roll an Asphodie or Asphodice + * + * @param numToRoll + * @returns a {@link DiceResult} with additional properties: + * - reroll: boolean - whether we rerolled + * - olddice: Array - original roll + * - dice: Array - final outcome + * - balance: number - +/- of outcomes (successes/failures) + */ roll (numToRoll: number): DiceResult { let results: DiceResult = { total: 0, dice: [] };