瀏覽代碼

Add countOutcomeBalance - balance successes/fails

tags/v0.1.3
bertieb 3 年之前
父節點
當前提交
1fe526f1d5
共有 1 個文件被更改,包括 14 次插入0 次删除
  1. +14
    -0
      dice.ts

+ 14
- 0
dice.ts 查看文件

@@ -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…
取消
儲存