1
1

Dice.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. interface TestingOptions {
  2. name : string;
  3. value : number;
  4. }
  5. class Dice {
  6. protected range : Array<number> = [0, 0, 1, 1];
  7. public minResult = 0;
  8. protected testString : string;
  9. public constructor (testString : string) {
  10. this.testString = testString;
  11. }
  12. public roll (stat : number) : Array<number> {
  13. if (stat == 0) {
  14. return [this.minResult];
  15. }
  16. let rng = this.range.slice();
  17. if (stat >= 10) {
  18. rng.push(2, 1);
  19. } else if (stat >= 7) {
  20. rng.push(1, 1);
  21. } else if (stat >= 4) {
  22. rng.push(1);
  23. }
  24. let results = [];
  25. for (let i = 0; i < stat; i++) {
  26. let index = Math.floor(Math.random() * (rng.length));
  27. results.push(rng[index]);
  28. }
  29. // TODO: Print the dice results if Memory.ShowDice = 1
  30. return results;
  31. }
  32. public rollAndSum (stat : number) : number {
  33. return this.roll(stat).reduce(Dice.sum);
  34. }
  35. public static sum (a, b) {
  36. return a + b;
  37. }
  38. public getSay (results : Array<number>) : Say {
  39. let finalResult = results.reduce(Dice.sum);
  40. return new Say(
  41. new SayBold("[", this.testString, "] "),
  42. " = [", results.join("] ["), "]",
  43. results.length == 1 ? "" :
  44. (" = " + finalResult)
  45. );
  46. }
  47. public static testAgainstRoll (player : TestingOptions, enemy : TestingOptions) : number {
  48. let playerDice = new Dice(player.name);
  49. let playerResult = playerDice.roll(player.value);
  50. let enemyDice = new Dice(enemy.name);
  51. let enemyResult = enemyDice.roll(enemy.value);
  52. return playerResult.reduce(Dice.sum) - enemyResult.reduce(Dice.sum);
  53. }
  54. public static testAgainstDifficulty (player : TestingOptions, difficulty : number) : number {
  55. let playerDice = new Dice(player.name);
  56. let playerResult = playerDice.roll(player.value);
  57. return playerResult.reduce(Dice.sum) - difficulty;
  58. }
  59. }