Re: 將製作ejcees(中國象棋打譜程式)
发表于 : 2026年 3月 30日 21:10
跟上fast一步一步調:
在if(!dc)內,判斷本行是否為最後一行,或下一行是否為!/^\d+\.且含有圈圈數字的行
代码: 全选
// ... 之前的代碼
// 這裡新增預校驗邏輯
// 正則解釋:
// ^[RNBAKCPrnbakcp+\-=1-9]{2} : 前兩個字符必須是 棋子代號、正負號或數字
// [+\-=] : 第三個字符必須是 + - 或 =
// [1-9] : 第四個字符必須是 1-9 的數字
const moveFormatRect = /^[RNBAKCPrnbakcp+\-=1-9]{2}[+\-=][1-9]/;
if (moveFormatRect.test(moveEn)) {
// 只有校驗通過,才嘗試轉換坐標
dc = deriveCoordsFromMove(currentFen, moveEn, isRed);
} else {
// 校驗不通過,直接設為 null,確保進入後面的註解處理邏輯
dc = null;
}
if (!dc) {
// 如果 dc 為空(校驗失敗或轉換失敗),則視為評論
if (!currentCommentNode.c) currentCommentNode.c = "";
currentCommentNode.c += (currentCommentNode.c ? " " : "") + token;
continue;
}
// ... 後續正常處理 dc 的代碼代码: 全选
if (!dc) {
// 檢查:1. 是否為最後一行; 2. 或者下一行不以數字點開頭且包含圈圈數字
let isLastLine = (i === lines.length - 1);
let nextLineIsBranch = false;
if (!isLastLine) {
let nextLine = lines[i + 1];
// 判斷下一行:不匹配 ^\d+\. 且 包含圈圈數字
nextLineIsBranch = !/^\d+\./.test(nextLine) && /[①-⑳]/.test(nextLine);
}
if (isLastLine || nextLineIsBranch) {
// 如果滿足條件,這裡可以執行特定邏輯,例如中斷解析或特殊標記
// 目前默認行為仍是轉為評論
}
// 原有的轉評論邏輯
if (!currentCommentNode.c) currentCommentNode.c = "";
currentCommentNode.c += (currentCommentNode.c ? " " : "") + token;
continue;
}代码: 全选
正則優化: /[①-⑳]/ 是 /[①②③④⑤⑥⑦⑧⑨⑩⑪⑫⑬⑭⑮⑯⑰⑱⑲⑳]/ 的簡寫,更加簡潔。