last commit implemented js file

This commit is contained in:
El Haddoury Younes
2026-01-21 14:24:29 +01:00
parent b483e5f73f
commit b3b02a59ae
4 changed files with 31 additions and 74 deletions

BIN
MainPage/.DS_Store vendored Normal file

Binary file not shown.

View File

@@ -161,7 +161,6 @@
user-select:none; user-select:none;
} }
/* 7-seg digit container */
.digit{ .digit{
position:relative; position:relative;
width: 110px; width: 110px;
@@ -173,7 +172,6 @@
flex:0 0 auto; flex:0 0 auto;
} }
/* Match segments */
.seg{ .seg{
position:absolute; position:absolute;
width: var(--matchL); width: var(--matchL);
@@ -186,7 +184,6 @@
.seg:hover{ filter: brightness(1.1); } .seg:hover{ filter: brightness(1.1); }
.seg:active{ transform: translateY(1px); } .seg:active{ transform: translateY(1px); }
/* removed (invisible/white) */
.seg.removed{ .seg.removed{
background: rgba(255,255,255,0) !important; background: rgba(255,255,255,0) !important;
box-shadow:none; box-shadow:none;
@@ -194,20 +191,13 @@
pointer-events:none; pointer-events:none;
} }
/* segment positions */
/* a (top) */
.seg.a{ top: 16px; left: 21px; } .seg.a{ top: 16px; left: 21px; }
/* b (top-right) */
.seg.b{ top: 32px; left: 73px; width: var(--matchW); height: var(--matchL); } .seg.b{ top: 32px; left: 73px; width: var(--matchW); height: var(--matchL); }
/* c (bottom-right) */
.seg.c{ top: 92px; left: 73px; width: var(--matchW); height: var(--matchL); } .seg.c{ top: 92px; left: 73px; width: var(--matchW); height: var(--matchL); }
/* d (bottom) */
.seg.d{ top: 146px; left: 21px; } .seg.d{ top: 146px; left: 21px; }
/* e (bottom-left) */
.seg.e{ top: 92px; left: 23px; width: var(--matchW); height: var(--matchL); } .seg.e{ top: 92px; left: 23px; width: var(--matchW); height: var(--matchL); }
/* f (top-left) */
.seg.f{ top: 32px; left: 23px; width: var(--matchW); height: var(--matchL); } .seg.f{ top: 32px; left: 23px; width: var(--matchW); height: var(--matchL); }
/* g (middle) */
.seg.g{ top: 81px; left: 21px; } .seg.g{ top: 81px; left: 21px; }
.side{ .side{

View File

@@ -14,7 +14,7 @@
<div class="title"> <div class="title">
<h1>Allumettes Equation Infinite Levels</h1> <h1>Allumettes Equation Infinite Levels</h1>
<div class="sub">Klicke auf die Streichhölzer, um sie zu entfernen (sie werden weiß bzw. unsichtbar). <div class="sub">Klicke auf die Streichhölzer, um sie zu entfernen (sie werden weiß bzw. unsichtbar).
Mache die Gleichung wahr, indem du genau die vorgegebene Anzahl entfernst.</div> Mache die Gleichung wahr, indem du genau die vorgegebene Anzahl entfernst.</div>
</div> </div>
<div class="controls"> <div class="controls">
<button id="btnReset">Reset level</button> <button id="btnReset">Reset level</button>
@@ -39,7 +39,7 @@ Mache die Gleichung wahr, indem du genau die vorgegebene Anzahl entfernst.</div>
<div class="footerNote"> <div class="footerNote">
Goal: lösch <b id="goalN">N</b> sodass die Gleichung <b>WAHR</b> wird. Goal: lösch <b id="goalN">N</b> sodass die Gleichung <b>WAHR</b> wird.
Es ist nur das Entfernen erlaubt (kein Hinzufügen). Die Ziffern bestehen aus 7 Segmenten. Es ist nur das Entfernen erlaubt (kein Hinzufügen). Die Ziffern bestehen aus 7 Segmenten.
Der Generator garantiert, dass jedes Level lösbar ist (unendlich viele Level). Der Generator garantiert, dass jedes Level lösbar ist (unendlich viele Level).
</div> </div>
</div> </div>

View File

@@ -12,16 +12,16 @@ function maskFromSegments(list){
const DIGIT_MASK = [ const DIGIT_MASK = [
maskFromSegments(["a","b","c","d","e","f"]), // 0 maskFromSegments(["a","b","c","d","e","f"]),
maskFromSegments(["b","c"]), // 1 maskFromSegments(["b","c"]),
maskFromSegments(["a","b","d","e","g"]), // 2 maskFromSegments(["a","b","d","e","g"]),
maskFromSegments(["a","b","c","d","g"]), // 3 maskFromSegments(["a","b","c","d","g"]),
maskFromSegments(["b","c","f","g"]), // 4 maskFromSegments(["b","c","f","g"]),
maskFromSegments(["a","c","d","f","g"]), // 5 maskFromSegments(["a","c","d","f","g"]),
maskFromSegments(["a","c","d","e","f","g"]), // 6 maskFromSegments(["a","c","d","e","f","g"]),
maskFromSegments(["a","b","c"]), // 7 maskFromSegments(["a","b","c"]),
maskFromSegments(["a","b","c","d","e","f","g"]), // 8 maskFromSegments(["a","b","c","d","e","f","g"]),
maskFromSegments(["a","b","c","d","f","g"]) // 9 maskFromSegments(["a","b","c","d","f","g"])
]; ];
@@ -56,13 +56,12 @@ function pick(arr){ return arr[randInt(0, arr.length-1)]; }
let level = 1; let level = 1;
let targetRemove = 2; let targetRemove = 2;
let current = null; // {A,B,C} shown digits let current = null;
let solution = null; // {A,B,C} target digits after removals let solution = null;
let removedSoFar = 0; let removedSoFar = 0;
// Each segment element knows which digit/segment it belongs to
// We'll store: digitIndex (0..2 for A,B,C) and segName let removedSet = new Set();
let removedSet = new Set(); // keys like "0-a" meaning digit0 seg a removed
const elEq = document.getElementById("equation"); const elEq = document.getElementById("equation");
const elRemovedGrid = document.getElementById("removedGrid"); const elRemovedGrid = document.getElementById("removedGrid");
@@ -80,13 +79,12 @@ document.getElementById("btnNew").addEventListener("click", () => {
generateLevel(); generateLevel();
}); });
document.getElementById("btnReset").addEventListener("click", () => { document.getElementById("btnReset").addEventListener("click", () => {
// reset current level state but keep the same puzzle
resetPlayState(); resetPlayState();
renderEquation(); renderEquation();
updateTruthUI(); updateTruthUI();
}); });
// Turn mask into a set of active segment names
function segmentsFromMask(mask){ function segmentsFromMask(mask){
const set = new Set(); const set = new Set();
for(let i=0;i<7;i++){ for(let i=0;i<7;i++){
@@ -95,13 +93,11 @@ function segmentsFromMask(mask){
return set; return set;
} }
// Compute displayed digit masks after removals
function displayedMaskForDigit(digitValue, digitIndex){ function displayedMaskForDigit(digitValue, digitIndex){
let mask = DIGIT_MASK[digitValue]; let mask = DIGIT_MASK[digitValue];
for(const s of segNames){ for(const s of segNames){
const key = digitIndex + "-" + s; const key = digitIndex + "-" + s;
if(removedSet.has(key)){ if(removedSet.has(key)){
// remove the segment if it exists
mask &= ~(1 << SEG[s]); mask &= ~(1 << SEG[s]);
} }
} }
@@ -110,11 +106,11 @@ function displayedMaskForDigit(digitValue, digitIndex){
function displayedDigitValue(digitValue, digitIndex){ function displayedDigitValue(digitValue, digitIndex){
const mask = displayedMaskForDigit(digitValue, digitIndex); const mask = displayedMaskForDigit(digitValue, digitIndex);
return MASK_TO_DIGIT.has(mask) ? MASK_TO_DIGIT.get(mask) : null; // null = invalid digit shape return MASK_TO_DIGIT.has(mask) ? MASK_TO_DIGIT.get(mask) : null;
} }
function equationIsTrue(){ function equationIsTrue(){
// get displayed digits; if any invalid => false
const a = displayedDigitValue(current.A, 0); const a = displayedDigitValue(current.A, 0);
const b = displayedDigitValue(current.B, 1); const b = displayedDigitValue(current.B, 1);
const c = displayedDigitValue(current.C, 2); const c = displayedDigitValue(current.C, 2);
@@ -128,7 +124,7 @@ function updateTruthUI(){
elTruthText.textContent = ok ? "Equation is TRUE" : "Equation is FALSE"; elTruthText.textContent = ok ? "Equation is TRUE" : "Equation is FALSE";
} }
// Reset removals and removed pile
function resetPlayState(){ function resetPlayState(){
removedSet.clear(); removedSet.clear();
removedSoFar = 0; removedSoFar = 0;
@@ -141,7 +137,6 @@ function syncRemovedCounts(){
elPileCount.textContent = String(removedSoFar); elPileCount.textContent = String(removedSoFar);
} }
// Render a digit as a 7-seg container with clickable match segments
function renderDigit(digitValue, digitIndex, color){ function renderDigit(digitValue, digitIndex, color){
const digit = document.createElement("div"); const digit = document.createElement("div");
digit.className = "digit"; digit.className = "digit";
@@ -150,7 +145,7 @@ function renderDigit(digitValue, digitIndex, color){
const baseSegs = segmentsFromMask(baseMask); const baseSegs = segmentsFromMask(baseMask);
for(const s of segNames){ for(const s of segNames){
if(!baseSegs.has(s)) continue; // no stick here in the original digit if(!baseSegs.has(s)) continue;
const seg = document.createElement("div"); const seg = document.createElement("div");
seg.className = "seg " + s; seg.className = "seg " + s;
@@ -161,16 +156,15 @@ function renderDigit(digitValue, digitIndex, color){
seg.addEventListener("click", () => { seg.addEventListener("click", () => {
if(removedSet.has(key)) return; if(removedSet.has(key)) return;
// If player already removed target count, block further removes
if(removedSoFar >= targetRemove) return; if(removedSoFar >= targetRemove) return;
removedSet.add(key); removedSet.add(key);
removedSoFar++; removedSoFar++;
// visually remove from equation (turn white/invisible)
seg.classList.add("removed"); seg.classList.add("removed");
// add to removed pile as a small colored bar
const clone = document.createElement("div"); const clone = document.createElement("div");
clone.className = "removedSeg"; clone.className = "removedSeg";
clone.style.background = color; clone.style.background = color;
@@ -179,12 +173,11 @@ function renderDigit(digitValue, digitIndex, color){
syncRemovedCounts(); syncRemovedCounts();
updateTruthUI(); updateTruthUI();
// win condition
if(removedSoFar === targetRemove){ if(removedSoFar === targetRemove){
if(equationIsTrue()){ if(equationIsTrue()){
elHint.innerHTML = "<b>Solved!</b> You removed exactly the target and made the equation true. Click <b>New level</b>."; elHint.innerHTML = "<b>Solved!</b> You removed exactly the target and made the equation true. Click <b>New level</b>.";
} else { } else {
elHint.innerHTML = " <b>Not solved.</b> You used all removals but the equation isnt true. Click <b>Reset level</b> to try again."; elHint.innerHTML = " <b>Not solved.</b> You used all removals but the equation isnt true. Click <b>Reset level</b> to try again.";
} }
} }
}); });
@@ -197,7 +190,6 @@ function renderDigit(digitValue, digitIndex, color){
function renderEquation(){ function renderEquation(){
elEq.innerHTML = ""; elEq.innerHTML = "";
// color palette (A,B,C different)
const colors = [ const colors = [
"linear-gradient(180deg,#ff6b6b,#d64545)", "linear-gradient(180deg,#ff6b6b,#d64545)",
"linear-gradient(180deg,#6bcBff,#3a7bd5)", "linear-gradient(180deg,#6bcBff,#3a7bd5)",
@@ -233,78 +225,57 @@ function renderEquation(){
updateTruthUI(); updateTruthUI();
} }
// -------- Infinite level generation algorithm --------
//
// We generate a "solution equation" (A_s + B_s = C_s) valid in 0..9.
// Then we create the "shown equation" by turning each solution digit into a SUPERSET digit
// (i.e., add extra segments) so that the player can remove sticks to get back to the solution.
// We also ensure the shown equation is NOT already true.
// We finally choose a removal target N = total segments added across the 3 digits (or some bounded version).
//
// This guarantees solvable by removals only, for infinite levels.
// -----------------------------------------------------
function generateLevel(){ function generateLevel(){
resetPlayState(); resetPlayState();
// Level difficulty curve: increase target removals slowly, but never explode.
// You can tweak this. Keeps it playable while still "infinite".
targetRemove = Math.min(2 + Math.floor(Math.log2(level + 1)), 6); targetRemove = Math.min(2 + Math.floor(Math.log2(level + 1)), 6);
// Find a puzzle with exactly targetRemove removals needed to reach a true equation.
// We'll attempt random constructions until we hit the required N.
let tries = 0; let tries = 0;
while(true){ while(true){
tries++; tries++;
if(tries > 5000){ if(tries > 5000){
// fallback: relax target a bit if extremely unlucky
targetRemove = Math.max(2, targetRemove - 1); targetRemove = Math.max(2, targetRemove - 1);
tries = 0; tries = 0;
} }
// 1) Pick a valid solution equation (A_s + B_s = C_s)
const As = randInt(0, 9); const As = randInt(0, 9);
const Bs = randInt(0, 9); const Bs = randInt(0, 9);
const Cs = As + Bs; const Cs = As + Bs;
if(Cs < 0 || Cs > 9) continue; if(Cs < 0 || Cs > 9) continue;
// 2) For each digit, choose a "shown digit" that can be reduced to solution digit by removing sticks.
// i.e., shownMask is a superset of solutionMask.
const Achoices = superDigits(As); const Achoices = superDigits(As);
const Bchoices = superDigits(Bs); const Bchoices = superDigits(Bs);
const Cchoices = superDigits(Cs); const Cchoices = superDigits(Cs);
// pick a random superset (could be itself)
const Ashow = pick(Achoices); const Ashow = pick(Achoices);
const Bshow = pick(Bchoices); const Bshow = pick(Bchoices);
const Cshow = pick(Cchoices); const Cshow = pick(Cchoices);
// Count how many removals required to go from shown -> solution (sum removed bits)
const need = const need =
popcount(DIGIT_MASK[Ashow] ^ DIGIT_MASK[As]) + popcount(DIGIT_MASK[Ashow] ^ DIGIT_MASK[As]) +
popcount(DIGIT_MASK[Bshow] ^ DIGIT_MASK[Bs]) + popcount(DIGIT_MASK[Bshow] ^ DIGIT_MASK[Bs]) +
popcount(DIGIT_MASK[Cshow] ^ DIGIT_MASK[Cs]); popcount(DIGIT_MASK[Cshow] ^ DIGIT_MASK[Cs]);
// Require exactly targetRemove removals
if(need !== targetRemove) continue; if(need !== targetRemove) continue;
// 3) Ensure the shown equation is not already true.
// (It might accidentally be true with different digit meanings.)
const shownTrue = (Ashow + Bshow) === Cshow; const shownTrue = (Ashow + Bshow) === Cshow;
if(shownTrue) continue; if(shownTrue) continue;
// Accept puzzle
solution = { A: As, B: Bs, C: Cs }; solution = { A: As, B: Bs, C: Cs };
current = { A: Ashow, B: Bshow, C: Cshow }; current = { A: Ashow, B: Bshow, C: Cshow };
break; break;
} }
// UI labels
elLvl.textContent = String(level); elLvl.textContent = String(level);
elTarget.textContent = String(targetRemove); elTarget.textContent = String(targetRemove);
elGoalN.textContent = String(targetRemove); elGoalN.textContent = String(targetRemove);
// Friendly hint (dont reveal exact digits)
elHint.innerHTML = elHint.innerHTML =
`Level <b>${level}</b>: Make the equation true by removing <b>${targetRemove}</b> match(es). ` + `Level <b>${level}</b>: Make the equation true by removing <b>${targetRemove}</b> match(es). ` +
`You can only remove sticks (click them).`; `You can only remove sticks (click them).`;
@@ -314,19 +285,15 @@ function generateLevel(){
updateTruthUI(); updateTruthUI();
} }
// Returns digits whose mask is a SUPERSET of base digit's mask (i.e., can remove to base)
function superDigits(baseDigit){ function superDigits(baseDigit){
const base = DIGIT_MASK[baseDigit]; const base = DIGIT_MASK[baseDigit];
const res = []; const res = [];
for(let d=0; d<=9; d++){ for(let d=0; d<=9; d++){
const m = DIGIT_MASK[d]; const m = DIGIT_MASK[d];
// m must contain all base segments
if((m & base) === base){ if((m & base) === base){
res.push(d); res.push(d);
} }
} }
return res; return res;
} }
// Start
generateLevel(); generateLevel();