From e6d8583220c044e9cdcb99a80c78e7bcd41cfac3 Mon Sep 17 00:00:00 2001 From: Emma Date: Mon, 5 Aug 2024 03:54:38 -0600 Subject: [PATCH] dice parser --- lib/dice.ts | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 lib/dice.ts diff --git a/lib/dice.ts b/lib/dice.ts new file mode 100644 index 0000000..fa2ad2e --- /dev/null +++ b/lib/dice.ts @@ -0,0 +1,89 @@ +export class Dice { + private count!: number; + private sides!: number; + + constructor(dice: string) { + this.parseDice(dice); + } + + private parseDice(dice: string) { + const [c, s] = dice.split(/[dD]/); + this.count = Number(c); + this.sides = Number(s); + } + + public roll() { + let total = 0; + for (let i = 0; i < this.count; i++) { + total += this.rollSingle(); + } + return total; + } + + private rollSingle() { + return Math.ceil(Math.random() * this.sides); + } + + public rollAvg() { + return this.roll() / this.count; + } + + public rollTimes(times: number) { + let total = 0; + for (let i = 0; i < times; i++) { + total += this.roll(); + } + return total; + } + + public rollTimesAvg(times: number) { + return this.rollTimes(times) / times; + } + + public getNormalizedRollDistribution(): Record { + const distribution: Record = this.computeDistribution(); + + // Normalize the distribution + const totalOutcomes = Math.pow(this.sides, this.count); + for (const sum in distribution) { + if (distribution.hasOwnProperty(sum)) { + distribution[sum] /= totalOutcomes; + } + } + + return distribution; + } + + public getRollDistribution(): Record { + return this.computeDistribution(); + } + + private computeDistribution(): Record { + const distribution: Record = {}; + + // Helper function to compute the sum distribution for given number of dice + const computeSumDistribution = ( + dice: number, + sides: number, + currentSum: number, + currentDice: number + ): void => { + if (currentDice === dice) { + distribution[currentSum] = (distribution[currentSum] || 0) + 1; + return; + } + for (let i = 1; i <= sides; i++) { + computeSumDistribution(dice, sides, currentSum + i, currentDice + 1); + } + }; + + // Compute distribution + computeSumDistribution(this.count, this.sides, 0, 0); + + return distribution; + } + // STATIC + static isDice(d: string) { + return /\d+[dD]\d+/.test(d); + } +}