So every time I try to click the “im feeling lucky button, it works for a second, then it decides to go back to the original number but adds a click from the autoclicker part of the code. Here is the code (also ignore the cooldown that was just for testing to make sure it works):
gambling.js:
function initClickerGame() {
// Obtain references to counter, button, and message elements
const counterEl = document.getElementById('counter');
const luckyBtn = document.getElementById('luckyBtn');
const messageEl = document.getElementById('message');
let isCooldown = false;
luckyBtn.addEventListener('click', () => {
if (isCooldown) {
messageEl.textContent = 'Cooldown in effect. Please wait 30 seconds.';
messageEl.style.display = 'block';
return;
}
const gamble = Math.random();
let points = parseInt(counterEl.textContent) || 0; // Retrieve current points and convert to integer
if (points < 100) {
messageEl.textContent = 'You need at least 100 points to gamble.';
} else {
if (gamble < 0.4) {
const pointsWon = Math.floor(points / 2);
points += pointsWon;
counterEl.textContent = points;
messageEl.textContent = `You won ${pointsWon} points!`;
} else if (gamble >= 0.4 && gamble < 0.7) {
const pointsLost = Math.floor(points / 3);
if (pointsLost >= points) {
points = 0;
counterEl.textContent = points;
messageEl.textContent = 'You lost all your points!';
} else {
points -= pointsLost;
counterEl.textContent = points;
messageEl.textContent = `You lost ${pointsLost} points!`;
}
} else if (gamble >= 0.7 && gamble < 0.9) {
points *= 2;
counterEl.textContent = points;
messageEl.textContent = `Congratulations! Your points have been doubled!`;
} else {
points = 0;
counterEl.textContent = points;
messageEl.textContent = 'Bad luck! You lost all points!';
}
}
messageEl.style.display = 'block';
isCooldown = true;
setTimeout(() => {
isCooldown = false;
messageEl.style.display = 'none';
}, 3); // 30 seconds cooldown
});
}
window.onload = initClickerGame;
index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Clicker Game</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<link rel="icon" type="image/x-icon" href="favicon.ico">
<div class="container">
<h1 id="counter" class="counter">0</h1>
<br>
<button class="clicker" id="clickerBtn">Click Me!</button><br><br>
<button class="clicker" id="upgradeBtn">Buy Upgrader (Cost: 10)</button><br><br>
<button class="clicker" id="luckyBtn">I'm Feeling Lucky</button><br><br>
<button class="clicker" id="saveBtn" onclick="saveProgress()">Save Game</button><br><br>
<button class="clicker" id="loadBtn" onclick="loadProgress()">Load Game</button><br><br>
<button class="clicker" id="resetBtn" onclick="confirmReset()">Reset Game</button><br>
</div>
<div class="message" id="message"></div>
<script src="anticheat.js"></script>
<script src="gambling.js"></script>
<script src="save.js"></script>
<script>
const counterEl = document.querySelector('.counter');
let count = 0;
let clickValue = 1;
let upgradeCost = 10;
let upgradeMultiplier = 2;
let upgradeLevel = 1;
let isCooldown = false;
const clickerBtn = document.getElementById('clickerBtn');
const upgradeBtn = document.getElementById('upgradeBtn');
const luckyBtn = document.getElementById('luckyBtn');
const messageEl = document.getElementById('message');
clickerBtn.addEventListener('click', () => {
count += clickValue;
counterEl.textContent = count;
});
upgradeBtn.addEventListener('click', () => {
if (count >= upgradeCost) {
count -= upgradeCost;
counterEl.textContent = count;
clickValue *= upgradeMultiplier;
upgradeLevel++;
upgradeCost = upgradeCost * upgradeMultiplier;
upgradeBtn.textContent = `Buy Upgrader (Cost: ${upgradeCost})`;
if (upgradeLevel === 500) {
upgradeBtn.disabled = true;
upgradeBtn.textContent = `Upgrader Maxed!`;
}
} else {
showMessage("Insufficient funds!");
}
});
function updateCounter() {
count += clickValue;
counterEl.textContent = count;
}
function showMessage(text) {
messageEl.textContent = text;
messageEl.style.display = 'block';
setTimeout(() => {
messageEl.style.display = 'none';
}, 5000);
}
function confirmReset() {
const answer = prompt("WARNING: THIS WILL DELETE YOUR SAVE FILE AND START OVER!! To confirm the reset, what is the result of 100 + 5?");
if (answer === "105") {
resetGame();
} else {
showMessage("Incorrect answer. Reset not confirmed.");
}
}
function resetGame() {
count = 0;
upgradeCost = 10;
upgradeLevel = 1;
clickValue = 1;
counterEl.textContent = count;
upgradeBtn.textContent = `Buy Upgrader (Cost: ${upgradeCost})`;
showMessage("Game reset successfully!");
}
setInterval(updateCounter, 1000);
</script>
</body>
</html>
I have tried rewriting the code with a new structure but it broke it more, so I just reverted the code.