gemini fast:
代码: 全选
要實現當玩家放棄行動(跳過回合)時將 `lastMovedPiece` 設為 `0` 的邏輯,我們需要針對初始化解析、放棄行動的處理函數以及狀態重置這幾個地方進行同步更正。
以下是針對 `chaosclockgame202604220741.js` 檔案中需要修改的相關代碼段落:
### 1. 修改初始化解析邏輯 (`parseComplexCkvalue`)
確保從 `ckvalue` 載入時,若無指定則預設為 `0`。
```javascript
parseComplexCkvalue(ckvalue) {
const segments = ckvalue.split(';');
// ... (盤面解析部分保持不變)
// 解析目前回合 (第二段)
if (segments.length > 1) {
this.turn = parseInt(segments[1].trim());
} else {
this.turn = 1; // 預設乙方
}
this.initialPlayer = this.turn;
// 3. 解析最後行動的棋子 (第三段)
// 更正:如果缺少或為空,預設為 0
if (segments.length > 2 && segments[2].trim() !== '') {
this.lastMovedPiece = parseInt(segments[2].trim());
} else {
this.lastMovedPiece = 0;
}
this.renderInitialState();
}
```
---
### 2. 修改放棄行動處理 (`handleCancelClick`)
當玩家點擊取消/放棄區域時,明確將 `lastMovedPiece` 設為 `0`。
```javascript
async handleCancelClick() {
if (this.state !== 'playing' || this.isAnimating) return;
this.consecutiveSkips++;
// 更正:當放棄行動時,將最後移動棋子設為 0
this.lastMovedPiece = 0;
this.recordMoveStr('0');
// ... (時針轉動與動畫邏輯保持不變)
if (this.consecutiveSkips >= 2) {
this.endGame(2);
} else {
this.switchTurn(false);
}
this.saveState();
}
```
---
### 3. 修改遊戲重置邏輯 (`restartGame`)
確保重新開始遊戲時,狀態回到 `0` 而非 `null`。
```javascript
restartGame() {
this.btnRestart.style.display = 'none';
this.statusOutput.textContent = '等待開始';
this.minuteRotation += 180;
this.hourRotation = -90;
this.updatePointersRealtime();
this.state = 'waiting';
this.moveSequence = [];
this.currentTurnMoves = [];
this.consecutiveSkips = 0;
// 更正:重置為 0
this.lastMovedPiece = 0;
this.isYiFinalTurn = false;
// ... (後續初始化邏輯保持不變)
}
```
---
### 4. 修改輔助解析邏輯 (`parseInitialData`)
此方法在代碼末尾重複定義了類似的功能,建議一併更正以保持一致性。
```javascript
parseInitialData(ckvalue) {
// ... (盤面解析部分保持不變)
// 3. 解析上一步剛走完的棋子
if (lastStr && lastStr.trim() !== '') {
const lastMoves = lastStr.split(',').map(Number);
this.lastMovedPiece = lastMoves[lastMoves.length - 1];
} else {
// 更正:預設改為 0
this.lastMovedPiece = 0;
}
}
```
### 重點更動說明:
* **統一性**:將所有 `this.lastMovedPiece` 出現 `null` 的地方改為 `0`。
* **規則銜接**:當值為 `0` 時,`handlePieceClick` 中的 `if (pieceValue === this.lastMovedPiece) return;` 判定將失效(因為沒有棋子編號為 0),這符合「放棄行動後,下一位玩家不受任何棋子移動限制」的規則。