diff --git a/app.js b/app.js index 678e7bc..fc4a840 100644 --- a/app.js +++ b/app.js @@ -71,7 +71,8 @@ activeSkin: 'skin-default', activeTrail: '', activeBackground: 'bg-default', activePet: '', activePetIcon: '', studentName: '', - focusQueue: [] + focusQueue: [], + lastFactId: '' }; let game = { @@ -185,22 +186,35 @@ const relevantTrouble = state.troubleList.filter(t => allowedNumbers.includes(t.n1) || allowedNumbers.includes(t.n2)); let n1, n2; + const makeId = (a, b) => { + const [s, l] = a < b ? [a, b] : [b, a]; + return `${s}x${l}`; + }; + const lastId = state.lastFactId; + if (state.focusQueue.length > 0) { const factId = state.focusQueue.shift(); const parts = factId.split('x').map(Number); [n1, n2] = parts; } else { - if (relevantTrouble.length > 0 && Math.random() < 0.5) { - const struggle = relevantTrouble[Math.floor(Math.random() * relevantTrouble.length)]; - n1 = struggle.n1; n2 = struggle.n2; - } else { - n1 = allowedNumbers[Math.floor(Math.random() * allowedNumbers.length)]; - n2 = Math.floor(Math.random() * 12) + 1; - if (Math.random() > 0.5) [n1, n2] = [n2, n1]; + let attempts = 0; + while (attempts < 6) { + if (relevantTrouble.length > 0 && Math.random() < 0.5) { + const struggle = relevantTrouble[Math.floor(Math.random() * relevantTrouble.length)]; + n1 = struggle.n1; n2 = struggle.n2; + } else { + n1 = allowedNumbers[Math.floor(Math.random() * allowedNumbers.length)]; + n2 = Math.floor(Math.random() * 12) + 1; + if (Math.random() > 0.5) [n1, n2] = [n2, n1]; + } + const cid = makeId(n1, n2); + if (cid !== lastId || attempts === 5) break; + attempts++; } } game.currentFact = { n1, n2, ans: n1 * n2 }; + state.lastFactId = makeId(n1, n2); document.getElementById('equation-display').innerText = `${n1} × ${n2}`; startDementor(); }