Browse Source

Add countOutcomeBalance - balance successes/fails

tags/v0.1.3
bertieb 3 years ago
parent
commit
1fe526f1d5
1 changed files with 14 additions and 0 deletions
  1. +14
    -0
      dice.ts

+ 14
- 0
dice.ts View File

@@ -32,6 +32,7 @@ interface DiceResult {
olddice?: Array<number>;
reroll?: boolean;
outcome?: Outcomes;
balance?: number;

}

@@ -121,6 +122,18 @@ class Asphodice extends D10 {
return ((checkDice.filter(die => die >= this.successCutOff).length == 0));
}

countOutcomeBalance (resultDice: Array<number>): number{
// Return a positive / negative number representing
// the overall outcomes: each failure (eg < 6) is -1
// while each success (eg >= 6) is +1

// fun with ternary operators
return resultDice.reduce(
(acc: number, curr: number) =>
{ return (curr < this.successCutOff) ? acc - 1 : acc + 1 },
0);
}

roll (numToRoll: number): DiceResult {
let results: DiceResult = { total: 0, dice: [] };

@@ -172,6 +185,7 @@ class Asphodice extends D10 {
}
}

results.balance = this.countOutcomeBalance(results.dice);
results.total = results.dice.reduce((acc: number, curr: number) => acc + curr);
return results;
}


Loading…
Cancel
Save