Die Tracking -Funktion erfordert ein Versuchssystem in einem JavaScript -Spiel [geschlossen]HTML

HTML-Programmierer
Anonymous
 Die Tracking -Funktion erfordert ein Versuchssystem in einem JavaScript -Spiel [geschlossen]

Post by Anonymous »

Ich entwickle ein Spiel für Programmierer mit JavaScript, bei dem die Spieler über die Browserkonsole interagieren. Das Spiel enthält ein Versuchssystem: < /p>
Der Spieler beginnt mit 5 Versuchen (in Localstorage gespeichert). />
Versuchen Sie, die Logik zu verfolgen: < /li>
< /ol>
// Initialize attempts from localStorage or default to 5
var allowedAttempts = 5;
let attempts = parseInt(localStorage.getItem("attempts")) || allowedAttempts;
localStorage.setItem("attempts", attempts);

let attemptReduced = false; // Prevent multiple reductions in one execution

// Check for game over
function checkGameOver() {
if (attempts {
document.body.innerHTML =
"
Image
";
}, 100);
}
}

// Reduce attempts safely & show warning
function reduceAttempts(reason) {
if (!attemptReduced && attempts > 0) {
attempts = Math.max(0, attempts - 1);
attemptReduced = true;
localStorage.setItem("attempts", attempts);
console.warn(`⚠️ ${reason}. Attempts left: ${attempts}`);
checkGameOver();
setTimeout(() => { attemptReduced = false; }, 100); // Prevent recursion
}
}
< /code>

Überschreibungsfunktion.Function.prototype.call = (function (originalCall) {
return function (thisArg, ...args) {
reduceAttempts("Function call detected");
return originalCall.apply(this, [thisArg, ...args]);
};
})(Function.prototype.call);
Problem
Some function executions correctly reduce attempts, while others do not. Here are my test cases so far:
< /code>
✅ reduziert die Versuche korrekt: < /p>
eval("console.log('Eval executed')"); // ✅ Works
new Function("console.log('Function Constructor executed');")(); // ✅ Works
console.dir({ key: "value" }); // ✅ Works
console.table([{ name: "Alice" }, { name: "Bob" }]); // ✅ Works
let obj = { name: "Hacker" }; Object.keys(obj); // ✅ Works
let arr = [1, 2, 3]; arr.push(4); // ✅ Works
atob("SGVsbG8sIFdvcmxkIQ=="); // ✅ Works
btoa("Hello, World!"); // ✅ Works
< /code>
❌ reduziert keine Versuche (aber sollte): < /p>
function testFunction() { console.log("Function executed"); } testFunction(); // ❌ Does not work
Array.of(1, 2, 3); // ❌ Not working
Array.from([1, 2, 3]); // ❌ Not working
❌ Does not need to reduce attempts (harmless cases):

console.log("Testing console.log tracking"); // ❌ Harmless, should not reduce
Math.floor(10.5); // ❌ Harmless, should not reduce
< /code>
Was ich bei der Verfolgung aller Funktionsaufrufe von
durch konsequentes Verfolgen von Testfunktionen () oder Array.Of (). nicht. Irgendwelche Vorschläge zur Verbesserung dieses Tracking -Systems? Danke!

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post