fertig-classic-games/src/games/mahjong/MahjongData.js

152 lines
9.9 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Mahjong (Hong Kong style) — static catalog. No Phaser, no game state.
// Tile kinds, label-art mapping, the faan scoring table (single source of
// truth for both the engine and the in-game reference panel), and the
// faan → base-points ladder.
//
// Tiles are encoded as small integers ("kinds") so the engine can count and
// decompose hands with flat arrays:
// 0..8 bamboo 1-9 9..17 circle 1-9 18..26 character 1-9
// 27..30 winds E S W N 31..33 dragons R G W
// 34..37 flowers (seat E S W N) 38..41 seasons (seat E S W N)
// pinyin sheet index for character (萬) tiles 1..9 — mirrors Mahjong Match.
const CHAR_LABEL = [13, 14, 15, 7, 8, 9, 10, 11, 12];
export const BAMBOO = 0, CIRCLE = 9, CHAR = 18;
export const WIND_KINDS = [27, 28, 29, 30]; // E S W N
export const DRAGON_KINDS = [31, 32, 33]; // red green white
export const FIRST_BONUS = 34; // flowers then seasons
export const KIND_COUNT = 42; // 34 playing + 8 bonus
export const isSuited = (k) => k < 27;
export const isHonor = (k) => k >= 27 && k < 34;
export const isBonus = (k) => k >= FIRST_BONUS;
export const suitOf = (k) => (k < 27 ? Math.floor(k / 9) : -1); // 0 bam, 1 cir, 2 char
export const rankOf = (k) => (k % 9) + 1; // suited kinds only
export const isTerminal = (k) => isSuited(k) && (rankOf(k) === 1 || rankOf(k) === 9);
export const WIND_NAMES = ['East', 'South', 'West', 'North'];
export const SUIT_NAMES = ['Bamboo', 'Circle', 'Character'];
// One entry per kind: { kind, id, label (texture key | null), name }.
export const TILES = (() => {
const t = [];
const suits = [['bamboo', 'Bamboo'], ['circle', 'Circle'], ['char', 'Character']];
suits.forEach(([id, name], s) => {
for (let n = 1; n <= 9; n++) {
const label = id === 'char' ? `mahjong-pinyin${CHAR_LABEL[n - 1]}` : `mahjong-${id}${n}`;
t.push({ kind: s * 9 + n - 1, id: `${id}${n}`, label, name: `${name} ${n}` });
}
});
const winds = [['east', 4], ['south', 3], ['west', 6], ['north', 5]];
winds.forEach(([w, pinyin], i) => {
t.push({ kind: 27 + i, id: `wind-${w}`, label: `mahjong-pinyin${pinyin}`, name: `${WIND_NAMES[i]} Wind` });
});
t.push({ kind: 31, id: 'dragon-red', label: 'mahjong-pinyin1', name: 'Red Dragon' });
t.push({ kind: 32, id: 'dragon-green', label: 'mahjong-pinyin2', name: 'Green Dragon' });
t.push({ kind: 33, id: 'dragon-white', label: null, name: 'White Dragon' }); // drawn procedurally
const flowers = ['orchid', 'peony', 'chrysanthemum', 'lotus'];
flowers.forEach((f, i) => {
t.push({ kind: 34 + i, id: f, label: `mahjong-${f}`, name: `Flower (${WIND_NAMES[i]})` });
});
const seasons = ['spring', 'summer', 'fall', 'winter'];
seasons.forEach((s, i) => {
t.push({ kind: 38 + i, id: s, label: `mahjong-${s}`, name: `Season (${WIND_NAMES[i]})` });
});
return t;
})();
// Full wall: 4 copies of each playing tile, plus 1 of each bonus tile when
// flowers & seasons are in play (144 tiles; 136 without them).
export function buildWall(bonusTiles = true) {
const wall = [];
for (let k = 0; k < FIRST_BONUS; k++) wall.push(k, k, k, k);
if (bonusTiles) for (let k = FIRST_BONUS; k < KIND_COUNT; k++) wall.push(k);
return wall;
}
// ── House rules ──────────────────────────────────────────────────────────────
// rounds: 1 = East round only, 2 = East + South, 4 = all four winds.
// minFaan: the smallest hand that may declare a win. faanCap: the limit (a
// limit hand scores exactly this). stake: everyone's opening score.
export const DEFAULT_OPTS = {
rounds: 1, minFaan: 1, faanCap: 13, robKong: true, bonusTiles: true, stake: 500,
};
export function normalizeOpts(o = {}) {
const r = { ...DEFAULT_OPTS, ...o };
r.rounds = [1, 2, 4].includes(r.rounds) ? r.rounds : 1;
r.minFaan = Math.max(0, Math.min(5, r.minFaan | 0));
r.faanCap = [8, 10, 13].includes(r.faanCap) ? r.faanCap : 13;
r.robKong = !!r.robKong;
r.bonusTiles = !!r.bonusTiles;
r.stake = Math.max(0, r.stake | 0);
return r;
}
// ── Scoring ──────────────────────────────────────────────────────────────────
// Classic HK Old Style faan values. A hand must reach the table's minimum
// (1 faan by default) to win; bonus-tile and contextual faan count toward it.
// `excludes` lists faan ids the engine must NOT also award when this row
// applies (prevents double counting, e.g. Great Dragons already contains its
// three dragon pungs). `limit` rows are worth the table's faan cap and
// replace every other row (a limit hand is scored as the limit alone).
// `group` orders the in-game reference panel.
export const MIN_FAAN = 1; // default table minimum (see DEFAULT_OPTS)
export const LIMIT_FAAN = 13; // the largest cap; BASE_POINTS covers 0..13
export const FAAN_TABLE = [
// hand patterns
{ id: 'common-hand', group: 'pattern', label: 'Common Hand', faan: 1, desc: 'Four chows and a pair of suited tiles' },
{ id: 'all-pungs', group: 'pattern', label: 'All Pungs', faan: 3, desc: 'Four pungs or kongs and a pair' },
{ id: 'mixed-one-suit', group: 'pattern', label: 'Mixed One Suit', faan: 3, desc: 'One suit plus honor tiles only' },
{ id: 'pure-one-suit', group: 'pattern', label: 'Pure One Suit', faan: 6, desc: 'One suit only, no honors',
excludes: ['mixed-one-suit'] },
{ id: 'mixed-terminals', group: 'pattern', label: 'Mixed Terminals', faan: 1, desc: 'Only 1s, 9s and honors, all in pungs' },
{ id: 'small-dragons', group: 'pattern', label: 'Small Dragons', faan: 5, desc: 'Two dragon pungs and a dragon pair',
excludes: ['dragon-pung'] },
{ id: 'great-dragons', group: 'pattern', label: 'Great Dragons', faan: 8, desc: 'All three dragon pungs',
excludes: ['dragon-pung', 'small-dragons'] },
{ id: 'small-winds', group: 'pattern', label: 'Small Winds', faan: 6, desc: 'Three wind pungs and a wind pair',
excludes: ['seat-wind', 'round-wind'] },
// limit hands
{ id: 'great-winds', group: 'limit', limit: true, label: 'Great Winds', faan: 13, desc: 'All four wind pungs' },
{ id: 'all-honors', group: 'limit', limit: true, label: 'All Honors', faan: 13, desc: 'Winds and dragons only' },
{ id: 'thirteen-orphans', group: 'limit', limit: true, label: 'Thirteen Orphans', faan: 13, desc: 'One of every 1, 9 and honor, plus a duplicate' },
{ id: 'nine-gates', group: 'limit', limit: true, label: 'Nine Gates', faan: 13, desc: 'Concealed 1112345678999 of one suit + one more of it' },
{ id: 'all-terminals', group: 'limit', limit: true, label: 'All Terminals', faan: 13, desc: 'Pungs and a pair of 1s and 9s only' },
{ id: 'four-kongs', group: 'limit', limit: true, label: 'Four Kongs', faan: 13, desc: 'Four kongs and a pair' },
{ id: 'four-concealed-pungs', group: 'limit', limit: true, label: 'Four Concealed Pungs', faan: 13, desc: 'Four pungs, none claimed from a discard' },
{ id: 'heavenly-hand', group: 'limit', limit: true, label: 'Heavenly Hand', faan: 13, desc: 'Dealer wins with the opening deal' },
{ id: 'earthly-hand', group: 'limit', limit: true, label: 'Earthly Hand', faan: 13, desc: "Win on the dealer's very first discard" },
// per-meld faan
{ id: 'dragon-pung', group: 'meld', label: 'Dragon Pung', faan: 1, desc: 'Each pung or kong of a dragon' },
{ id: 'seat-wind', group: 'meld', label: 'Seat Wind Pung', faan: 1, desc: 'Pung or kong of your seat wind' },
{ id: 'round-wind', group: 'meld', label: 'Round Wind Pung', faan: 1, desc: 'Pung or kong of the prevailing wind' },
// win context
{ id: 'self-draw', group: 'context', label: 'Self Draw', faan: 1, desc: 'Winning tile drawn from the wall' },
{ id: 'concealed', group: 'context', label: 'Concealed Hand', faan: 1, desc: 'No melds claimed from discards' },
{ id: 'last-tile', group: 'context', label: 'Last Wall Tile', faan: 1, desc: 'Win on the very last wall tile' },
{ id: 'last-discard', group: 'context', label: 'Last Discard', faan: 1, desc: 'Win on the discard after the wall runs out' },
{ id: 'kong-draw', group: 'context', label: 'Kong Replacement', faan: 1, desc: 'Win on the tile drawn after a kong' },
{ id: 'rob-kong', group: 'context', label: 'Robbing the Kong', faan: 1, desc: "Win on the tile another player adds to a pung" },
// bonus tiles
{ id: 'seat-flower', group: 'bonus', label: 'Own Flower / Season', faan: 1, desc: 'Each flower or season matching your seat' },
{ id: 'flower-set', group: 'bonus', label: 'All Four Flowers', faan: 2, desc: 'The complete flower set' },
{ id: 'season-set', group: 'bonus', label: 'All Four Seasons', faan: 2, desc: 'The complete season set' },
{ id: 'no-bonus', group: 'bonus', label: 'No Bonus Tiles', faan: 1, desc: 'Hand finished with no flowers or seasons' },
];
export const FAAN_BY_ID = Object.fromEntries(FAAN_TABLE.map((r) => [r.id, r]));
// Base points per faan (half-doubling ladder; the cap's value is the limit).
export const BASE_POINTS = [1, 2, 4, 8, 16, 24, 32, 48, 64, 96, 128, 192, 256, 384];
export const basePoints = (faan, cap = LIMIT_FAAN) => BASE_POINTS[Math.min(faan, cap, LIMIT_FAAN)];
// Payments: winner by discard collects 2× base from the discarder alone;
// a self-drawn winner collects 1× base from each of the three others.
// Seat colours (shared visual language with the other tabletop games).
export const PLAYER_COLORS = [0xd0473a, 0x4a90d9, 0x49a25a, 0xe2b53c];
export const PLAYER_COLOR_HEX = ['#d0473a', '#4a90d9', '#49a25a', '#e2b53c'];