將製作ejcees(中國象棋打譜程式)

分享和讨论Javascript相关的话题
回复
头像
ejsoon
圈圈精英
圈圈精英
帖子: 4916
注册时间: 2022年 11月 18日 17:36
为圈友点赞: 173 次
被圈友点赞: 202 次
联系:

Re: 將製作ejcees(中國象棋打譜程式)

帖子 ejsoon »

跟上fast一步一步調:

代码: 全选

// ... 之前的代碼

// 這裡新增預校驗邏輯
// 正則解釋:
// ^[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)內,判斷本行是否為最後一行,或下一行是否為!/^\d+\.且含有圈圈數字的行

代码: 全选

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; 
}

代码: 全选

正則優化: /[①-⑳]/ 是 /[①②③④⑤⑥⑦⑧⑨⑩⑪⑫⑬⑭⑮⑯⑰⑱⑲⑳]/ 的簡寫,更加簡潔。
https://ejsoon.vip/
弈趣極光:享受思維樂趣
头像
ejsoon
圈圈精英
圈圈精英
帖子: 4916
注册时间: 2022年 11月 18日 17:36
为圈友点赞: 173 次
被圈友点赞: 202 次
联系:

Re: 將製作ejcees(中國象棋打譜程式)

帖子 ejsoon »

路fast調(二):

代码: 全选

