Browse Source

add cancelRerollDice() as helper function

Cancels out 10s and 1s, for use in rerollNeeded()
tags/v0.1.3
bertieb 3 years ago
parent
commit
dde3e66bf0
1 changed files with 54 additions and 8 deletions
  1. +54
    -8
      dice.ts

+ 54
- 8
dice.ts View File

@@ -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<number>): Array<number> {
// 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: [] };



Loading…
Cancel
Save