From fc1eb2040c4190804c98ce361a410bbc13414ce6 Mon Sep 17 00:00:00 2001 From: M1ngdaXie <156019134+M1ngdaXie@users.noreply.github.com> Date: Fri, 27 Mar 2026 01:39:09 -0700 Subject: [PATCH] Refactor code structure for improved readability and maintainability --- .gitignore | 1 + app.js | 103 ++++- index.html | 16 +- style.css | 1195 ++++++++++++++++++++++++++++------------------------ 4 files changed, 742 insertions(+), 573 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..73300d0 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.superpowers \ No newline at end of file diff --git a/app.js b/app.js index 8e8ce20..fe62bbc 100644 --- a/app.js +++ b/app.js @@ -93,7 +93,7 @@ function sfxComplete() { const APP_VERSION = 2; var state = { sound: true, - bigFont: false, + theme: "ink", difficulty: 1, score: 0, combo: 0, @@ -123,7 +123,7 @@ function saveState() { "chengyu_s", JSON.stringify({ sound: state.sound, - bigFont: state.bigFont, + theme: state.theme, difficulty: state.difficulty, totalCount: state.totalCount, streak: state.streak, @@ -237,7 +237,7 @@ function loadState() { try { var s = JSON.parse(localStorage.getItem("chengyu_s") || "{}"); state.sound = s.sound !== undefined ? s.sound : true; - state.bigFont = s.bigFont || false; + state.theme = s.theme || "ink"; state.totalCount = s.totalCount || 0; state.streak = s.streak || 0; state.checkinDays = s.checkinDays || []; @@ -312,16 +312,15 @@ function pickIdiom() { return item; } var pool = IDIOMS.filter(function (i) { - return i.l <= state.difficulty; + return i.l <= state.difficulty && !state.usedIdiomIndices[i.w]; }); - var a = 0, - idx; - do { - idx = Math.floor(Math.random() * pool.length); - a++; - } while (state.usedIdiomIndices[pool[idx].w] && a < 100); - state.usedIdiomIndices[pool[idx].w] = 1; - return pool[idx]; + if (!pool.length) { + state.usedIdiomIndices = {}; + pool = IDIOMS.filter(function (i) { return i.l <= state.difficulty; }); + } + var item = pool[Math.floor(Math.random() * pool.length)]; + state.usedIdiomIndices[item.w] = 1; + return item; } function generateBlanks(w, d) { var n = d === 1 ? 1 : d === 2 ? 2 : 3; @@ -375,6 +374,11 @@ function renderStats() { document.getElementById("statCombo").textContent = state.combo; document.getElementById("statTotal").textContent = state.totalCount; document.getElementById("statStreak").textContent = state.streak; + var flame = document.getElementById("flameIcon"); + flame.className = ""; + if (state.combo >= 8) flame.className = "flame-high"; + else if (state.combo >= 5) flame.className = "flame-mid"; + else if (state.combo >= 3) flame.className = "flame-low"; } function renderProgress() { var bar = document.getElementById("progressBar"); @@ -455,6 +459,12 @@ function renderCombo() { "学富五车!", "梅子无人能挡!", "登峰造极!", + "出口成章!", + "满腹经纶!", + "博古通今!", + "梅子开挂了!", + "无可匹敌!", + "梅子是成语王!", ]; var i = Math.min(Math.floor((state.combo - 3) / 2), msgs.length - 1); var txt = "🔥 连击 ×" + state.combo + " " + msgs[i]; @@ -477,6 +487,7 @@ function renderCombo() { function startRound() { state.round = 0; state.roundResults = []; + state.usedIdiomIndices = {}; nextQuestion(); } function nextQuestion() { @@ -620,7 +631,19 @@ function showResult(ok, nc) { '">