Skip to content

Commit

Permalink
fix: display chess moves
Browse files Browse the repository at this point in the history
  • Loading branch information
iFwu committed Nov 29, 2024
1 parent ccce8f9 commit 009f71b
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 19 deletions.
20 changes: 18 additions & 2 deletions src/components/SolutionDisplay.vue
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,24 @@ const currentMoveNumber = computed(() => moveHistory.value.length + 1);
const moveItems = computed(() =>
moveHistory.value.map((move, index) => {
const fenBeforeMove = fenHistory.value[index];
return moveToChineseNotation(fenBeforeMove, move);
// 确保历史记录索引有效
if (index >= fenHistory.value.length) {
console.error('历史记录不同步:', {
moveIndex: index,
fenHistoryLength: fenHistory.value.length,
moveHistoryLength: moveHistory.value.length,
fenHistory: fenHistory.value,
moveHistory: moveHistory.value,
});
return '未知走法';
}
try {
return moveToChineseNotation(fenHistory.value[index], move);
} catch (err) {
console.error('转换走法出错:', err);
return '未知走法';
}
})
);
Expand Down
47 changes: 30 additions & 17 deletions src/stores/chess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,20 @@ export const useChessStore = defineStore('chess', () => {
};

const setFenCode = (fen: string) => {
try {
console.log('[Store] 设置 FEN:', {
current: fenCode.value,
new: fen,
isProcessing: isProcessing.value,
bestMove: bestMove.value,
});
fenCode.value = fen;
fenHistory.value.push(fen);
} catch (err) {
setError(`设置 FEN 码时出错: ${(err as Error).message}`);
console.log('[Store] 设置 FEN:', {
current: fenCode.value,
new: fen,
isProcessing: isProcessing.value,
bestMove: bestMove.value,
});

// 如果是初始化或重置,清空历史并设置初始状态
if (fenCode.value === INITIAL_FEN) {
fenHistory.value = [fen];
moveHistory.value = [];
}

fenCode.value = fen;
};

const setBestMove = (move: string) => {
Expand All @@ -63,8 +65,14 @@ export const useChessStore = defineStore('chess', () => {
}
try {
const newFen = updateFEN(fenCode.value, bestMove.value);
setFenCode(newFen);
moveHistory.value.push(bestMove.value);
const currentMove = bestMove.value;

// 先更新历史记录
fenHistory.value = [...fenHistory.value, newFen];
moveHistory.value = [...moveHistory.value, currentMove];

// 再更新当前状态
fenCode.value = newFen;
setBestMove('');
} catch (err) {
setError(`执行下一步时出错: ${(err as Error).message}`);
Expand All @@ -73,24 +81,29 @@ export const useChessStore = defineStore('chess', () => {

const handlePreviousMove = () => {
if (fenHistory.value.length > 1) {
fenHistory.value.pop();
moveHistory.value.pop();
// 移除最后一步
fenHistory.value = fenHistory.value.slice(0, -1);
moveHistory.value = moveHistory.value.slice(0, -1);

// 更新当前状态
fenCode.value = fenHistory.value[fenHistory.value.length - 1];
setBestMove('');
}
};

const resetHistory = () => {
console.log('[Store] 重置历史');
bestMove.value = ''; // 清空最佳着法
// 先重置所有状态
bestMove.value = '';
fenHistory.value = [INITIAL_FEN];
moveHistory.value = [];
fenCode.value = INITIAL_FEN;
overlayImageSrc.value = '';
chessboardRect.value = null;
originalImageSize.value = { width: 0, height: 0 };
error.value = null;
isCalculating.value = false;
isProcessing.value = true; // 设置为 true,表示正在处理
isProcessing.value = true;
};

const setDepth = (newDepth: number) => {
Expand Down

0 comments on commit 009f71b

Please sign in to comment.