/* ZimiGames: Makruk (Thai chess), the page. The rules and the searcher are in makruk-engine.js, run in a Web Worker built from its own source (enginekit.js) with the main thread as the fallback. Our own code and drawn pieces, GPL-3.0. */ (function (root) { 'use strict'; if (typeof document === 'undefined' || !root.ZimiMakrukEngine || !root.BoardUI) return; var WK = root.WorldKit, BK = root.BoardKit, UI = root.BoardUI, T = UI.t, $ = function (id) { return document.getElementById(id); }; var F = root.ZimiFairy, E = root.ZimiMakrukEngine(root.ZimiSearch()), brain = root.EngineKit.client(root.ZimiMakrukEngine, 300); var WHITE = E.WHITE, SIZE = E.SIZE, N = SIZE * SIZE, HINT_MS = 1500; function sideName(p) { return UI.side(p === 0 ? 'white' : 'black'); } var LEVEL_CHOICES = [['fairy', 'chess.level.fairy'], ['master', 'chess.level.master']]; // Strong and Master, when Fairy-Stockfish is there // Show legal moves is the shell's Hints setting (Menu > Settings), not a New game option. function hintsOn() { return !root.ZimiShell || root.ZimiShell.settings.get('hints') !== 'off'; } // ── the pieces, drawn on a 45 x 45 grid ── var BASE = ''; var ART = { 1: '', 2: BASE + '', 3: BASE + '', 4: BASE + '', 5: BASE + '', 6: BASE + '' }; function pieceSvg(v) { var t = Math.abs(v); return ''; } var G = WK.create({ id: 'makruk', options: WK.standardOptions({ first: 'chess.side.white', second: 'board.black', levels: LEVEL_CHOICES }), onNew: newGame, onUndo: undo, onHint: hint }); var score = WK.scoreBar($('wk-score'), [{ color: 'var(--mk-w)', border: 'var(--mk-w-line)' }, { color: 'var(--mk-b)', border: 'var(--mk-b-line)' }]); var board = $('mk-board'), flipBtn = $('mk-flip'), cells = []; var pos, keys, played, sel = -1, targets = {}, lastMove = 0, manualFlip = false, tip = null; function turnIdx() { return pos.turn === WHITE ? 0 : 1; } function flipped() { return manualFlip !== (G.vsComputer() && G.settings.side === 'second'); } function sqAt(d) { return flipped() ? N - 1 - d : d; } function nameOf(p) { return G.vsComputer() ? (G.human(p) ? T('board.you') : T('board.zimi')) : sideName(p); } function label(s) { return E.squareName(s); } function buildBoard() { board.textContent = ''; for (var d = 0; d < N; d++) { var c = document.createElement('button'); c.type = 'button'; c.dataset.d = d; c.addEventListener('click', onSquare); board.appendChild(c); cells.push(c); } BK.gridNav(board, cells, SIZE); } function paint() { var kingSq = E.inCheck(pos, pos.turn) ? pos.king[pos.turn === WHITE ? 0 : 1] : -1; var open = !G.over && !G.thinking && G.human(turnIdx()); for (var d = 0; d < N; d++) { var s = sqAt(d), c = cells[d], v = pos.sq[s], r = d >> 3, f = d & 7, cls = 'bg-sq' + ((r + f) % 2 ? ' dk' : ''); if (lastMove && (s === E.from(lastMove) || s === E.to(lastMove))) cls += ' last'; if (s === sel) cls += ' sel'; if (s === kingSq) cls += ' bad'; if (tip && (s === tip[0] || s === tip[1])) cls += ' sel'; if (hintsOn() && targets.hasOwnProperty(s)) cls += ' hint' + (v ? ' cap' : ''); c.className = cls; var sig = (v || '') + '|' + (f === 0 ? 'r' : '') + (r === SIZE - 1 ? 'f' : '') + '|' + (flipped() ? 1 : 0); if (c.dataset.sig !== sig) { c.innerHTML = (v ? pieceSvg(v) : '') + (f === 0 ? '' + label(s)[1] + '' : '') + (r === SIZE - 1 ? '' + label(s)[0] + '' : ''); c.dataset.sig = sig; } var name = label(s), text = v ? T('chess.sq.piece', { sq: name, color: sideName(v > 0 ? 0 : 1).toLowerCase(), piece: T('makruk.piece.' + Math.abs(v)) }) : T(targets.hasOwnProperty(s) ? 'chess.sq.target' : 'chess.sq.empty', { sq: name }); c.setAttribute('aria-label', s === sel ? T('chess.sq.selected', { label: text }) : text); } var count = [0, 0]; for (var s2 = 0; s2 < N; s2++) if (pos.sq[s2]) count[pos.sq[s2] > 0 ? 0 : 1]++; var names = G.names(sideName(0), sideName(1)); score(names, count.map(function (n) { return T('cards.n.piece', { n: n }); }), G.over ? -1 : turnIdx()); // what the number counts is said flipBtn.setAttribute('aria-pressed', String(flipped())); G.setUndo(played.length > 0); G.setHint(open); } var WHY = { stalemate: 'makruk.why.stalemate', repetition: 'xiangqi.why.repetition', limit: 'makruk.why.limit' }; function finish(st) { sel = -1; targets = {}; tip = null; var msg, out, name; if (st.result === 'mate') { var wi = st.winner === WHITE ? 0 : 1; msg = T('chess.why.mate'); out = G.vsComputer() ? (G.human(wi) ? 'w' : 'l') : null; name = sideName(wi); } else { msg = T(WHY[st.result]); out = G.vsComputer() ? 'd' : null; } G.finish(msg, out, name); paint(); } function afterMove(note) { sel = -1; targets = {}; tip = null; var st = E.status(pos, keys), p = turnIdx(); if (st.over) { finish(st); return; } paint(); var line = !G.vsComputer() ? T('board.toMove', { name: sideName(p) }) : G.human(p) ? T('makruk.yourTurn', { side: sideName(p).toLowerCase() }) : T('shell.thinking'); if (st.check) line = T('chess.check', { line: line }); G.say(note ? UI.noted(note, line) : line, !G.human(p)); if (!G.human(p)) computerTurn(); } function play(m) { var who = nameOf(turnIdx()), text = E.san(pos, m); played.push(m); var was = root.ZimiPieceIcons.snap(cells, sqAt, pos.sq); E.make(pos, m); keys.push(E.key(pos)); lastMove = m; afterMove(T(G.vsComputer() ? 'makruk.played' : 'makruk.playedTwo', { name: who, move: text + (E.isPromo(m) ? T('makruk.promoted') : '') })); root.ZimiPieceIcons.ghosts(was, cells, sqAt, pos.sq, E.from(m)); } // The level our own engine plays at: the one chosen, or its best when Strong or Master were chosen and Fairy-Stockfish is not there. function jsLevel() { return F.wants(G.settings.level) ? 'hard' : G.settings.level; } function askLocal(token) { brain.ask({ cmd: 'move', fen: E.toFen(pos), level: jsLevel() }, function (r) { if (!G.done(token)) return; var legal = E.legalMoves(pos); play(r && legal.indexOf(r.move) >= 0 ? r.move : legal[0]); }); } // Strong and Master: Fairy-Stockfish in its own Worker, started by the first move that needs it. If it cannot start, say so // once and play on with our own best level (jsLevel). function computerTurn() { var token = G.begin(); paint(); if (!F.wants(G.settings.level)) { askLocal(token); return; } F.ask('makruk', E, pos, played, G.settings.level, function (move, why) { if (!G.current(token)) return; if (move !== null) { G.done(token); play(move); return; } if (why === 'unavailable') G.say(T('board.fairyNote', { thinking: T('shell.thinking') }), true); askLocal(token); }); } function hint() { var token = G.begin(); paint(); G.say(T('board.thinkingHint'), true); brain.ask({ cmd: 'move', fen: E.toFen(pos), level: 'hard', ms: HINT_MS }, function (r) { if (!G.done(token)) return; tip = r && r.move ? [E.from(r.move), E.to(r.move)] : null; paint(); G.say(tip ? T('makruk.hint', { move: E.san(pos, r.move) }) : T('board.noHint')); }); } function movesFrom(s) { var out = {}; E.legalMoves(pos).forEach(function (m) { if (E.from(m) === s) out[E.to(m)] = true; }); return out; } function onSquare(e) { if (G.over || G.thinking || !G.human(turnIdx())) return; var s = sqAt(+e.currentTarget.dataset.d), v = pos.sq[s]; if (sel >= 0 && targets.hasOwnProperty(s)) { play(E.findMove(pos, sel, s)); return; } if (v * pos.turn > 0 && s !== sel) { sel = s; targets = movesFrom(s); tip = null; if (Object.keys(targets).length) G.say(T('board.chooseWhere')); else { sel = -1; G.refuse(e.currentTarget, T('chess.cantMove')); } } else { sel = -1; targets = {}; } paint(); } function undo() { if (!played.length) return; G.cancel(); G.over = false; do { E.unmake(pos); played.pop(); keys.pop(); } while (played.length && G.vsComputer() && !G.human(turnIdx())); lastMove = played[played.length - 1] || 0; afterMove(); } function newGame() { sel = -1; targets = {}; lastMove = 0; tip = null; manualFlip = false; F.cancel(); pos = E.startPosition(); keys = [E.key(pos)]; played = []; afterMove(); } flipBtn.addEventListener('click', function () { manualFlip = !manualFlip; paint(); }); document.addEventListener('keydown', function (e) { if (e.ctrlKey || e.metaKey || e.altKey || e.target.closest('.ar-sheet,input')) return; if (e.key.toLowerCase() === 'f') { manualFlip = !manualFlip; paint(); } else if (e.key === 'Escape' && sel >= 0) { sel = -1; targets = {}; paint(); } }); if (root.ZimiShell) { root.ZimiShell.settings.on(function (k) { if (k === 'hints' || k === '*') paint(); }); root.ZimiShell.game({ hints: true, inProgress: function () { return !G.over && played.length > 0; }, help: function () { return { extra: [root.ZimiPieceIcons.key(T('shell.help.pieces'), [6, 4, 5, 3, 2, 1].map(function (t) { return { html: pieceSvg(t), name: T('makruk.piece.' + t) }; }))] }; } }); } buildBoard(); G.newGame(); G.persist({ save: function () { return played.length ? { played: played.slice(), flip: manualFlip } : null; }, load: function (d) { pos = E.startPosition(); keys = [E.key(pos)]; played = []; d.played.forEach(function (m) { E.make(pos, m); played.push(m); keys.push(E.key(pos)); }); lastMove = played[played.length - 1] || 0; sel = -1; targets = {}; tip = null; manualFlip = !!d.flip; paint(); }, resume: function () { afterMove(); } }); if (new URLSearchParams(location.search).has('debug')) { root.__mk = { engine: E, pos: function () { return pos; }, play: play, usingWorker: function () { return brain.usingWorker(); }, setFen: function (fen) { pos = E.parseFen(fen); keys = [E.key(pos)]; played = []; lastMove = 0; G.over = false; afterMove(); } }; } })(typeof globalThis !== 'undefined' ? globalThis : this);