A chess library and engine for the MoonBit ecosystem, with a chess.js-compatible API
moonchess/
├── moon.mod # 模块定义
├── lib/ # 核心库
│ ├── types.mbt # Color / PieceType / Piece / Move / 易位标志
│ ├── square.mbt # 0x88 坐标工具
│ ├── board.mbt # 棋盘存储与攻击检测
│ ├── game.mbt # Chess 对象、FEN、走子/撤销、材料/重复判定
│ ├── movegen.mbt # 走法生成、SAN、对局状态
│ ├── engine.mbt # 评估函数与 α-β 搜索
│ ├── search.mbt # Zobrist 哈希、置换表、迭代加深
│ ├── book.mbt # 开局库
│ ├── pgn.mbt # PGN 导入导出
│ ├── display.mbt # 棋盘渲染
│ └── moonchess_wbtest.mbt # 测试(含 perft 套件)
├── cmd/main/ # 命令行前端
├── examples/demo/ # 可运行示例
├── web/ # 浏览器演示(MoonBit + JS FFI)
├── docs/design.md # 设计说明
└── .github/workflows/ # CI(check / test / fmt / info)moon add poipoiiii/moonchessimport {
"poipoiiii/moonchess/lib" as @lib
}moon check --target native
moon test --target nativelet game = @lib.Chess::new()
ignore(game.move_by_san("e4"))
ignore(game.move_by_san("e5"))
let m = game.move_by_san("Nf3").unwrap()
println(m.san) // Nf3
println(game.fen()) // rnbqkbnr/pppp1ppp/8/4p3/4P3/5N2/PPPP1PPP/RNBQKB1R b KQkq - 1 2
// 列出全部合法走法
for move in game.moves() {
println(move.san)
}
// 简单 α-β 搜索
match game.best_move(4) {
Some(best) => println("best: \{best.san}")
None => println("game over")
}
// 迭代加深 + 置换表
let searcher = @lib.Searcher::new()
let result = searcher.search(game, 6)
println("depth=\{result.depth} score=\{result.score} nodes=\{result.nodes}")moon run cmd/main -- board # 打印棋盘
moon run cmd/main -- perft 4 # perft(4) = 197281
moon run cmd/main -- divide 3 # perft 按首着拆分
moon run cmd/main -- best 4 # 搜索 4 层
moon run cmd/main -- think 6 # 迭代加深 + 置换表
moon run cmd/main -- timed 1500 # 限时 1500ms 搜索
moon run cmd/main -- selfplay 3 20 # 自我对弈(开局用开局库)
moon run cmd/main -- book e4 e5 Nf3 # 查询开局库续着
moon run cmd/main -- validate "<fen>" # FEN 合法性校验
moon run cmd/main -- fen # 打印当前 FENmoon run examples/demo# 1. 构建 JS 产物并复制到 web/moonchess.js
powershell -ExecutionPolicy Bypass -File web/build.ps1
# 2. 起一个静态服务器
python -m http.server 8000 --directory web| 局面 | 深度 1 | 深度 2 | 深度 3 | 深度 4 |
|---|---|---|---|---|
| 初始局面 | 20 | 400 | 8902 | 197281 |
| Kiwipete | 48 | 2039 | 97862 | — |
| Position 3 | 14 | 191 | 2812 | 43238 |
| Position 4 | 6 | 264 | 9467 | — |
| Position 5 | 44 | 1486 | 62379 | — |
| Position 6 | 46 | 2079 | 89890 | — |
Install
Download zipA chess library and engine for the MoonBit ecosystem, with a chess.js-compatible API