Files
rong cb28bbb49e feat: 围棋在线对战(Rust 服务端 + 零构建前端)
- 规则引擎:提子、禁自杀、全局同形(超级劫)、中国规则数子(白贴 7.5 目)、停一手终局
- 服务端:axum + tokio,房间/观战/包干计时/断线重连/语音中继,前端资源内嵌单文件
- 前端:Canvas 木纹棋盘(9/13/19 路、星位、四边坐标、翻转视角)、WebAudio 合成落子声、
  本机双人模式、终局数子面板
- 测试:16 组规则断言 + 5 组 e2e(提子/劫/数子/观战/语音/静态资源)
- 构建缓存遵循 dev-cache-layout:target/ 留项目内并忽略,前端零构建不产生包缓存
2026-10-23 10:12:55 +08:00

240 lines
6.1 KiB
JavaScript

/**
* 围棋规则的浏览器副本(`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);
}