<div class="mx-1 h-8 flex justify-center items-center relative">
<div class="mr-2 text-sm font-Libre">
{{ gameStatus.currentStep }} / {{ gameStatus.totalStep }} 題
</div>
<ul class="flex items-center"> <!-- 主要進度與答題正確性顯示區塊 -->
<li
v-for="(isCorrect, index) in gameStatus.stepCorrect"
:key="index"
class="w-6 h-2.5 mr-[2px] border-2 rounded-full"
:class="[
isCorrect === null ? 'bg-[#e3e0d5] border-[#e3e0d5]' : isCorrect ? 'bg-[#5cb887] border-[#5cb887]' : 'bg-[#d25353] border-[#d25353]',
gameStatus.currentStep === index + 1 ? 'border-[#5d72c9]' : '',
]"
/>
</ul>
<div
data-test="score"
class="ml-2 text-sm font-Libre"
>
{{ gameStatus.score }} 分
</div>
<RuleDeclaration class="z-50 ml-2" />
</div>
const initialGameState = {
currentStep: 1,
totalStep: 8,
stepCorrect: [null, null, null, null, null, null, null, null],
score: 0,
scoreRecord: [0, 0, 0, 0, 0, 0, 0, 0],
};
const gameStatus = reactive(JSON.parse(JSON.stringify(initialGameState)));
說明:stepCorrect 主要是用來顯示這題是否答對,而每一題都有不同的分數,不同分數則記錄在 scoreRecord 裡,總分則累積在 score 裡。
handleScore 主要功能
const handleScore = () => {
let isCorrect = false;
let insertPostion = overOutlineCount.value;
if (insertPostion === 0) {
isCorrect = timelineEvents.value[insertPostion].year < timelineEvents.value[insertPostion + 1].year;
} else if (insertPostion === timelineEvents.value.length - 1) {
isCorrect = timelineEvents.value[insertPostion].year > timelineEvents.value[insertPostion - 1].year;
} else {
isCorrect = timelineEvents.value[insertPostion].year >= timelineEvents.value[insertPostion - 1].year && timelineEvents.value[insertPostion].year <= timelineEvents.value[insertPostion + 1].year;
}
gameStatus.stepCorrect[gameStatus.currentStep - 1] = isCorrect;
if (isCorrect) {
gameStatus.score += timelineEvents.value[insertPostion].point;
}
//標記 timelineEvents 是否回答正確
timelineEvents.value[insertPostion].isCorrect = isCorrect;
return isCorrect;
};
變數和資料結構說明:
邏輯流程說明:
進度條上顯示是否正確。灰:未作答、綠:正確、紅:錯誤。
isCorrect === null ? 'bg-[#e3e0d5] border-[#e3e0d5]' : isCorrect ? 'bg-[#5cb887] border-[#5cb887]' : 'bg-[#d25353] border-[#d25353]',
const getYearAtPosition = (events, position) => events.value[position].year;
const handleScore = () => {
let isCorrect = false;
const position = overOutlineCount.value;
const currentYear = getYearAtPosition(timelineEvents, position);
const prevYear = getYearAtPosition(timelineEvents, position - 1);
const nextYear = getYearAtPosition(timelineEvents, position + 1);
if (position === 0) {
isCorrect = currentYear < nextYear;
} else if (position === timelineEvents.value.length - 1) {
isCorrect = currentYear > prevYear;
} else {
isCorrect = currentYear > prevYear && currentYear < nextYear;
}
gameStatus.stepCorrect[gameStatus.currentStep - 1] = isCorrect;
if (isCorrect) {
gameStatus.score += timelineEvents.value[position].point;
}
timelineEvents.value[position].isCorrect = isCorrect;
return isCorrect;
};