Newer
Older
KYKYKY / count.js
const startButton = document.getElementById('startButton');
const statusDisplay = document.getElementById('status');
const circles = document.querySelectorAll('.circle');
let targetNumber;
let timer;
let countdown;

// スタートボタンのクリックでゲーム開始
startButton.addEventListener('click', startGame);

function startGame() {
    // 状態をリセット
    resetGame();
    
    // ランダムに当たりの番号を設定
    targetNumber = Math.floor(Math.random() * 10) + 1;
    statusDisplay.textContent = '3秒前...';
    
    let timeLeft = 3;
    countdown = setInterval(() => {
        timeLeft--;
        statusDisplay.textContent = `${timeLeft}秒前...`;
        if (timeLeft <= 0) {
            clearInterval(countdown);
            endGame(false); // 時間切れで失敗
        }
    }, 1000);

    // タイムアウトを設定
    timer = setTimeout(() => {
        clearInterval(countdown);
        endGame(false); // 時間切れで失敗
    }, 3000);
}

// 各ボックスにクリックイベントを設定
circles.forEach(circle => {
    circle.addEventListener('click', () => {
        if (parseInt(circle.textContent) === targetNumber) {
            endGame(true); // 成功
        }
    });
});

// ゲーム終了処理
function endGame(isWin) {
    clearTimeout(timer);
    clearInterval(countdown);
    statusDisplay.textContent = isWin ? "爆弾を解除できた!" : "解除失敗だ。";
}

// ゲームリセット処理
function resetGame() {
    clearTimeout(timer);
    clearInterval(countdown);
    statusDisplay.textContent = "スタートボタンを押して爆弾解除に挑戦!";
}