Roll dice (eg Asphodice) and show outcomes https://rpg.bertieb.org/dice-roller/
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. const util = require('util');
  2. /**
  3. * Counts the occurrences of a number in an array of numbers
  4. *
  5. * @param inputArray - array of numbers
  6. * @param checkNumber - number to count occurrences of
  7. * @returns number of occurrences
  8. *
  9. */
  10. function countOccurrencesOfNumber(inputArray: Array<number>, checkNumber: number): number {
  11. return inputArray.filter(c => c === checkNumber).length;
  12. }
  13. /**
  14. * Generate a random integer between *1* and `max` (inclusive)
  15. *
  16. * @param max - upper bound of random integer to generate
  17. * @returns random integer
  18. */
  19. function randIntMinOne(max: number): number {
  20. return (1 + Math.floor(Math.random() * Math.floor(max)));
  21. }
  22. interface Dice {
  23. readonly sides: number;
  24. readonly type: string;
  25. }
  26. enum Outcomes {
  27. Success = "Success",
  28. Fail = "Failure",
  29. CritSuccess = "Critical Success",
  30. CritFail = "Critical Failure",
  31. Other = "Something Else"
  32. }
  33. interface DiceResult {
  34. total: number;
  35. dice: Array<number>;
  36. olddice?: Array<number>;
  37. reroll?: boolean;
  38. outcome?: Outcomes;
  39. balance?: number;
  40. }
  41. /**
  42. * Simple d10, implements `roll()`
  43. */
  44. class D10 implements Dice {
  45. sides: number = 10;
  46. type: string = "d10"
  47. roll(numberToRoll: number): DiceResult {
  48. let results: DiceResult = { total: 0, dice: [] };
  49. for (let i = 0; i < numberToRoll; i++) {
  50. results.dice.push(randIntMinOne(this.sides));
  51. }
  52. results.total = results.dice.reduce((acc: number, curr: number) => acc + curr);
  53. return results;
  54. }
  55. [util.inspect.custom](): string {
  56. return this.type;
  57. }
  58. /**
  59. * Roll a specified number of dice
  60. *
  61. * @param numberToRoll - integer number of dice to roll
  62. * @returns DiceResult: .total and .dice (individual dice)
  63. *
  64. * @example Roll 4d10
  65. *
  66. * ```
  67. * D10.roll(4)
  68. * { total: 16, dice [ 3, 5, 6, 2] }
  69. *
  70. */
  71. }
  72. class Asphodice extends D10 {
  73. readonly type: string = "asphodice";
  74. readonly passCrit: number = 10;
  75. readonly failCrit: number = 1;
  76. readonly successCutOff: number = 6; // this or larger
  77. /**
  78. * Re-roll the high dice (ie >= 6) for a chance of failure
  79. * happens on eg 1
  80. *
  81. * @remarks
  82. * Basically we want to remove a die from the original result,
  83. * as long as that die isn't a 10, and is above the
  84. * cutoff. So we filter to get rerollCandidates, find the
  85. * highest value and get the index of that value in the
  86. * original results. We use that to splice() out (remove) that
  87. * value and push the rerolled dice onto the modified array
  88. *
  89. * @param rerollDice
  90. * @returns rerollOutcome
  91. *
  92. * @example Re-roll High Dice on 1
  93. * ```
  94. * rerollHighDice([8, 1, 1, 4, 7, 10]);
  95. * [ 1, 1, 4, 7, 10, 6]
  96. * ```
  97. *
  98. * ## Explanation
  99. *
  100. * - first filter dice to be not 10 and >= 6 (see {@link Asphodice.successCutOff})
  101. * - rerollCandidates are [8, 7]
  102. * - max([8, 7] = 8
  103. * - indexOf(8) = 0
  104. * - rerollResult = 6 (see {@link randIntMinOne()})
  105. * - `splice()` replaced dice out of array = [ 1, 1, 4, 7, 10 ]
  106. * - `push()` new dice = [ 1, 1, 4, 7, 10, 6]
  107. */
  108. rerollHighDice (rerollDice: Array<number>): Array<number> {
  109. let rerollCandidates: Array<number> = rerollDice.filter(die => (die < 10 && die >= this.successCutOff));
  110. let maxValue: number = Math.max(...rerollCandidates);
  111. let maxIndex = rerollDice.indexOf(maxValue);
  112. let rerollResult: number = randIntMinOne(this.sides);
  113. rerollDice.splice(maxIndex, 1);
  114. rerollDice.push(rerollResult);
  115. return rerollDice
  116. }
  117. /**
  118. * Re-roll low dice (ie < 6) for chance of success (ie getting >= 6)
  119. *
  120. * @remarks
  121. *
  122. * Generally happens on a 10.
  123. *
  124. * @see {@link Asphodice.rerollHighDice} for a fuller explanation
  125. * for what process in opposite direction
  126. */
  127. rerollLowDice (rerollDice: Array<number>): Array<number> {
  128. let rerollCandidates: Array<number> = rerollDice.filter(die => (die > 1 && die < this.successCutOff));
  129. let minValue: number = Math.min(...rerollCandidates);
  130. let minIndex = rerollDice.indexOf(minValue);
  131. let rerollResult: number = randIntMinOne(this.sides);
  132. rerollDice.splice(minIndex, 1);
  133. rerollDice.push(rerollResult);
  134. return rerollDice
  135. }
  136. /**
  137. * Used to determine if all dice are above Asphodice.successCutOff
  138. *
  139. * @param checkDice - Array of Dice to test
  140. *
  141. * @see {@link Asphodice.roll} where it is used to determine if
  142. * a re-roll can be skipped
  143. *
  144. * @see {@link allBelowCutOff} also.
  145. */
  146. allAboveCutOff (checkDice: Array<number>): boolean {
  147. // if filtering those *below* cutoff results in empty set
  148. // then all must be above cutoff
  149. return ((checkDice.filter(die => die < this.successCutOff).length == 0));
  150. }
  151. /**
  152. * Used to determine if all dice are below Asphodice.successCutOff
  153. *
  154. * @param checkDice - Array of Dice to test
  155. *
  156. * @see {@link Asphodice.roll} where it is used to determine if
  157. * a re-roll can be skipped
  158. *
  159. * @see {@link allAboveCutOff} also.
  160. */
  161. allBelowCutOff (checkDice: Array<number>): boolean {
  162. // if filtering those *above or equal to* cutoff results in empty set
  163. // then all must be below cutoff
  164. return ((checkDice.filter(die => die >= this.successCutOff).length == 0));
  165. }
  166. /**
  167. * Determine balance of outcomes - ie successes minus failures
  168. *
  169. * @remarks
  170. *
  171. * Success = 1, failure = -1
  172. *
  173. * Particularly for Asphodice, we aren't terribly concerned with the
  174. * numeric sum total of the dice values, we are more concerned with
  175. * the overall outcome (see {@link Outcomes}) and for narrative purposes
  176. * perhaps the balance of outcomes; for example, 1 success may be
  177. * narrated differently than 4 successes.
  178. *
  179. * **Special note**: This ignores crits! **End special note**
  180. *
  181. * TODO: Special consideration of a single die roll (?)
  182. *
  183. * @param resultDice - the final dice after any re-rolls
  184. * @returns Single positive/negative number with balance of roll outcomes
  185. *
  186. * @example Count Outcomes of 5 Dice
  187. * ```
  188. * countOutcomeBalance([ 7, 4, 4, 9, 1 ])
  189. * -1
  190. * ```
  191. *
  192. * - 7 and 9 are above successCutOff → +2
  193. * - 4, 4, 1 are below successCutOff → -3
  194. */
  195. countOutcomeBalance (resultDice: Array<number>): number{
  196. // fun with ternary operators
  197. return resultDice.reduce(
  198. (acc: number, curr: number) =>
  199. { return (curr < this.successCutOff) ? acc - 1 : acc + 1 },
  200. 0);
  201. // PS also good practice to supply initialValue (0)
  202. }
  203. /**
  204. * Roll an Asphodie or Asphodice
  205. *
  206. * @param numToRoll
  207. * @returns a {@link DiceResult} with additional properties:
  208. * - reroll: boolean - whether we rerolled
  209. * - olddice: Array - original roll
  210. * - dice: Array - final outcome
  211. * - balance: number - +/- of outcomes (successes/failures)
  212. */
  213. roll (numToRoll: number): DiceResult {
  214. let results: DiceResult = { total: 0, dice: [] };
  215. // Initial roll
  216. for (let i = 0; i < numToRoll; i++) {
  217. results.dice.push(randIntMinOne(this.sides));
  218. }
  219. // Check for re-rolls
  220. // 1. if no re-rolls we can finish here
  221. if (!(results.dice.includes(this.passCrit) || results.dice.includes(this.failCrit))) {
  222. // results.total = results.dice.reduce((acc: number, curr: number) => acc + curr);
  223. results.reroll = false;
  224. } else {
  225. // count successes and fails
  226. let rerollGood = countOccurrencesOfNumber(results.dice, this.passCrit);
  227. let rerollBad = countOccurrencesOfNumber(results.dice, this.failCrit);
  228. // 2. only reroll if they don't cancel each other out
  229. if (rerollGood == rerollBad) {
  230. // console.log("Good = Bad, no need to reroll");
  231. results.reroll = false;
  232. // results.total = results.dice.reduce((acc: number, curr: number) => acc + curr);
  233. }
  234. // If all dice are above/below cutoff we don't need to reroll
  235. // 3a. Above
  236. else if (this.allAboveCutOff(results.dice)) {
  237. console.log("All above cutoff, auto-success", results.dice);
  238. results.reroll = false;
  239. }
  240. // 3b. Below
  241. else if (this.allBelowCutOff(results.dice)) {
  242. console.log("All below cutoff, auto-fail", results.dice);
  243. results.reroll = false;
  244. }
  245. // Reroll
  246. else {
  247. // Reminder: arr1 = arr2 is a copy by reference!
  248. let olddice = results.dice.slice();
  249. if (rerollGood > rerollBad) {
  250. // Re-roll low (<6) dice for chance of success
  251. results.dice = this.rerollLowDice(results.dice);
  252. } else {
  253. // Re-roll high (>=6) dice for chance of failure
  254. results.dice = this.rerollHighDice(results.dice);
  255. }
  256. results.olddice = olddice;
  257. results.reroll = true;
  258. }
  259. }
  260. results.balance = this.countOutcomeBalance(results.dice);
  261. results.total = results.dice.reduce((acc: number, curr: number) => acc + curr);
  262. return results;
  263. }
  264. }
  265. let asphodice: Asphodice = new Asphodice();
  266. let number: number = 4;
  267. for (let i = 0; i < 10; i++) {
  268. console.log("--------------------");
  269. console.log("Rolling", number, asphodice);
  270. console.log(asphodice.roll(4));
  271. }