@fortemate/dicechess-engine ###################################################################################################################################################################################################################################################
高性能、跨平台游戏引擎和移动生成器 双子棋,被编译为纯Javascript (ES Modules),并附有完整的TypeScript类型声明.
为网页浏览器,混合应用程序,和Node.js服务器中的最大性能所设计.
安装
通过 npm 或您喜欢的软件包管理器从 npmjs.org 安装软件包 。 没有登记册映射或 需要认证令牌 :
npm install @fortemate/dicechess-engine
还可以从 GitHub 软件包。 。 。 .
两个条目: 整个引擎,或仅规则
套件有两个切入点. 他们来自一个建筑 共享他们共同的代码, 所以在同一应用程序中导入两者都加载共享的一半一次.
| 导入 | 它给你什么 | 装满什么 |
|---|---|---|
@fortemate/dicechess-engine | 所有东西: DiceChess, (中文(简体) ), EngineFacade合法倒转的树 花瓶, getBestMove、公平、加倍和决定 | 1,308,539 B (191,262 B gzipped) (英语) |
@fortemate/dicechess-engine/rules | 只有规则: getLegalUciMoves, (中文(简体) ), generateMoves, (中文(简体) ), applyMove, (中文(简体) ), endTurn, (中文(简体) ), perft, (中文(简体) ), getPieceFromDice, (中文(简体) ), canonicalKey | 891 712 B (138 394 B gzipped) (英语) |
使用 /rules 应用程序验证、应用和移动但从未询问引擎
改为 播放 ——活游戏与另一个人,板编辑,分析取景. 职能是:
其名称和行为与 DiceChess 因此,移民是一个进口:
import { DiceChess } from '@fortemate/dicechess-engine/rules';
const dfen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1 PN";
const legalMoves = DiceChess.getLegalUciMoves(dfen);
const nextDfen = DiceChess.applyMove(dfen, "e2", "e4");
两个条目还输出其功能为平面命名出口
(单位:千美元)import { getLegalUciMoves } from '@fortemate/dicechess-engine/rules')),每个都有自己的
类型脚本声明( E)dicechess-rules.d.ts 以给养.
网络大会软件包 @fortemate/dicechess-engine-wasm 已经 没有 /rules 子路径: Scala.js
WebAssembly后端会发出一个单一的模块,这样包只传送完整的API.
快速启动( JavaScript / TypeScript)
软件包作为标准ES模块分发(type: "module") (中文(简体) ).
import { DiceChess } from '@fortemate/dicechess-engine';
// Dice Chess FEN (DFEN) encodes the board state and the current dice pool as the 7th field
// (e.g. "PN" represents a rolled Pawn (P) and Knight (N))
const dfen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1 PN";
// 1. Get all legal moves as a flat array of UCI strings for the DFEN position
const legalMoves = DiceChess.getLegalUciMoves(dfen);
console.log("Legal moves in this turn:", legalMoves);
// e.g. ["e2e3", "e2e4", "b1c3", "b1a3", ...]
// 2. Get every legal turn as a prefix tree of UCI micro-moves. A node without children is a
// complete turn. getLegalUciMoves judges each position in isolation, so a client that follows
// a turn one action at a time walks this tree instead of calling getLegalUciMoves again.
const turns = DiceChess.getLegalTurnTree(dfen);
console.log("Continuations of e2e4:", Object.keys(turns["e2e4"]));
// e.g. ["b1a3", "b1c3", "g1e2", "g1f3", "g1h3"]
// The dice a legal turn can still spend: pass the rolled DFEN and the moves played since, not the
// DFEN after them, because the turn is judged as a whole. A client can dim every other die.
console.log("Playable after e2e4:", DiceChess.getPlayableDice(dfen, ["e2e4"]));
// "N"
// 3. Apply a micro-move. applyMove preserves the active color (White), since a Dice Chess turn
// may consist of multiple micro-moves, and keeps the dice the move did not spend ("N" here).
// Arguments: (dfen, fromSquare, toSquare, optionalPromotionPiece)
const nextDfen = DiceChess.applyMove(dfen, "e2", "e4");
console.log("DFEN after micro-move:", nextDfen);
// Spend the knight die too: the turn ends at a leaf of the tree, here after e2e4 then g1f3.
const turnDfen = DiceChess.applyMove(nextDfen, "g1", "f3");
// 4. Explicitly end the turn when the player has exhausted their dice.
// This toggles the active color to Black, clears the dice pool (the full-move number advances
// only after Black's turn), and clears any stale en-passant targets from the previous turn.
const finalDfen = DiceChess.endTurn(turnDfen);
console.log("DFEN after ending turn:", finalDfen);
// 5. Discover available bots (search algorithms)
const bots = DiceChess.getAvailableBots();
console.log("Available bots:", bots);
// e.g. [ { id: 'random', name: 'Random', description: '...', difficulty: 1, isExperimental: false }, ... ]
// 6. Discover the built-in time-management policies.
const timePolicies = DiceChess.getAvailableTimePolicies();
console.log("Available time policies:", timePolicies);
// [ "empirical-v1", "legacy-linear-v1" ]
// 7. Compute the best sequence of micro-moves using the greedy bot search.
// The bot needs Black's roll: append the dice (lowercase for Black), here a Pawn and a Knight.
// Arguments: (dfen, optionalOptions)
const rolledDfen = `${finalDfen} pn`;
const botResult = DiceChess.getBestMove(rolledDfen, { algorithm: "greedy" });
console.log("Bot moves:", botResult.moves);
// e.g. [ { from: "b8", to: "c6" }, { from: "a7", to: "a5" } ]
// Clock-aware searches use empirical-v1 by default. Pass legacy-linear-v1 to
// reproduce the original allocation for rollback or an A/B comparison.
const timedResult = DiceChess.getBestMove(rolledDfen, {
algorithm: "monte-carlo",
clock: { remainingMs: 180_000, incrementMs: 2_000, moveNumber: 8 },
timePolicy: "empirical-v1",
});
console.log("Effective budget (ms):", timedResult.budgetMs);
// 8. Doubling Cube & Draw Offers (New)
// Check if the bot wants to offer a double before its turn:
const shouldDouble = DiceChess.shouldBotOfferDouble(finalDfen, 1);
// Check if the bot accepts a double proposed by the opponent (Take/Drop):
const acceptDouble = DiceChess.shouldBotAcceptDouble(finalDfen, 2);
// Check if the bot wants to offer a draw:
const offerDraw = DiceChess.shouldBotOfferDraw(finalDfen);
// Check if the bot accepts a draw proposed by the opponent:
const acceptDraw = DiceChess.shouldBotAcceptDraw(finalDfen);
特征
- 完整的国际象棋规则: 执行转弯顺序、解封、晋升路径计算和严格 最大微移动过滤。 。 。 .
- 类型安全: 以手写类型Script定义文件(
.d.ts对于这两个条目,与出口的Scala.js API保持一致. - 侧效果自由配置为
sideEffects: false允许在Vite,Webpack,和Rollup等现代捆绑机上高级摇树. - 快速射击:通过高级Scala.js优化链接器来编译优化位元操作.
API 参考文献
- 交互式用户和开发者指南: 完整规则,建筑文献,以及现场互动视觉目录.
- GitHub 仓库:出处代码,出处追踪器,贡献指南.
许可证
此软件包仅根据 GNU Affero 通用公共许可证 v3. 0 (SPDX) 获得许可 AGPL-3.0-only) (中文(简体) ).