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

分享和讨论Javascript相关的话题
回复
头像
ejsoon
一枝独秀
一枝独秀
帖子: 5398
注册时间: 2022年 11月 18日 17:36
为圈友点赞: 176 次
被圈友点赞: 204 次
联系:

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

帖子 ejsoon »

這段代碼是否需要修改?尤其是getMoveNotation已經不存在了。

代码: 全选

            if (moveStr.length >= 4) {
              let dc = null;
              let moveEn = '';
              let isRed = currentFen.includes(' w');

              if (/^[a-i][0-9][a-i][0-9]$/.test(moveStr)) {
                let startX = moveStr.charCodeAt(0) - 97;
                let startY = 9 - parseInt(moveStr.charAt(1), 10);
                let endX = moveStr.charCodeAt(2) - 97;
                let endY = 9 - parseInt(moveStr.charAt(3), 10);
                dc = {
                  startX,
                  startY,
                  endX,
                  endY
                };

                let pId = vMap.get(`${startX},${startY}`);
                if (!pId) {
                  hasError = true;
                  continue;
                }
                moveEn = this.getMoveNotation(pId, startX, startY, endX, endY, null, vMap);
              } else {
                let isEnglish = /^[a-zA-Z]/.test(moveStr) || /^[+\-=1-9][a-zA-Z0-9]/.test(moveStr);
                moveEn = isEnglish ? moveStr : this.NotationConverter.toEnglish(moveStr, isRed);
                console.log('moveEn', moveEn);
                if (/^[RNBAKCPrnbakcp+\-=1-9]{2}[+\-=][1-9]/.test(moveEn)) {
                  dc = this.deriveCoordsFromPgnMove(currentFen, moveEn, isRed);
                } else {
                  dc = moveEn;
                }
              }

              if (moveEn === dc) {
                if (!currentCommentNode.c) {
                  currentCommentNode.c = '';
                }
                // Append the token, separating with a space if a comment already exists
                currentCommentNode.c += (currentCommentNode.c ? ' ' : '') + token;
                continue; // Skip node creation and continue parsing
              }

              if (!dc) {
                const cleanInitial = this.INITIAL_FEN.replace(/ - - 0 1$/, '');
                if (importedFen !== cleanInitial && importedFen !== this.INITIAL_FEN) {
                  this.importExportedText(text, this.INITIAL_FEN);
                  return;
                } else {
                  alert('Parsed partially due to an invalid move notation.');
                  this.renderRecordUI();
                  this.renderNoteUI();
                  this.updateToolHighlights();
                  return;
                }
              }

              // Rule 4: The variation is attached to the parent of the move
              lastMoveParent = currNode;
              lastMoveParentFen = currentFen;

              let nextFen = this.simulateMove(currentFen, dc);
              let childIdx = currNode.v.findIndex(c => c.move === moveEn && c.fen === nextFen);

              if (childIdx === -1) {
                let newNode = {
                  fen: nextFen,
                  move: this.coordsToEngine(dc.startX, dc.startY, dc.endX, dc.endY),
                  c: '',
                  v: []
                };
                currNode.v.push(newNode);
                childIdx = currNode.v.length - 1;
              }

              // If this.this creates a fork on the main path, update this.currentBranch
              if (currNode.v.length > 1 && currNode === attachNode) {
                this.currentBranch[forkCount] = childIdx;
                forkCount++;
                attachNode = currNode.v[childIdx]; // Follow the first parsed line as the main path
              }

              currNode = currNode.v[childIdx];
              currentFen = nextFen;
              currentCommentNode = currNode;

              let pId = vMap.get(`${dc.startX},${dc.startY}`);
              vMap.delete(`${dc.startX},${dc.startY}`);
              if (pId) vMap.set(`${dc.endX},${dc.endY}`, pId);
            }

這段代碼是否需要修改?尤其是getMoveNotation已經不存在了。

找到所有getMoveNotation,並替換成正確的代碼。

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

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

帖子 ejsoon »

gemini pro夠強:

代码: 全选

Yes, the code needs to be modified. Since `getMoveNotation` no longer exists, and the `move` property expects the engine format (e.g., `"h2e2"`), you should replace all instances of `this.getMoveNotation(...)` with `this.coordsToEngine(startX, startY, endX, endY)`.

Here are all the places in the code that need to be updated, with all comments in English:

### 1. In `parseUrlHash` method
**Modify this part:**
```javascript
      let pId = vMap.get(`${startX},${startY}`);
      if (!pId) return null;

      // Replace getMoveNotation with coordsToEngine
      let moveNotation = this.coordsToEngine(startX, startY, endX, endY);

      return {
        fen: nextFen,
        move: moveNotation,
        lastMove: dc,
        c: '',
        v: []
      };
```

