docs: cite specific perft reference sources, license and scope Replace the vague ‘public reference values’ wording with explicit citations: Chess Programming Wiki ‘Chinese Chess Perft Results’ (CC BY-SA 3.0, initial position depths 1-8), cross-checked against Nguyen Pham’s TalkChess thread (2026-01-11) which extends the initial position to depth 10 and agrees with Pikafish (GPL-3.0). State the exact start FEN, the depths covered by this library (1-3), and the full reference chain. Bump version to 0.2.3 so the mooncakes page picks up the corrected readme.
docs: cite specific perft reference sources, license and scope
Replace the vague ‘public reference values’ wording with explicit citations: Chess Programming Wiki ‘Chinese Chess Perft Results’ (CC BY-SA 3.0, initial position depths 1-8), cross-checked against Nguyen Pham’s TalkChess thread (2026-01-11) which extends the initial position to depth 10 and agrees with Pikafish (GPL-3.0). State the exact start FEN, the depths covered by this library (1-3), and the full reference chain. Bump version to 0.2.3 so the mooncakes page picks up the corrected readme.
MoonBit 生态的中国象棋规则基础库:零依赖、纯逻辑、可编译到 wasm-gc / js / native 全后端。它不绑定任何界面与运行方式,只负责回答两个问题——哪些走法合法,走完之后局面处于什么状态。AI 引擎、对弈平台、教学软件、棋谱工具,都以它为规则内核,不必各自重写蹩马腿、塞象眼、将帅照面这些细节。
wasm-gc
js
native
moon add wbgxiaosu/xiangqi
公共接口由 moon info 生成的 pkg.generated.mbti 定义,类型安全、所见即所得:
moon info
pkg.generated.mbti
Board::legal_moves
pseudo_moves
pseudo_moves_from
in_check
is_checkmate
is_stalemate
kings_facing
result
move_from_chinese
move_to_chinese
move_from_iccs
move_to_iccs
parse_fen
Board::to_fen
perft
parse_position
move_to_ucci
validate_move_line
ucci
GameHistory::new
push
position_repetitions
check_streak
long_check_side
Board::get
with_piece
without
piece_count
// 走法生成与状态判定 let board = @xiangqi.Board::initial() let moves = board.legal_moves() // 开局 44 种合法着法 let mv = board.move_from_chinese("炮二平五").unwrap() // 解析为 h2e2 let next = board.apply_move(mv) // 返回新棋盘,原局面不变 println(board.in_check(@xiangqi.Red)) // 将军判定 println(next.to_fen()) // 导出 FEN // 棋谱双向转换 println(board.move_to_chinese(mv)) // 炮二平五 println(board.move_to_iccs(mv)) // h2e2 // 从 FEN 恢复任意局面 let mid = @xiangqi.parse_fen("2k6/9/9/9/9/9/9/9/4K4/9 w - - 0 1")
UCCI 适配与长将判定(wbgxiaosu/xiangqi/ucci 子包 + 根包 GameHistory):
wbgxiaosu/xiangqi/ucci
GameHistory
// 解析 UCCI position 命令,直接得到对局局面 let board = @ucci.parse_position("position startpos moves h2e2 h9g7") // 长将判负(中国象棋竞赛规则):连续将军 + 局面重复 let history = @xiangqi.GameHistory::new(board) // 每走一步后:history.push(next) // history.long_check_side(min_repeats=2, min_streak=3) 返回违规一方
走法生成是所有象棋软件最容易藏 bug 的环节,本库用 perft(穷举式节点计数)逐层校验。 对拍局面为标准初始局面(FEN rnbakabnr/9/1c5c1/p1p1p1p1p/9/9/P1P1P1P1P/1C5C1/9/RNBAKABNR w - - 0 1), 本库对拍深度 1–3:
rnbakabnr/9/1c5c1/p1p1p1p1p/9/9/P1P1P1P1P/1C5C1/9/RNBAKABNR w - - 0 1
另有 42 个单元测试覆盖边界规则(蹩马腿、塞象眼、炮架、九宫、过河兵、将帅照面、 长将判定、UCCI 回放),CI 全程回归。
apply_move
is_attacked
.mbti
legal_moves
moon check # 静态检查(零警告) moon test # 42 个测试(含 perft 回归 / UCCI 回放 / 长将判定) moon fmt # 格式化 moon info # 重新生成 .mbti 接口文件
仓库自带一个最小可执行示例(解析开局 FEN、枚举合法着法、按中文记谱走两步并渲染棋盘), 用于验证库在本地环境可正常运行:
git clone https://github.com/wbgxiaosu/xiangqi cd xiangqi moon run cmd/main
预期输出:初始局面棋盘、开局合法着法数: 44(与 perft(1) 一致)、 炮二平五 (h2e2) 与 马8进7 (h9g7) 两次走法及其后局面。
开局合法着法数: 44
炮二平五 (h2e2)
马8进7 (h9g7)
Apache-2.0
中国象棋规则引擎库:完整走法生成、将军/将死判定、中文纵线记谱与FEN,零依赖纯 MoonBit 实现。Xiangqi rules engine in pure MoonBit.
版权所有:中国计算机学会技术支持:开源发展技术委员会 京ICP备13000930号-9 京公网安备 11010802047560号
xiangqi · MoonBit 中国象棋规则引擎库
MoonBit 生态的中国象棋规则基础库:零依赖、纯逻辑、可编译到
wasm-gc/js/native全后端。它不绑定任何界面与运行方式,只负责回答两个问题——哪些走法合法,走完之后局面处于什么状态。AI 引擎、对弈平台、教学软件、棋谱工具,都以它为规则内核,不必各自重写蹩马腿、塞象眼、将帅照面这些细节。安装
API 一览
公共接口由
moon info生成的pkg.generated.mbti定义,类型安全、所见即所得:Board::legal_moves/pseudo_moves/pseudo_moves_fromin_check/is_checkmate/is_stalemate/kings_facing/resultmove_from_chinese/move_to_chinese(炮二平五、前车进一)move_from_iccs/move_to_iccs(h2e2 风格)parse_fen/Board::to_fen(XiangqiFEN 标准)perftparse_position/move_to_ucci/validate_move_line(ucci子包)GameHistory::new/push/position_repetitions/check_streak/long_check_sideBoard::get/with_piece/without/piece_count等用法示例
UCCI 适配与长将判定(
wbgxiaosu/xiangqi/ucci子包 + 根包GameHistory):正确性保证
走法生成是所有象棋软件最容易藏 bug 的环节,本库用 perft(穷举式节点计数)逐层校验。 对拍局面为标准初始局面(FEN
rnbakabnr/9/1c5c1/p1p1p1p1p/9/9/P1P1P1P1P/1C5C1/9/RNBAKABNR w - - 0 1), 本库对拍深度 1–3:Perft 参考来源
另有 42 个单元测试覆盖边界规则(蹩马腿、塞象眼、炮架、九宫、过河兵、将帅照面、 长将判定、UCCI 回放),CI 全程回归。
设计决策
apply_move返回新局面,原局面不变,天然适合做搜索树与撤销move_from_chinese与move_to_chinese永远互逆is_attacked从目标格反推攻击子,正确处理马腿与炮架遮挡.mbti接口文件中,升级版本时 diff 即可审查兼容性典型集成
legal_moves+apply_move构成搜索树的展开层,配一层 UCCI 协议即可接入象棋界面与现有引擎开发
可复现演示
仓库自带一个最小可执行示例(解析开局 FEN、枚举合法着法、按中文记谱走两步并渲染棋盘), 用于验证库在本地环境可正常运行:
预期输出:初始局面棋盘、
开局合法着法数: 44(与 perft(1) 一致)、炮二平五 (h2e2)与马8进7 (h9g7)两次走法及其后局面。License
Apache-2.0