Skip to content

Commit 009f71b

Browse files
committed
fix: display chess moves
1 parent ccce8f9 commit 009f71b

File tree

2 files changed

+48
-19
lines changed

2 files changed

+48
-19
lines changed

src/components/SolutionDisplay.vue

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,24 @@ const currentMoveNumber = computed(() => moveHistory.value.length + 1);
7070
7171
const moveItems = computed(() =>
7272
moveHistory.value.map((move, index) => {
73-
const fenBeforeMove = fenHistory.value[index];
74-
return moveToChineseNotation(fenBeforeMove, move);
73+
// 确保历史记录索引有效
74+
if (index >= fenHistory.value.length) {
75+
console.error('历史记录不同步:', {
76+
moveIndex: index,
77+
fenHistoryLength: fenHistory.value.length,
78+
moveHistoryLength: moveHistory.value.length,
79+
fenHistory: fenHistory.value,
80+
moveHistory: moveHistory.value,
81+
});
82+
return '未知走法';
83+
}
84+
85+
try {
86+
return moveToChineseNotation(fenHistory.value[index], move);
87+
} catch (err) {
88+
console.error('转换走法出错:', err);
89+
return '未知走法';
90+
}
7591
})
7692
);
7793

src/stores/chess.ts

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,20 @@ export const useChessStore = defineStore('chess', () => {
3030
};
3131

3232
const setFenCode = (fen: string) => {
33-
try {
34-
console.log('[Store] 设置 FEN:', {
35-
current: fenCode.value,
36-
new: fen,
37-
isProcessing: isProcessing.value,
38-
bestMove: bestMove.value,
39-
});
40-
fenCode.value = fen;
41-
fenHistory.value.push(fen);
42-
} catch (err) {
43-
setError(`设置 FEN 码时出错: ${(err as Error).message}`);
33+
console.log('[Store] 设置 FEN:', {
34+
current: fenCode.value,
35+
new: fen,
36+
isProcessing: isProcessing.value,
37+
bestMove: bestMove.value,
38+
});
39+
40+
// 如果是初始化或重置,清空历史并设置初始状态
41+
if (fenCode.value === INITIAL_FEN) {
42+
fenHistory.value = [fen];
43+
moveHistory.value = [];
4444
}
45+
46+
fenCode.value = fen;
4547
};
4648

4749
const setBestMove = (move: string) => {
@@ -63,8 +65,14 @@ export const useChessStore = defineStore('chess', () => {
6365
}
6466
try {
6567
const newFen = updateFEN(fenCode.value, bestMove.value);
66-
setFenCode(newFen);
67-
moveHistory.value.push(bestMove.value);
68+
const currentMove = bestMove.value;
69+
70+
// 先更新历史记录
71+
fenHistory.value = [...fenHistory.value, newFen];
72+
moveHistory.value = [...moveHistory.value, currentMove];
73+
74+
// 再更新当前状态
75+
fenCode.value = newFen;
6876
setBestMove('');
6977
} catch (err) {
7078
setError(`执行下一步时出错: ${(err as Error).message}`);
@@ -73,24 +81,29 @@ export const useChessStore = defineStore('chess', () => {
7381

7482
const handlePreviousMove = () => {
7583
if (fenHistory.value.length > 1) {
76-
fenHistory.value.pop();
77-
moveHistory.value.pop();
84+
// 移除最后一步
85+
fenHistory.value = fenHistory.value.slice(0, -1);
86+
moveHistory.value = moveHistory.value.slice(0, -1);
87+
88+
// 更新当前状态
7889
fenCode.value = fenHistory.value[fenHistory.value.length - 1];
7990
setBestMove('');
8091
}
8192
};
8293

8394
const resetHistory = () => {
8495
console.log('[Store] 重置历史');
85-
bestMove.value = ''; // 清空最佳着法
96+
// 先重置所有状态
97+
bestMove.value = '';
8698
fenHistory.value = [INITIAL_FEN];
99+
moveHistory.value = [];
87100
fenCode.value = INITIAL_FEN;
88101
overlayImageSrc.value = '';
89102
chessboardRect.value = null;
90103
originalImageSize.value = { width: 0, height: 0 };
91104
error.value = null;
92105
isCalculating.value = false;
93-
isProcessing.value = true; // 设置为 true,表示正在处理
106+
isProcessing.value = true;
94107
};
95108

96109
const setDepth = (newDepth: number) => {

0 commit comments

Comments
 (0)