### 2. In `importExportedText` method (PGN Format Parsing)
**Modify this part:**
```javascript
          if (/^[a-i][0-9][a-i][0-9]$/i.test(word) || /^[A-I][0-9][A-I][0-9]$/i.test(word)) {
            word = word.toLowerCase();
            let startX = word.charCodeAt(0) - 97;
            let startY = 9 - parseInt(word.charAt(1), 10);
            let endX = word.charCodeAt(2) - 97;
            let endY = 9 - parseInt(word.charAt(3), 10);
            dc = { startX, startY, endX, endY };
            let vMap = this.buildVirtualMap(currentNode.fen);
            let pId = vMap.get(`${startX},${startY}`);
            if (pId) {
              // Replace getMoveNotation with coordsToEngine
              moveEn = this.coordsToEngine(startX, startY, endX, endY);
            } else {
              moveEn = word;
            }
          } else {
```

### 3. In `importExportedText` method (Complex Format Parsing - The snippet you provided)
**Modify this part:**
```javascript
              if (/^[a-i][0-9][a-i][0-9]$/.test(moveStr)) {
                let startX = moveStr.charCodeAt(0) - 97;
                let startY = 9 - parseInt(moveStr.charAt(1), 10);
                let endX = moveStr.charCodeAt(2) - 97;
                let endY = 9 - parseInt(moveStr.charAt(3), 10);
                dc = {
                  startX,
                  startY,
                  endX,
                  endY
                };

                let pId = vMap.get(`${startX},${startY}`);
                if (!pId) {
                  hasError = true;
                  continue;
                }
                // Replace getMoveNotation with coordsToEngine
                moveEn = this.coordsToEngine(startX, startY, endX, endY);
              } else {
```

### 4. In `importExportedSVG` method
**Modify this part:**
```javascript
      const startX = startPt.x;
      const startY = startPt.y;
      const endX = endPt.x;
      const endY = endPt.y;

      const capturedId = this.tileMap.get(`${endX},${endY}`) || null;
      
      // Replace getMoveNotation with coordsToEngine
      const moveNotation = this.coordsToEngine(startX, startY, endX, endY);
      
      const dc = {
        startX,
        startY,
        endX,
        endY
      };
```
https://ejsoon.vip/
弈趣極光:享受思維樂趣
头像
ejsoon
一枝独秀
一枝独秀
帖子: 5398
注册时间: 2022年 11月 18日 17:36
为圈友点赞: 176 次
被圈友点赞: 204 次
联系:

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

帖子 ejsoon »

問題:當導入PGN時,中文又轉換失敗,且rotate=true沒有生效。

代码: 全选

[Game "Chinese Chess"]
[Rotate "true"]
[Format "Chinese"]

{吳可欣贏網友一盤棋}
1. 炮八平五 馬2進3
2. 兵七進一 車1平2
3. 馬八進七 卒7進1
4. 車九平八 馬8進7
5. 馬二進一 車9進1
6. 炮二平四{估計是這步棋使局勢急轉直下,因為它導致紅方右車完全出不來}
馬7進8
7. 炮四進三 卒7進1
8. 兵三進一 馬8進6
9. 馬七進六 車9平4
{吳姐說希望他踩中兵}
10. 馬六進五 馬3進5
11. 炮五進四 車4進3
12. 兵三進一 車4平5
{吳姐說差點走馬6進8}
13. 車一平二 砲8進4
14. 炮五平四 車5進2
15. 仕四進五 馬6進4
16. 車八進六 馬4進3
17. 帥五平四 砲8平6
18. 前炮平六 砲6退1
19. 炮六退五 車5平6
20. 帥四平五 車6平4
21. 帥五平四 車4進2
22. 車二進四 車4退2
23. 車二平四 砲2平6
24. 車八進三 砲6進3
{紅方認負}
*
https://ejsoon.vip/
弈趣極光:享受思維樂趣
头像
ejsoon
一枝独秀
一枝独秀
帖子: 5398
注册时间: 2022年 11月 18日 17:36
为圈友点赞: 176 次
被圈友点赞: 204 次
联系:

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

帖子 ejsoon »

上面問題已解決,現在的小問題是,PGN導出會產生兩個[Rotate "true"]。

TODO:URI導出如果放進rotate。

還有mini的剛走完的棋子的綠邊和白點又沒有了…
附件
ejceesobj202604302037.js
(299.29 KiB) 已下载 2 次
https://ejsoon.vip/
弈趣極光:享受思維樂趣
头像
ejsoon
一枝独秀
一枝独秀
帖子: 5398
注册时间: 2022年 11月 18日 17:36
为圈友点赞: 176 次
被圈友点赞: 204 次
联系:

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

帖子 ejsoon »

解決綠邊和白點小問題
engineToCoords(engineMove) {
coordsToEngine(startX, startY, endX, endY) {
機器碼和坐標相互轉。
附件
ejceesobj202604302047.js
(299.33 KiB) 已下载 1 次
https://ejsoon.vip/
弈趣極光:享受思維樂趣
回复

在线用户

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