How a Mines multiplier is calculated: (1 - edge) / P(survive), in JavaScript

How a Mines multiplier is calculated: (1 - edge) / P(survive), in JavaScript

Mines is the Minesweeper-flavored grid game: a 5×5 board hides a few mines, you open tiles one at a time, and a multiplier climbs with every safe tile. The multiplier looks like a design choice, but it's almost entirely determined by one probability. This post derives it, writes it in JavaScript, and points out two easy mistakes along the way. The code blocks below form one script: paste them into a single .mjs file in order and node prints the output shown under each. The survival probability Take a board of T tiles with M mines, so T − M tiles are safe. Mine positions are fixed before the first click, and you open tiles without replacement. The first tile is safe with probability (T − M) / T. Given that it was safe, the board now has T − 1 tiles left and still M mines, so the second is safe with probability (T − M − 1) / (T − 1). Keep going for k picks: P(survive k) = ∏_{i=0}^{k−1} (T − M − i) / (T − i) Enter fullscreen mode Exit fullscreen mode This is a hypergeometric probability: the chance that a sample of k tiles drawn without replacement contains zero mines. Written with binomial coefficients, it's the number of all-safe k-tile subsets over the number of k-tile subsets: P(survive k) = C(T − M, k) / C(T, k) Enter fullscreen mode Exit fullscreen mode From probability to multiplier A multiplier is fair when its expected payout equals the stake: P(survive k) × m_k = 1, so m_k = 1 / P(survive k). A game with a house edge e pays a fraction of that: m_k = (1 − e) / P(survive k) Enter fullscreen mode Exit fullscreen mode In code: // Chance of opening `picks` tiles in a row without a mine, // on a board of `tiles` tiles with `mines` mines. function survival(tiles, mines, picks) { const safe = tiles - mines; if (picks safe) return 0; let p = 1; for (let i = 0; i `${c} pick${c > 1 ? "s" : ""}`.padStart(12)).join("")); for (const mines of [1, 3, 5, 10, 24]) { const cells = cols.map((c) => { const s = survival(T, mines, c); return (s ? (0.99 / s).toFixed(4) + "×" : "—").padStart(12); }); console.log(String(mines).padStart(5) + cells.join("")); } Enter fullscreen mode Exit fullscreen mode mines 1 pick 2 picks 3 picks 5 picks 10 picks 1 1.0313× 1.0761× 1.1250× 1.2375× 1.6500× 3 1.1250× 1.2857× 1.4786× 1.9974× 5.0044× 5 1.2375× 1.5632× 1.9974× 3.3926× 17.5154× 10 1.6500× 2.8286× 5.0044× 17.5154× 1077.6132× 24 24.7500× — — — — Enter fullscreen mode Exit fullscreen mode 3 mines and 5 picks gives 1.9974×; so does 5 mines and 3 picks. 1 mine and 3 picks equals 3 mines and 1 pick, and 10 mines and 5 picks equals 5 mines and 10 picks. That follows from the binomials: C(T − M, k) / C(T, k) = C(T − k, M) / C(T, M) Enter fullscreen mode Exit fullscreen mode Both sides equal (T − M)! (T − k)! / (T! (T − M − k)!), which is symmetric in M and k. In words: "the k tiles I open contain no mine" is the same event as "the M mines all sit among the T − k tiles I didn't open". Swap the number of mines and the number of picks and the probability doesn't change. (Print this table to two decimals and the symmetry seems to break: 1 mine / 3 picks shows 1.12× and 3 mines / 1 pick shows 1.13×. Both are exactly 0.99 × 25/22 = 1.125, but one comes out of the floating-point product as 1.1249999999999998, and toFixed(2) rounds the two values in opposite directions.) Mistake 2: forgetting what rounding costs Displayed multipliers are usually rounded down to two decimals. That takes up to 0.01 off each multiplier, which is a bigger share of a small multiplier than a large one. With a 5% edge: // Round the multiplier down to two decimals and see what it returns. for (const picks of [1, 2, 3, 4, 5, 10]) { const s = survival(T, M, picks); const raw = 0.95 / s; const shown = Math.floor(raw * 100) / 100; console.log(`${String(picks).padStart(2)} pick${picks > 1 ? "s" : " "}: ${raw.toFixed(4)} -> ${shown.toFixed(2)}, returns ${(s * shown * 100).toFixed(3)}%`); } Enter fullscreen mode Exit fullscreen mode 1 pick : 1.0795 -> 1.07, returns 94.160% 2 picks: 1.2338 -> 1.23, returns 94.710% 3 picks: 1.4188 -> 1.41, returns 94.409% 4 picks: 1.6429 -> 1.64, returns 94.835% 5 picks: 1.9167 -> 1.91, returns 94.670% 10 picks: 4.8022 -> 4.80, returns 94.957% Enter fullscreen mode Exit fullscreen mode The loss isn't monotonic (it depends on where each raw value falls between two cents), but the first pick, the smallest multiplier, takes the biggest hit here, returning 94.16% instead of 95%. If you publish a return figure, say "up to", or compute it from the rounded values rather than the formula. Exact fractions with BigInt The running product is fine in double precision for boards this size, but it's a product of rounded divisions. For exact work, keep the fraction C(T − M, k) / C(T, k) as two BigInts and reduce it: // Exact binomial coefficient: after step i, r is C(n, i + 1), so each division is exact. function binom(n, k) { let r = 1n; for (let i = 0n; i (b === 0n ? a : gcd(b, a % b)); // Survival as an exact fraction C(T − M, k) / C(T, k), in lowest terms. function survivalExact(tiles, mines, picks) { const num = binom(tiles - mines, picks); const den = binom(tiles, picks); const g = gcd(num, den); return [num / g, den / g]; } console.log(survivalExact(25, 3, 5)); const [a, b] = survivalExact(25, 3, 22); console.log(survivalExact(25, 3, 22), survival(25, 3, 22), Number(a) / Number(b)); Enter fullscreen mode Exit fullscreen mode [ 57n, 115n ] [ 1n, 2300n ] 0.00043478260869565214 0.0004347826086956522 Enter fullscreen mode Exit fullscreen mode The worked example reduces to 57/115. The longest run on 25 tiles with 3 mines, all 22 safe tiles, is exactly 1/2300. The running product lands on the neighboring double rather than the one nearest to 1/2300: harmless here, but it's the kind of drift that matters once you compare values with === or floor them to cents. If you do need a Number, convert the reduced numerator and denominator once at the end. If you'd rather try boards than read tables, our small team built a mines calculator that runs these same formulas for any board from 3×3 to 7×7, any mine count and any edge. Takeaways P(survive k) = C(T − M, k) / C(T, k), a hypergeometric probability, not (1 − M/T)^k. The multiplier is (1 − e) / P, so every stopping point has the same expectation, 1 − e. Mines and picks are interchangeable in the formula, which is why the table is symmetric. Rounding down to two decimals costs up to 0.01 per multiplier, most noticeably on small ones.

Original Source

Read the full article at Dev →

KhanList aggregates and links to publicly available news content. We do not host full articles from third-party sources. Always verify important information with original sources.