Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 | 1x 1x 58x 11x 11x 9x 31x 9x 31x 9x 9x 9x 46x 2x 11x 58x 101x 11x 58x 11x 47x 128x 47x 7x 7x 40x 1x 3x 13x 1x 8x 8x 38x 8x 21x 1x 2x 2x 6x 2x 1x 1x 1x | import { Bag } from "./bag"; import { Fraction } from "./Fraction"; import { OutcomeFunction } from "./OutcomeFunction"; import { Token, TokenEffects } from "./tokens"; import { allCombinations, arrayEquals, cartesianProduct, combinations, factorial, flatten, replace } from "./utils"; interface PullWithOdds { tokens: Token[]; odds: Fraction; } export type OddsFn = ( numTokensPulled: number, bag: Bag, outcomes: TokenEffects, outcomeFunction: OutcomeFunction ) => number; /** * Compute the odds of a particular combination of tokens based on how many * tokens are not "redraw" tokens. * It took me a lot of time to get this formula right. Many thanks to those who * helped me get it. * * @param {number} totalNumberOfTokens * The total number of tokens in the bag * @param {number} numberOfTokensInCombination * The number of tokens in the combination * @param {number} numberOfNonRedrawTokensInCombination * The number of tokens in this combination that are not "redraw" tokens * @return {number} * The odds of this combination */ function oddsOfCombination( totalNumberOfTokens: number, numberOfTokensInCombination: number, numberOfNonRedrawTokensInCombination: number ): Fraction { return new Fraction( factorial(totalNumberOfTokens - numberOfTokensInCombination) * factorial(numberOfTokensInCombination - 1) * numberOfNonRedrawTokensInCombination, factorial(totalNumberOfTokens) ); } /** * Return all possible sets of n tokens that can be pulled from the bag along * with the odds of pullingthis particular set among all possible sets. * For exemple, if drawing only 1 token from a bag containing only a +1 token * and 2 -1 token, the result will be: * * +1 with a 0.33 odds * * -1 with a 0.66 odds * * @param {number} numTokensPulled * The number of tokens simultaneously pulled from the bag. * @param {Bag} bag * The bag from which the tokens are pulled. * @param {TokenEffects} outcomes * The token effects if needed (which is true if some of them have redraw effects). * @return {PullWithOdds[]} * The list of possible pulls along with their odds as fractions. */ function getPossiblePullsWithOdds( numTokensPulled: number, bag: Bag, outcomes?: TokenEffects ): PullWithOdds[] { let allPossibleCombinations: Token[][] = []; if (outcomes) { const tokensWithRedraw = bag .getTokens() .filter(t => outcomes.getEffect(t).isRedraw()); const tokensWithoutRedraw = bag .getTokens() .filter(t => !outcomes.getEffect(t).isRedraw()); const combinationsOfRedrawTokens = allCombinations(tokensWithRedraw); const combinationsOfNonRedrawTokens = combinations( numTokensPulled, tokensWithoutRedraw ); allPossibleCombinations = cartesianProduct( combinationsOfRedrawTokens, combinationsOfNonRedrawTokens ).map(c => flatten(c)); } else { allPossibleCombinations = combinations(numTokensPulled, bag.getTokens()); } const allCombinationsWithOdds: PullWithOdds[] = allPossibleCombinations.map( tokens => ({ odds: oddsOfCombination( bag.getTokens().length, tokens.length, outcomes ? tokens.filter(token => !outcomes.getEffect(token).isRedraw()).length : tokens.length ), tokens: tokens.sort() }) ); return allCombinationsWithOdds.reduce( (reducedCombinations: PullWithOdds[], currentCombination: PullWithOdds) => { if (reducedCombinations.length === 0) { return [currentCombination]; } else { // Find a combination with the same set of tokens const matchingCombinationIndex = reducedCombinations.findIndex( ({ tokens }) => arrayEquals(tokens, currentCombination.tokens) ); if (matchingCombinationIndex > -1) { // Update the existing combination by adding the odds const updatedCombination = { odds: reducedCombinations[matchingCombinationIndex].odds.add( currentCombination.odds ), tokens: reducedCombinations[matchingCombinationIndex].tokens }; return replace( reducedCombinations, matchingCombinationIndex, updatedCombination ); } else { // Add the combination return [...reducedCombinations, currentCombination]; } } }, [] ); } /** * Return all possible sets of n tokens that can be pulled from the bag along * with the odds of pullingthis particular set among all possible sets. * For exemple, if drawing only 1 token from a bag containing only a +1 token * and 2 -1 token, the result will be: * * +1 with a 0.33 odds * * -1 with a 0.66 odds * * @param {number} numTokensPulled * The number of tokens simultaneously pulled from the bag. * @param {Bag} bag * The bag from which the tokens are pulled. * @param {TokenEffects} outcomes * The token effects if needed (which is true if some of them have redraw effects). * @return {{ tokens: Token[]; odds: number }[]} * The list of possible pulls along with their odds. */ export function drawFromBag( numTokensPulled: number, bag: Bag, outcomes?: TokenEffects ): { tokens: Token[]; odds: number }[] { return getPossiblePullsWithOdds(numTokensPulled, bag, outcomes).map( pullWithOdds => ({ odds: pullWithOdds.odds.valueOf(), tokens: pullWithOdds.tokens }) ); } /** * Compute the odds of a particular outcome when pulling tokens from the bag. * * The outcome function is called for each possible combination of `numTokens` * from the bag and return `true` or `false` wether this specific combination * is a success or not. This function is called with first argument being the * combination (it will contain exactly `numTokens` tokens), second optional * argument being the effects of the tokens and third optional argument being * the Chaos bag. * * The simplest outcome function is checking if a particular token was pulled: * * ```javascript * function pulledASkull(tokensPulled) { * return tokensPulled[0] === ArkhamOdds.Token.SKULL; * } * ``` * * Using the token effects mapping, you can check if the skill test is a * success when the total skil vallue is 2 above the difficulty. * * ```javascript * function isSuccessWhenTwoAbove(tokensPulled, tokenEffects) { * return tokenEffects.isSuccess(tokensPulled, 2); * } * ``` * * @param {number} numTokensPulled * The number of tokens simultaneously pulled from the bag. * @param {Bag} bag * The bag from which the tokens are pulled. * @param {TokenEffects} outcomes * The token effects. * @param {OutcomeFunction} outcomeFunction * The outcome function returning `true` if the pulled tokens represent a * desired outcome and `false` otherwise. * The first argument passed to the function are the tokens pulled, the second * is the token effects map and the third is the bag. * The outcome function should always assume that redraw tokens are counted * and that pulled tokens contains the result of redrawing. * @return {number} * The odds of the desired outcome. */ export const odds: OddsFn = ( numTokensPulled: number, bag: Bag, outcomes: TokenEffects, outcomeFunction: OutcomeFunction ): number => { const possiblePullsWithOdds = getPossiblePullsWithOdds( numTokensPulled, bag, outcomes ); const filterCondition = (possiblePullWithOdds: PullWithOdds) => { return outcomeFunction(possiblePullWithOdds.tokens, outcomes, bag); }; return possiblePullsWithOdds .filter(filterCondition) .reduce( (totalOdds, successfulPullWithOdds) => totalOdds.add(successfulPullWithOdds.odds), new Fraction(0, 1) ) .valueOf(); }; /** * Similar to `odds` but this time putting tokens back into the bag between * each pull. * * It should be used for abilities like Wendy's: * * ```javascript * let wendyOdds = ArkhamOdds.oddsWithRedraw( * 2, theBag, theEffects, * ArkhamOdds.successChoosingBest(0)); * ``` * * TODO Update this function to account for tokens with a redraw effect. * * @param {number} numTokensPulled * The number of tokens sequentially pulled from the bag while putting them * back in between each pull.. * @param {Bag} bag * The bag from which the tokens are pulled. * @param {OutcomeFunction} outcomeFunction * The outcome function returning `true` if the pulled tokens represent a * desired outcome and `false` otherwise. * The first argument passed to the function are the tokens pulled, the second * is the token effects map and the third is the bag. * @return {number} * The odds of the desired outcome. */ export const oddsWithRedraw: OddsFn = ( numTokensPulled: number, bag: Bag, outcomes: TokenEffects, outcomeFunction: OutcomeFunction ): number => { const comb: Token[][] = cartesianProduct( ...Array(numTokensPulled).fill(bag.getTokens()) ); const filterCondition = (tokensPulled: Token[]) => { return outcomeFunction(tokensPulled, outcomes, bag); }; return comb.filter(filterCondition).length / comb.length; }; export * from "./bag"; export * from "./cards"; export * from "./tokens"; export * from "./OutcomeFunction"; |