/** * 围棋规则的浏览器副本(`src/rules.rs` 的 JS 镜像) * * 只负责两件事:本地双人模式的即时判定、联机模式下「点一下就知道能不能下」的提示。 * 服务端才是权威 —— 改规则一定要两边一起改。 */ export const MAX_SIZE = 19; /** 中国规则贴目:黑贴 3又3/4 子 = 7.5 目 */ export const KOMI = 7.5; export const EMPTY = 0; export const BLACK = 1; export const WHITE = 2; export function other(c) { return c === 'b' ? 'w' : 'b'; } export function stoneOf(c) { return c === 'b' ? BLACK : WHITE; } export function colorOf(v) { return v === BLACK ? 'b' : v === WHITE ? 'w' : null; } export function newBoard(size) { return new Uint8Array(size * size); } export function idxOf(size, x, y) { return y * size + x; } export function xOf(size, i) { return i % size; } export function yOf(size, i) { return Math.floor(i / size); } export function neighbors(size, i) { const x = xOf(size, i); const y = yOf(size, i); const out = []; if (x > 0) out.push(i - 1); if (x + 1 < size) out.push(i + 1); if (y > 0) out.push(i - size); if (y + 1 < size) out.push(i + size); return out; } /** 一块棋的所有子 + 气数(气按空点去重) */ export function groupInfo(board, size, start) { const color = board[start]; if (!color) return { stones: [], libs: 0 }; const seen = new Uint8Array(board.length); const libSeen = new Uint8Array(board.length); const stack = [start]; seen[start] = 1; const stones = []; let libs = 0; while (stack.length) { const p = stack.pop(); stones.push(p); for (const q of neighbors(size, p)) { if (board[q] === EMPTY) { if (!libSeen[q]) { libSeen[q] = 1; libs++; } } else if (board[q] === color && !seen[q]) { seen[q] = 1; stack.push(q); } } } return { stones, libs }; } /** * 最基础的落子判定:只判禁着点与自杀,不管劫。 * @returns {{board: Uint8Array, captured: number[]} | null} null = 不合法 */ export function play(board, size, at, color) { if (at < 0 || at >= board.length) return null; if (board[at] !== EMPTY) return null; const mine = stoneOf(color); const theirs = stoneOf(other(color)); const nb = board.slice(); nb[at] = mine; const captured = []; for (const q of neighbors(size, at)) { if (nb[q] === theirs) { const g = groupInfo(nb, size, q); if (g.libs === 0) { for (const s of g.stones) { nb[s] = EMPTY; captured.push(s); } } } } if (groupInfo(nb, size, at).libs === 0) return null; // 禁自杀 return { board: nb, captured }; } export function isSuicide(board, size, at, color) { if (board[at] !== EMPTY) return false; return play(board, size, at, color) === null; } /** 中国规则数子:活子 + 只被本方围住的空点 */ export function scoreArea(board, size, komi = KOMI) { const seen = new Uint8Array(board.length); let blackTerr = 0; let whiteTerr = 0; let neutral = 0; for (let i = 0; i < board.length; i++) { if (board[i] !== EMPTY || seen[i]) continue; const stack = [i]; seen[i] = 1; let region = 0; let touchB = false; let touchW = false; while (stack.length) { const p = stack.pop(); region++; for (const q of neighbors(size, p)) { const v = board[q]; if (v === EMPTY) { if (!seen[q]) { seen[q] = 1; stack.push(q); } } else if (v === BLACK) touchB = true; else touchW = true; } } if (touchB && !touchW) blackTerr += region; else if (touchW && !touchB) whiteTerr += region; else neutral += region; } let blackStones = 0; let whiteStones = 0; for (let i = 0; i < board.length; i++) { if (board[i] === BLACK) blackStones++; else if (board[i] === WHITE) whiteStones++; } const black = blackStones + blackTerr; const white = whiteStones + whiteTerr + komi; return { black, white, blackStones, whiteStones, blackTerr, whiteTerr, neutral, komi, winner: black >= white ? 'b' : 'w', margin: Math.abs(black - white), }; } /* ------------------------------ 坐标与编码 ------------------------------ */ /** 围棋习惯用法:跳过字母 I */ export const COLS = 'ABCDEFGHJKLMNOPQRST'; export function coordName(size, i) { return COLS[xOf(size, i)] + String(size - yOf(size, i)); } export function encodeBoard(board) { let s = ''; for (let i = 0; i < board.length; i++) { s += board[i] === BLACK ? 'b' : board[i] === WHITE ? 'w' : '.'; } return s; } export function decodeBoard(str, size) { const b = new Uint8Array(size * size); for (let i = 0; i < b.length; i++) { const c = str[i]; b[i] = c === 'b' || c === 'B' ? BLACK : c === 'w' || c === 'W' ? WHITE : EMPTY; } return b; } export function positionKey(board, turn) { return encodeBoard(board) + turn; } export function isSuperko(keys, key) { return keys.indexOf(key) >= 0; } /* ------------------------------ 让子 / 星位 ------------------------------ */ /** 星位(含天元),按棋盘大小取 */ export function starPoints(size) { if (size === 19) { const e = [3, 9, 15]; const out = []; for (const y of e) for (const x of e) out.push(idxOf(size, x, y)); return out; } if (size === 13) { const e = [3, 6, 9]; const out = []; for (const y of e) for (const x of e) out.push(idxOf(size, x, y)); return out; } if (size === 9) { const e = [2, 4, 6]; const out = []; for (const y of e) for (const x of e) out.push(idxOf(size, x, y)); return out; } return []; } /** 附近最近的交叉点(点击容差) */ export function nearestPoint(size, x, y, radius) { if (x < -radius || y < -radius || x > size - 1 + radius || y > size - 1 + radius) return null; const cx = Math.min(size - 1, Math.max(0, Math.round(x))); const cy = Math.min(size - 1, Math.max(0, Math.round(y))); const dx = x - cx; const dy = y - cy; if (Math.hypot(dx, dy) > radius) return null; return idxOf(size, cx, cy); }