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

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

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

帖子 ejsoon »

https://ejsoon.vip/
弈趣極光:享受思維樂趣
头像
ejsoon
圈圈精英
圈圈精英
帖子: 4641
注册时间: 2022年 11月 18日 17:36
为圈友点赞: 172 次
被圈友点赞: 201 次
联系:

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

帖子 ejsoon »

問:undo和redo存在問題。

代码: 全选

            // Deep clone the entire game tree for undo/redo
            function cloneGameState() {
                return {
                    historyFEN: JSON.parse(JSON.stringify(historyFEN)),
                    currentBranch: [...currentBranch],
                    currentStepIndex: currentStepIndex
                };
            }

            function saveStateToUndo() {
                undoStack.push(cloneGameState());
                if (undoStack.length > 50) undoStack.shift(); // limit history size
                redoStack = []; // clear redo on new action
            }

            function restoreState(state) {
                historyFEN = state.historyFEN;
                currentBranch = state.currentBranch;
                currentStepIndex = state.currentStepIndex;
                renderRecordUI();
                renderNoteUI();
                jumpToStep(currentStepIndex);
            }

            function undo() {
                if (undoStack.length === 1) return;
                const previous = undoStack.pop();
                redoStack.push(previous);
                restoreState(undoStack[undoStack.length - 1]);
            }

            function redo() {
                if (redoStack.length === 0) return;
                const next = redoStack.pop();
                undoStack.push(next);
                restoreState(next);
            }

在以上代碼中,如果我走到第二步,之後走一下undo和redo,然後往後走到第四步或更多,當我undo回到第二步時,historyFEN跟當前最後一步是一樣的。

懷疑是深複製存在問題。

請查找問題原因,並說出如何修複。
https://ejsoon.vip/
弈趣極光:享受思維樂趣
头像
ejsoon
圈圈精英
圈圈精英
帖子: 4641
注册时间: 2022年 11月 18日 17:36
为圈友点赞: 172 次
被圈友点赞: 201 次
联系:

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

帖子 ejsoon »

https://ejsoon.vip/
弈趣極光:享受思維樂趣
头像
ejsoon
圈圈精英
圈圈精英
帖子: 4641
注册时间: 2022年 11月 18日 17:36
为圈友点赞: 172 次
被圈友点赞: 201 次
联系:

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

帖子 ejsoon »

上面的按鈕:

代码: 全选

<div class="ejceestool">
                <div class="tool-btn" id="tool-new" title="New">
                </div>
                <div class="tool-btn" id="tool-open" title="Open">
                </div>
                <div class="tool-btn" id="tool-edit" title="Edit">
                </div>
                <div class="tool-btn" id="tool-save" title="Save">
                </div>
                <div class="tool-btn" id="tool-undo" title="Undo">
                </div>
                <div class="tool-btn" id="tool-redo" title="Redo">
                </div>
                <div class="tool-btn" id="tool-rotate" title="Rotate">
                </div>
                <div class="tool-btn" id="tool-flip" title="Flip">
                </div>
                <div class="tool-btn" id="tool-exp-txt" title="Export Text">
                </div>
                <div class="tool-btn" id="tool-exp-svg" title="Export SVG">
                </div>
            </div>
下面的按鈕:

代码: 全选

                            <div class="note-btn" id="btn-del-move" title="Delete Move">
                            </div>
                            <div class="note-btn" id="btn-range" title="Range Selection">
                            </div>
                            <div class="note-btn" id="btn-autoplay" title="Auto Play">
                            </div>
                            <div class="note-btn" id="btn-speed" title="Move Speed" style="width: auto; padding: 0 4px; font-weight: bold;">
                                0.36s
                            </div>
                        
https://ejsoon.vip/
弈趣極光:享受思維樂趣
头像
ejsoon
圈圈精英
圈圈精英
帖子: 4641
注册时间: 2022年 11月 18日 17:36
为圈友点赞: 172 次
被圈友点赞: 201 次
联系:

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

帖子 ejsoon »

前面的html結構:

代码: 全选

            <div class="ejceesoutput">Click on the SVG area to get coordinates.</div>
            <div class="ejceesstep">
                <div class="ejceesstepminus">-</div>
                <input type="range" class="ejceesstepdrop" min="0" max="0" value="0">
                <div class="ejceesstepplus">+</div>
            </div>
下面的結構:

代码: 全选

<div class="ejceestext">
                <div class="ejceesrecord">
                    <div class="ejceesrcdstart" id="record-start">棋局開始,紅方先行</div>

<div class="ejceesrcdstep" id="step-record-1" data-index="1"><span class="ejceesrcdstepcontent">炮二平五</span></div>

                </div>
                <div class="ejceesnote">
                    <div class="ejceescomment"></div>
                    <div class="ejceestextbtn"></div>
                </div>
            </div>
https://ejsoon.vip/
弈趣極光:享受思維樂趣
回复

在线用户

正浏览此版面之用户: ejsoon 和 3 访客