Most board-game rule engines start as loose UI flags—and quickly break when edge cases like Joker redemptions, 14-tile conservation, dead-hand feasibility, and 3-exposure table penalties collide.

While building the free browser-based practice engine at Mahjong First (including the 4-seat 152-tile practice game and 8 foundation drills), we needed a 100% client-side, zero-backend ES module engine that enforces official 152-tile invariants without relying on copyrighted annual NMJL card data.

Here are the four core engineering invariants and verified math behind the engine.

1. Exact 152-Tile Conservation & Wall Dealing Math

Unlike 144-tile Chinese/Riichi sets, official 4-player American Mahjong uses exactly 152 tiles:

  • Suited tiles (108): 3 suits (Dots, Bams, Craks) × 9 ranks × 4 copies = 108
  • Winds (16): North, East, West, South × 4 copies = 16
  • Dragons (12): Red, Green, Soap (White) × 4 copies = 12
  • Flowers (8) & Jokers (8): 8 + 8 = 16
  • Total: 108 + 16 + 12 + 8 + 8 = 152

(Note: Most retail boxes ship with **166 tiles—adding 2 extra Jokers and 12 spare Blanks/Flowers. Our 152 vs. 166 Set Checker helps players isolate those 14 spares before shuffling.)

When building the 4 walls in code (and on a physical table—see our 19-Stack Wall & Setup Guide):

4 walls × 19 two-high stacks = 38 tiles per wall (152 total) Initial deal = East (14) + South (13) + West (13) + North (13) = 53 tiles Remaining playable wall = 152 - 53 = 99 tiles 

In mahjong-table-engine.mjs, every draw, discard, call, and Joker exchange must preserve total conservation (rack + exposures + discards + wall === 152) and active-turn count (14 tiles on the turn player's rack + melds; 13 tiles for waiting seats).

2. Fail-Closed State Isolation (WeakMap + Transactional Rollback)

In browser-only games, trusting mutated caller arrays invites subtle bugs (or console tampering). We isolate table ownership and turn locks using Symbol.for realm-scoped WeakMap and WeakSet registries rather than exposed object properties:

// Coupled ownership & state locks stay invisible to caller-mutated arrays const SEAT_TABLE_REF = new WeakMap(); const CLOSED_DISCARD_REFS = new WeakSet(); const SEAT_CREATED_THIS_TURN_MELD = new WeakMap(); function snapshotOwnArray(arr) { if (!Array.isArray(arr) || !Number.isInteger(arr.length) || arr.length < 0) return null; const out = new Array(arr.length); for (let i = 0; i < arr.length; i++) { if (!Object.hasOwn(arr, i)) return null; // Reject sparse or tampered arrays out[i] = arr[i]; } return out; } 

Every mutation (such as calling a discard, adjusting an exposed meld, or executing a Joker swap) runs inside a transactional clone-and-commit boundary: if any post-condition (14-tile count, Joker legality, or createdThisTurn finesse ban) fails, state rolls back atomically.

3. Joker Feasibility & Coupled Dead-Hand Detection

Under American Mahjong Joker rules, a Joker can substitute only inside a required group of 3 or more identical tiles (Pung, Kong, Quint, or Sextet). It is strictly illegal in:

  • Singles (1 tile)
  • Pairs (2 identical tiles)
  • Individual N-E-W-S winds or year-digit singles (2-0-2-6)

Even trickier is dead-hand feasibility—knowing whether a player with exposed melds can still mathematically reach 14 tiles. A naive check only counts tiles left in the unseen wall/rack. In real play, a hand can stay alive if an opponent holds an exposed Joker on top of their rack that you can legally redeem with a matching natural tile on your turn.

Our Dead Hand Rules & Feasibility Checker models coupled feasibility:

Available natural supply(T) = ownRack[T] + exposedOwn[T] + unseenWallEstimate[T] + redeemableExposedJokersInOpponentMelds(T) 

If missingNaturals > availableSupply (for instance, 3 natural 4 Dots are already dead in the discard tray while a player needs a 4 Dots pair), the seat is deterministically marked dead.

4. Verified 4-Player Payout & 3+ Exposure Penalty Matrix

Finally, American Mahjong scoring has three multipliers that often trip up both players and scorekeeper apps:

  1. Win source: Discard winner collects 2× from the discarder and 1× from the other two opponents (4× total). Self-pick collects 2× from all three (6× total).
  2. Jokerless bonus (2×): Applies only to Joker-eligible hands (containing Pungs/Kongs/Quints/Sextets) won with 0 Jokers. Singles & Pairs hands never receive a 2× Jokerless bonus, because Jokers were never legal in those hands to begin with.
  3. 3+ Exposures Penalty: Throwing a winning discard to a player who already has 3 or more exposures makes the discarder pay the entire table's share (4× base, or 8× if Jokerless), while the two innocent seats pay 0.
Card Value Category Win Mode Jokerless (2×) Opponent Shares (B / C / D) Total Collected
25 pts Standard Discard No (1×) 50 / 25 / 25 100 pts
25 pts Standard Discard Yes (2×) 100 / 50 / 50 200 pts
25 pts Standard Self-Pick Yes (2×) 100 / 100 / 100 300 pts
25 pts Standard 3+ Exp. Penalty Yes (2×) 200 / 0 / 0 200 pts
50 pts Singles/Pairs Discard N/A 100 / 50 / 50 200 pts
50 pts Singles/Pairs Self-Pick N/A 100 / 100 / 100 300 pts
75 pts Singles/Pairs Self-Pick N/A 150 / 150 / 150 450 pts

Live Interactive References (No Account, Runs Locally)

All teaching hands are independently authored so players can practice mechanics without touching copyrighted annual card lines.