Przeglądaj źródła

Add countOutcomeBalance - balance successes/fails

tags/v0.1.3
bertieb 3 lat temu
rodzic
commit
1fe526f1d5
1 zmienionych plików z 14 dodań i 0 usunięć
  1. +14
    -0
      dice.ts

+ 14
- 0
dice.ts Wyświetl plik

@@ -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;
}


Ładowanie…
Anuluj
Zapisz