function importExportedText(text, importedFen = null) {

代码: 全选

if (!dc) {
    const isLastLine = (i === lines.length - 1);
    const nextLineIsBranch = !isLastLine && !/^\d+\./.test(lines[i + 1]) && /[①-⑳]/.test(lines[i + 1]);

    if (isLastLine || nextLineIsBranch) {
        const cleanInitial = INITIAL_FEN.replace(/ - - 0 1$/, '');
        const currentFenClean = importedFen ? importedFen.replace(/ - - 0 1$/, '') : "";

        // 如果當前 FEN 不是初始 FEN,則嘗試用初始 FEN 重啟導入
        if (currentFenClean !== cleanInitial && currentFenClean !== INITIAL_FEN) {
            importExportedText(text, INITIAL_FEN);
            return; 
        } else {
            // 如果已經是初始 FEN 導入卻仍然失敗,則回退狀態並退出
            undo();
            return;
        }
    }

    // 否則,按原邏輯轉為評論
    if (!currentCommentNode.c) currentCommentNode.c = "";
    currentCommentNode.c += (currentCommentNode.c ? " " : "") + token;
    continue;
}

代码: 全选

// 在 let importedFen = null; 之後添加
if (importedFen) {
    // 如果是第二次調用,直接使用傳入的 INITIAL_FEN
    movesStr = text.replace(new RegExp(`position fen | moves |${fenRegex.source}`, 'g'), '').trim();
} else {
    // 原有的 FEN 提取邏輯...
    const fenMatch = text.match(fenRegex);
    if (fenMatch) { 
        importedFen = fenMatch[0];
        // ... 其餘代碼
    }
}
https://ejsoon.vip/
弈趣極光:享受思維樂趣
头像
ejsoon
圈圈精英
圈圈精英
帖子: 4916
注册时间: 2022年 11月 18日 17:36
为圈友点赞: 173 次
被圈友点赞: 202 次
联系:

Re: 將製作ejcees(中國象棋打譜程式)

帖子 ejsoon »

ejcees20260330impsthbug9997765:

實現隨意導入東萍格式棋譜,而不必先按「新建」。導入可以有分支和評論。

修復兩個undo按鈕不亮的地方,一個是點擊step後,一個是走完棋後。

https://ejsoon.vip/wp-content/uploads/2 ... 97765.html
附件
ejcees20260330impsthbug9997765.7z
(42.84 KiB) 已下载 5 次
https://ejsoon.vip/
弈趣極光:享受思維樂趣
头像
ejsoon
圈圈精英
圈圈精英
帖子: 4916
注册时间: 2022年 11月 18日 17:36
为圈友点赞: 173 次
被圈友点赞: 202 次
联系:

Re: 將製作ejcees(中國象棋打譜程式)

帖子 ejsoon »

新增一個函式,其功能為,搜尋行動方所有可能的合規走法,剔除下一步會被對方吃到將帥以及將帥照面的情況。

其用途有三個,一是判斷勝負,當一方當行動結束時作被將死或被困斃的判斷;二是當編輯結束時,如果行動方能立即獲勝,或者行動方無子可走,則彈出提示;三是在導出文本狀態時,當第三個按鈕engin-txt處於active狀態時,再次按engin-txt,則加上\n+seachmoves a1b2 c3b4。「a1b2 c3b4」指所有可能走法。再次點擊則切換回來

只需給出所要修改的地方,所有的代碼和注釋都應使用英文。

https://ejsoon.vip/wp-content/uploads/2 ... rchIt.html
附件
geminiSearchIt.7z
(71.02 KiB) 已下载 5 次
https://ejsoon.vip/
弈趣極光:享受思維樂趣
头像
ejsoon
圈圈精英
圈圈精英
帖子: 4916
注册时间: 2022年 11月 18日 17:36
为圈友点赞: 173 次
被圈友点赞: 202 次
联系:

Re: 將製作ejcees(中國象棋打譜程式)

帖子 ejsoon »

接著前面的改動:
一,當一方被將軍的時候,需要在原來output的文字後面加上「 check!」
二,當checkmate時,也是在原來的文字提示的後面加上「 checkmate」。

只需給出要修改的地方,所有的代碼和注釋都要使用英文。



gemini pro

Here is the modification to update the check and checkmate text appending logic.

Find the movePieceWithAnimation function. Locate the Checkmate / Stalemate Detection block we added previously inside the onComplete callback (around line 1250), and replace it entirely with this updated block:

代码: 全选

// --- Added Logic: Checkmate / Stalemate / Check Detection ---
                        const nextTurnIsRed = (currentTurn === 'w');
                        const legalMoves = getAllLegalMoves(nextTurnIsRed, tileMap);
                        
                        let isCheck = false;
                        let myKingPos = null;
                        
                        // Find the King of the next player to determine if they are in check
                        for (const [pos, id] of tileMap.entries()) {
                            if ((nextTurnIsRed && id[0] === 'K') || (!nextTurnIsRed && id[0] === 'k')) {
                                myKingPos = pos.split(',').map(Number);
                                break;
                            }
                        }
                        
                        // Determine if the King is currently under attack
                        if (myKingPos && isSquareAttacked(myKingPos[0], myKingPos[1], !nextTurnIsRed, tileMap)) {
                            isCheck = true;
                        }
                        
                        // Get the output element to append the status to the existing text
                        const outputElement = document.querySelector('.ejceesoutput');
                        
                        if (legalMoves.length === 0) {
                            if (isCheck) {
                                if (outputElement) outputElement.textContent += " checkmate";
                                setTimeout(() => alert("Game Over: Checkmate!"), 50);
                            } else {
                                if (outputElement) outputElement.textContent += " stalemate";
                                setTimeout(() => alert("Game Over: Stalemate!"), 50);
                            }
                        } else if (isCheck) {
                            // Append check status if the game is not over
                            if (outputElement) outputElement.textContent += " check!";
                        }
                        // ------------------------------------------------------------
https://ejsoon.vip/
弈趣極光:享受思維樂趣
头像
ejsoon
圈圈精英
圈圈精英
帖子: 4916
注册时间: 2022年 11月 18日 17:36
为圈友点赞: 173 次
被圈友点赞: 202 次
联系:

Re: 將製作ejcees(中國象棋打譜程式)

帖子 ejsoon »

在前面的基礎上,在jumpToStep()函式裡,也要加上對check和checkmate的檢查。

Here is the modification to add the check, checkmate, and stalemate detection to the jumpToStep function.

Note: I have deliberately omitted the alert popups here, as it would be very annoying to get a popup every time you scroll through the game history and land on the final move. The text will simply update in the output bar.

Find the jumpToStep function (around line 935) and modify its inner if (stepData) block:

代码: 全选

const totalDepth = getPathDepth(historyFEN, currentBranch);
        if (index >= 0 && index < totalDepth) {
            currentStepIndex = index;
            stepSlider.value = index;

            const stepData = getNodeAtStep(index);
            if (stepData) {
                loadFEN(stepData.fen);
                applyLastMoveVisuals(stepData.lastMove);
                highlightActiveStep(index);

                restoreOutputText();
                renderNoteUI();

                // --- Added Logic: Checkmate / Stalemate / Check Detection on Jump ---
                // We do not trigger alerts here to avoid spamming the user when navigating history
                const nextTurnIsRed = (currentTurn === 'w');
                const legalMoves = getAllLegalMoves(nextTurnIsRed, tileMap);
                
                let isCheck = false;
                let myKingPos = null;
                
                // Find the King of the next player to determine if they are in check
                for (const [pos, id] of tileMap.entries()) {
                    if ((nextTurnIsRed && id[0] === 'K') || (!nextTurnIsRed && id[0] === 'k')) {
                        myKingPos = pos.split(',').map(Number);
                        break;
                    }
                }
                
                // Determine if the King is currently under attack
                if (myKingPos && isSquareAttacked(myKingPos[0], myKingPos[1], !nextTurnIsRed, tileMap)) {
                    isCheck = true;
                }
                
                // Get the output element to append the status to the existing text
                const outputElement = document.querySelector('.ejceesoutput');
                
                if (legalMoves.length === 0) {
                    if (isCheck) {
                        if (outputElement) outputElement.textContent += " checkmate";
                    } else {
                        if (outputElement) outputElement.textContent += " stalemate";
                    }
                } else if (isCheck) {
                    if (outputElement) outputElement.textContent += " check!";
                }
                // --------------------------------------------------------------------
            }
        }
https://ejsoon.vip/
弈趣極光:享受思維樂趣
头像
ejsoon
圈圈精英
圈圈精英
帖子: 4916
注册时间: 2022年 11月 18日 17:36
为圈友点赞: 173 次
被圈友点赞: 202 次
联系:

Re: 將製作ejcees(中國象棋打譜程式)

帖子 ejsoon »

附件
ejcees20260331posible999777.7z
(43.6 KiB) 已下载 4 次
https://ejsoon.vip/
弈趣極光:享受思維樂趣
头像
ejsoon
圈圈精英
圈圈精英
帖子: 4916
注册时间: 2022年 11月 18日 17:36
为圈友点赞: 173 次
被圈友点赞: 202 次
联系:

Re: 將製作ejcees(中國象棋打譜程式)

帖子 ejsoon »

附件
ejcees20260331staticclass9997773.7z
(43.61 KiB) 已下载 4 次
https://ejsoon.vip/
弈趣極光:享受思維樂趣
头像
ejsoon
圈圈精英
圈圈精英
帖子: 4916
注册时间: 2022年 11月 18日 17:36
为圈友点赞: 173 次
被圈友点赞: 202 次
联系:

Re: 將製作ejcees(中國象棋打譜程式)

帖子 ejsoon »

根據上傳的代碼,做一個獨立的ejceesexpsvg函式,它將能根據傳入的參數,導出單張svg圖片或svg動畫。

function ejceesexpsvg(txt, interval=2, speed=0.6)

其中txt是所有支持導入的文本格式,interval是每步時間間隔,speed是移動速度。

只需返回svg文本。

注意,這個獨立函式將不依賴任何外部資源。你給出的代碼中的svg.ejceespb部分可以省略。如果是需要用到的原來的函式,只需簡單說要用到,不必全部給出。如果是原來的函式需要調整,則告知具體要修改的地方。

所有的代碼和注釋都要是英文。

https://ejsoon.vip/wp-content/uploads/2 ... xpsvg.html
附件
geminiEjceesexpsvg.7z
(102.76 KiB) 已下载 2 次
https://ejsoon.vip/
弈趣極光:享受思維樂趣
头像
ejsoon
圈圈精英
圈圈精英
帖子: 4916
注册时间: 2022年 11月 18日 17:36
为圈友点赞: 173 次
被圈友点赞: 202 次
联系:

Re: 將製作ejcees(中國象棋打譜程式)

帖子 ejsoon »

新建特定局面時,rotate不應更改。
https://ejsoon.vip/
弈趣極光:享受思維樂趣
头像
ejsoon
圈圈精英
圈圈精英
帖子: 4916
注册时间: 2022年 11月 18日 17:36
为圈友点赞: 173 次
被圈友点赞: 202 次
联系:

Re: 將製作ejcees(中國象棋打譜程式)

帖子 ejsoon »

在自動播放時,也要顯示check和checkmate的提示文字。



gemini pro:

Here is the modification to add the check, checkmate, and stalemate detection to the auto-play functionality. It also ensures that the auto-play automatically stops if the game reaches a checkmate or stalemate state.

Find the playNextAnimated() function (around line 2580). Locate the movePieceWithAnimation callback inside it, and replace that block with the following:

代码: 全选

// Trigger animation; callback runs only after the piece has finished moving
            movePieceWithAnimation(pieceId, moveData.endX, moveData.endY, eatenId, () => {
                // Animation complete: sync turn and visuals, advance step
                currentTurn = currentTurn === 'w' ? 'b' : 'w';
                applyLastMoveVisuals(moveData);
                currentStepIndex = nextStepIndex;
                stepSlider.value = currentStepIndex;
                highlightActiveStep(currentStepIndex);
                renderNoteUI();

                restoreOutputText();

                // --- Added Logic: Checkmate / Stalemate / Check Detection during Auto-Play ---
                const nextTurnIsRed = (currentTurn === 'w');
                const legalMoves = getAllLegalMoves(nextTurnIsRed, tileMap);
                
                let isCheck = false;
                let myKingPos = null;
                
                // Find the King of the next player to determine if they are in check
                for (const [pos, id] of tileMap.entries()) {
                    if ((nextTurnIsRed && id[0] === 'K') || (!nextTurnIsRed && id[0] === 'k')) {
                        myKingPos = pos.split(',').map(Number);
                        break;
                    }
                }
                
                // Determine if the King is currently under attack
                if (myKingPos && isSquareAttacked(myKingPos[0], myKingPos[1], !nextTurnIsRed, tileMap)) {
                    isCheck = true;
                }
                
                const outputElement = document.querySelector('.ejceesoutput');
                
                if (legalMoves.length === 0) {
                    if (isCheck) {
                        if (outputElement) outputElement.textContent += " checkmate";
                    } else {
                        if (outputElement) outputElement.textContent += " stalemate";
                    }
                    stopAutoPlay(); // Stop playing if the game is over
                    return;         // Do not schedule the next move
                } else if (isCheck) {
                    if (outputElement) outputElement.textContent += " check!";
                }
                // -----------------------------------------------------------------------------

                // Chain the next animated move after the full moveInterval delay
                setTimeout(playNextAnimated, moveInterval * 1000);
            });
https://ejsoon.vip/
弈趣極光:享受思維樂趣
头像
ejsoon
圈圈精英
圈圈精英
帖子: 4916
注册时间: 2022年 11月 18日 17:36
为圈友点赞: 173 次
被圈友点赞: 202 次
联系:

Re: 將製作ejcees(中國象棋打譜程式)

帖子 ejsoon »

增加功能,讀取以下格式的fen局面:

┌─┬-[象][士][將]-┬─┬-[車]-┐
│ │ │ │\│/│ │ │ │
├─┼─┼─┼-[士]-┼─┼─┼─┤
│ │ │ │/│\│ │ │ │
├─┼-[馬]-┼-[象]-┼-[馬][炮]-┤
│ │ │ │ │ │ │ │ │
[卒]-┼-[卒]-┼-[卒]-┼─┼-(車)[卒]
│ │ │ │ │ │ │ │ │
├-[車]-┴─┴─┴─┴-[卒]-┴─┤
│               │
(兵)-┬-(兵)-┬-(兵)-┬─┬─┬─┤
│ │ │ │ │ │ │ │ │
├─┼─┼-(車)(馬)-┼-(兵)-┼-(兵)
│ │ │ │ │ │ │ │ │
├-(炮)(馬)-┼-(炮)-┼─┼─┼─┤
│ │ │ │\│/│ │ │ │
├─┼─┼─┼-(士)-┼─┼─┼─┤
│ │ │ │/│\│ │ │ │
└─┴-[炮]-┴-(帥)(士)(相)-┴─┘

規則如下:
一,這些字符「┌┘└┐┬├┤┴┼※」表示空位
二,「[]」之中的棋子屬於黑方,「()」之中的棋子屬於紅方。
三,無視「 │─-\//\」等字符 。

探測是否存在這種fen局面格式。方法是匹配「┌」或(紅或[黑。則由匹配到的這個字符開始,到「┘或紅)或黑]為止,都是這種fen局面的字符。「紅」代表「車馬炮仕相帥兵」,「黑」代表「車馬炮士象將卒」。

從第一行開始,每單數行有意義,雙數行可以跳過。

只需給出所要修改的地方,所有的代碼和注釋都要使用英文。
https://ejsoon.vip/
弈趣極光:享受思維樂趣
头像
ejsoon
圈圈精英
圈圈精英
帖子: 4916
注册时间: 2022年 11月 18日 17:36
为圈友点赞: 173 次
被圈友点赞: 202 次
联系:

Re: 將製作ejcees(中國象棋打譜程式)

帖子 ejsoon »

gemini pro:
Here is the modification to add support for parsing the ASCII art board format into a standard FEN string.

Find the importExportedText function (around line 2150). Insert the new ASCII detection and conversion logic right after the initial normalization steps, before it checks for URL formats.

代码: 全选

function importExportedText(text, importedFen = null) {
            text = text.trim();
            if (!text) {
                renderRecordUI();
                renderNoteUI();
                updateToolHighlights();
                return;
            }

            // Normalize Chinese notations
            text = text.replace(/车/g, '車').replace(/马/g, '馬').replace(/帅/g, '帥').replace(/将/g, '將').replace(/后/g, '後').replace(/进/g, '進');
            text = text.replace(/ r(\n|\s|$)/g, ' w$1');

            // --- Added Logic: Check for ASCII Art Board Format ---
            // Matches empty grid intersections or pieces enclosed in [] (Black) or () (Red)
            const asciiPiecePattern = "\\[[車馬炮士仕象相將帥卒兵]\\]|\\([車馬炮士仕象相將帥卒兵]\\)|[┌┘└┐┬├┤┴┼※]";
            const asciiRegex = new RegExp(asciiPiecePattern, 'g');
            const matches = [...text.matchAll(asciiRegex)];
            
            if (matches.length >= 90) {
                // Take the first 90 matches which represent the 10x9 board
                const boardMatches = matches.slice(0, 90);
                
                // Red = uppercase, Black = lowercase. Includes fallbacks for typos (e.g., Red using 士/象)
                const redMap = { '車':'R','馬':'N','炮':'C','仕':'A','士':'A','相':'B','象':'B','帥':'K','兵':'P' };
                const blackMap = { '車':'r','馬':'n','炮':'c','士':'a','仕':'a','象':'b','相':'b','將':'k','卒':'p' };
                
                let fenRows = [];
                for (let i = 0; i < 10; i++) {
                    let rowStr = "";
                    let emptyCount = 0;
                    for (let j = 0; j < 9; j++) {
                        let matchStr = boardMatches[i * 9 + j][0];
                        if (matchStr.startsWith('[')) { // Black piece
                            if (emptyCount > 0) { rowStr += emptyCount; emptyCount = 0; }
                            rowStr += blackMap[matchStr.charAt(1)];
                        } else if (matchStr.startsWith('(')) { // Red piece
                            if (emptyCount > 0) { rowStr += emptyCount; emptyCount = 0; }
                            rowStr += redMap[matchStr.charAt(1)];
                        } else { // Empty square
                            emptyCount++;
                        }
                    }
                    if (emptyCount > 0) rowStr += emptyCount;
                    fenRows.push(rowStr);
                }
                
                // Override the importedFen with the generated one
                importedFen = fenRows.join('/') + " w - - 0 1";
                
                // Remove the ASCII board from the text so it doesn't interfere with subsequent move parsing
                const startIndex = boardMatches[0].index;
                const endIndex = boardMatches[89].index + boardMatches[89][0].length;
                text = text.substring(0, startIndex) + " " + text.substring(endIndex);
            }
            // -----------------------------------------------------

            // Handle URL format directly if pasted
            if (text.includes('#') || text.startsWith('i;') || text.startsWith('i+') || text.startsWith('i@') || /^([a-zA-Z0-9]+,)+[wb]/.test(text)) {
https://ejsoon.vip/
弈趣極光:享受思維樂趣
头像
ejsoon
圈圈精英
圈圈精英
帖子: 4916
注册时间: 2022年 11月 18日 17:36
为圈友点赞: 173 次
被圈友点赞: 202 次
联系:

Re: 將製作ejcees(中國象棋打譜程式)

帖子 ejsoon »

附件
ejcees20260401check9997774.7z
(44.5 KiB) 已下载 1 次
https://ejsoon.vip/
弈趣極光:享受思維樂趣
回复

在线用户

正浏览此版面之用户: 没有注册用户 和 1 访客