webs/Kuli/script.js

273 lines
9.4 KiB
JavaScript
Raw Permalink Normal View History

2025-08-08 22:45:17 +09:00
let wordClass = "noun";
let target = words();
2025-08-08 22:45:17 +09:00
let targetLen = target.length;
//単語リストの長さ
2025-08-08 22:45:17 +09:00
console.log(targetLen);
let body = document.getElementsByTagName("body")[0];
2025-08-15 20:57:59 +09:00
let BGMButton = document.getElementById("BGMButton");
2025-08-08 22:45:17 +09:00
let display = document.getElementById("display");
let word = document.getElementById("word");
let pronunc = document.getElementById("pronunc");
let ans = [document.getElementById("ans_0"), document.getElementById("ans_1"), document.getElementById("ans_2"), document.getElementById("ans_3")]
let result = document.getElementById("result");
let scoreElemA = document.getElementById("scoreA");
let scoreElemB = document.getElementById("scoreB");
let corrCntElemA = document.getElementById("corrCntA");
let wrongCntElemA = document.getElementById("wrongCntA");
let corrCntElemB = document.getElementById("corrCntB");
let wrongCntElemB = document.getElementById("wrongCntB");
let perfect = document.getElementById("perfect");
let imperfect = document.getElementById("imperfect");
let replay = document.getElementById("replay");
2025-08-14 20:26:33 +09:00
//let startSound = document.getElementById("startSound");
2025-08-08 22:45:17 +09:00
let corrSound = document.getElementById("corrSound");
let corrSound2 = document.getElementById("corrSound2");
let wrongSound = document.getElementById("wrongSound");
2025-08-15 20:57:59 +09:00
let BGM = document.getElementById("BGM");
corrSound.volume = 1;
wrongSound.volume = 0.5;
2025-08-15 20:57:59 +09:00
BGM.loop = true;
BGM.volume = 1;
2025-08-08 22:45:17 +09:00
let curWordIdx;
let curCorrIdx;
//quiz()の返り値で指定される問題語番号と四択正答番号
2025-08-08 22:45:17 +09:00
let isClicked = [false, false, false, false];
let wordIdx;
let tmpWordIdx;
//関数内の問題語番号の運用
2025-08-08 22:45:17 +09:00
let corrDict;
//問題語の個人情報リスト
2025-08-08 22:45:17 +09:00
let corrIdx;
let wrongIdx = [];
2025-08-15 13:18:42 +09:00
let quizedWordIdx = [];
2025-08-08 22:45:17 +09:00
let tmpWrongIdx;
2025-08-15 13:18:42 +09:00
let wrongChoiceList = [];
2025-08-08 22:45:17 +09:00
let cnt = 0;
let corrCnt = 0;
let wrongCnt = 0;
//正答数・誤答数の計上のちElemへ
2025-08-08 22:45:17 +09:00
let wrongs = [];
let wrongsLen;
let mistakenWordIdx;
let mistakenWordPronunc;
let mistakenWordPronuncLen;
let mistakenWordPronuncTxt;
2025-08-08 22:45:17 +09:00
let wrongsTxt;
2025-08-15 20:57:59 +09:00
let isOnTheBeat = false;
2025-08-08 22:45:17 +09:00
function randElem(arr) {
//与配列からランダムに要素を一つ取り出して返す関数
2025-08-08 22:45:17 +09:00
return arr[Math.floor(Math.random() * arr.length)];
}
function setQuiz() {
scoreElemA.style.display = "block";
display.style.display = "table";
result.style.display = "none";
replay.style.display = "none";
}
function removeQuiz() {
display.style.display = "none";
result.style.display = "block";
replay.style.display = "block";
}
2025-08-08 22:45:17 +09:00
function quiz() {
if (cnt == 20) {
//終了処理
2025-08-08 22:45:17 +09:00
removeQuiz();
cnt = 0;
wrongsLen = wrongs.length;
if (wrongsLen == 0) {
//perfect画面
2025-08-08 22:45:17 +09:00
perfect.style.display = "block";
imperfect.style.display = "none";
scoreElemA.style.display = "none";
scoreElemB.style.display = "none";
body.style.animation = "gaming 2s linear infinite";
result.style.animation = "gaming 2s linear infinite";
perfect.style.animation = "gaming 2s linear infinite";
return [null, null];
} else {
//imperfect画面
2025-08-08 22:45:17 +09:00
perfect.style.display = "none";
imperfect.style.display = "block";
scoreElemA.style.display = "none";
scoreElemB.style.display = "block";
corrCntElemB.innerText = corrCnt;
wrongCntElemB.innerText = wrongCnt;
wrongsTxt = "";
for (let i = 0; i < wrongsLen; ++i) {
//誤答の一覧の表示
2025-08-08 22:45:17 +09:00
mistakenWordIdx = wrongs[i][0];
//間違い方リストから正答番号を取り出した
2025-08-15 13:18:42 +09:00
mistakenWordPronuncTxt = "" + mistakenWordPronunc + "";
2025-08-15 20:57:59 +09:00
wrongsTxt += "" + target[mistakenWordIdx]["word"] + ""
+ "<span style=\"color: gold;\">是</span>"
+ target[mistakenWordIdx]["pronunc"] + "<span style=\"color: gold;\">不是‘" + wrongs[i][1] + "’。</span><br>"
2025-08-08 22:45:17 +09:00
}
imperfect.innerHTML = wrongsTxt.slice(0, -4);
return [null, null];
}
} else {
//継続処理
2025-08-08 22:45:17 +09:00
setQuiz();
tmpWordIdx = Math.floor(Math.random() * targetLen);
2025-08-15 13:18:42 +09:00
while (quizedWordIdx.includes(tmpWordIdx) || tmpWordIdx == wordIdx) {
//今ゲームの出題リストに含まれる出題を排除する
2025-08-08 22:45:17 +09:00
tmpWordIdx = Math.floor(Math.random() * targetLen);
}
wordIdx = tmpWordIdx;
2025-08-15 13:18:42 +09:00
quizedWordIdx.push(wordIdx);
2025-08-08 22:45:17 +09:00
corrIdx = Math.floor(Math.random() * 4);
console.log(corrIdx);
//問題語と正答番号をリフレッシュした
2025-08-15 13:18:42 +09:00
corrDict = target[wordIdx]
word.innerText = corrDict["word"];
pronunc.innerText = corrDict["meaning"];
ans[corrIdx].innerText = corrDict["pronunc"];
//誤答選択肢を作る
wrongChoiceList = [];
wrongChoiceList = mkWrongChoice(corrDict["pronunc"][0])
2025-08-08 22:45:17 +09:00
for (let i = 0; i < 4; ++i) {
if (i == corrIdx) continue;
2025-08-15 13:18:42 +09:00
tmpWrongIdx = Math.floor(Math.random() * wrongChoiceList.length);
while (wrongIdx.includes(tmpWrongIdx)) {
//今回の誤答選択肢に含まれる誤答を省く
tmpWrongIdx = Math.floor(Math.random() * wrongChoiceList.length);
2025-08-08 22:45:17 +09:00
}
wrongIdx.push(tmpWrongIdx);
//誤答集に今回使った誤答を追加した
2025-08-15 13:18:42 +09:00
ans[i].innerText = wrongChoiceList[tmpWrongIdx];
//ランダム列、ピンイン行の要素を誤答ゾーンに代入した
2025-08-08 22:45:17 +09:00
}
wrongIdx = [];
//誤答集がリセットされた
2025-08-08 22:45:17 +09:00
cnt++;
return [wordIdx, corrIdx]
}
}
function start() {
//初期値設定
2025-08-15 13:18:42 +09:00
quizedWordIdx = [];
2025-08-08 22:45:17 +09:00
[curWordIdx, curCorrIdx] = quiz();
2025-08-14 20:26:33 +09:00
// startSound.currentTime = 0;
// startSound.play();
2025-08-08 22:45:17 +09:00
corrCnt = 0;
wrongCnt = 0;
wrongs = [];
isClicked = [false, false, false, false];
corrCntElemA.innerText = 0;
wrongCntElemA.innerText = 0;
body.style.animation = "";
result.style.animation = "";
}
start();
2025-08-15 20:57:59 +09:00
2025-08-08 22:45:17 +09:00
ans[0].addEventListener("click", () => {
if (curCorrIdx == 0) {
//正解の場合
2025-08-08 22:45:17 +09:00
if (cnt == 20) {
corrSound2.play();
} else {
corrSound.currentTime = 0;
corrSound.play();
}
corrCnt++;
corrCntElemA.innerText = corrCnt;
[curWordIdx, curCorrIdx] = quiz();
isClicked = [false, false, false, false];
} else {
//間違いの場合
2025-08-08 22:45:17 +09:00
wrongSound.currentTime = 0;
wrongSound.play();
if (isClicked[0]) return;
//同じ誤答の間違いをカウントしないようにした
2025-08-08 22:45:17 +09:00
wrongCnt++;
wrongCntElemA.innerText = wrongCnt;
isClicked[0] = true;
wrongs.push([curWordIdx, ans[0].textContent]);
//間違い方リストに正答番号と選んだ誤答が追加された
2025-08-08 22:45:17 +09:00
}
})
ans[1].addEventListener("click", () => {
if (curCorrIdx == 1) {
if (cnt == 20) {
corrSound2.play();
} else {
corrSound.currentTime = 0;
corrSound.play();
}
corrCnt++;
corrCntElemA.innerText = corrCnt;
[curWordIdx, curCorrIdx] = quiz();
isClicked = [false, false, false, false];
} else {
wrongSound.currentTime = 0;
wrongSound.play();
if (isClicked[1]) return;
wrongCnt++;
wrongCntElemA.innerText = wrongCnt;
isClicked[1] = true;
wrongs.push([curWordIdx, ans[1].textContent]);
}
})
ans[2].addEventListener("click", () => {
if (curCorrIdx == 2) {
if (cnt == 20) {
corrSound2.play();
} else {
corrSound.currentTime = 0;
corrSound.play();
}
corrCnt++;
corrCntElemA.innerText = corrCnt;
[curWordIdx, curCorrIdx] = quiz();
isClicked = [false, false, false, false];
} else {
wrongSound.currentTime = 0;
wrongSound.play();
if (isClicked[2]) return;
wrongCnt++;
wrongCntElemA.innerText = wrongCnt;
isClicked[2] = true;
wrongs.push([curWordIdx, ans[2].textContent]);
}
})
ans[3].addEventListener("click", () => {
if (curCorrIdx == 3) {
if (cnt == 20) {
corrSound2.play();
} else {
corrSound.currentTime = 0;
corrSound.play();
}
corrCnt++;
corrCntElemA.innerText = corrCnt;
[curWordIdx, curCorrIdx] = quiz();
isClicked = [false, false, false, false];
} else {
wrongSound.currentTime = 0;
wrongSound.play();
if (isClicked[3]) return;
wrongCnt++;
wrongCntElemA.innerText = wrongCnt;
isClicked[3] = true;
wrongs.push([curWordIdx, ans[3].textContent]);
}
})
2025-08-15 20:57:59 +09:00
replay.addEventListener("click", start)
BGMButton.addEventListener("click", () => {
if(isOnTheBeat){
BGM.pause();
BGMButton.innerText = "播放音乐";
isOnTheBeat = false;
}else{
BGM.play();
BGMButton.innerText = "停止音乐";
isOnTheBeat = true;
}
})