<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Codebreaker Game</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f9;
            color: #333;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            height: 100vh;
            margin: 0;
        }
        h1 {
            margin-bottom: 20px;
        }
        .game-container {
            text-align: center;
            max-width: 400px;
            padding: 20px;
            background: #fff;
            border-radius: 10px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }
        input[type="text"] {
            width: 120px;
            padding: 5px;
            font-size: 18px;
            margin-bottom: 10px;
        }
        button {
            padding: 10px 20px;
            font-size: 16px;
            cursor: pointer;
            background: #007BFF;
            color: white;
            border: none;
            border-radius: 5px;
        }
        button:hover {
            background: #0056b3;
        }
        .feedback {
            margin-top: 20px;
        }
        .feedback ul {
            list-style: none;
            padding: 0;
        }
        .feedback ul li {
            background: #f8f9fa;
            padding: 10px;
            margin: 5px 0;
            border: 1px solid #ddd;
            border-radius: 5px;
        }
    </style>
</head>
<body>
    <div class="game-container">
        <h1>Codebreaker Game</h1>
        <p>Guess the 6-digit code!</p>
        <input type="text" id="guess" maxlength="6" placeholder="Enter 6 digits">
        <button onclick="makeGuess()">Submit Guess</button>
        <div class="feedback">
            <ul id="feedbackList"></ul>
        </div>
    </div>

    <script>
        const secretCode = generateCode();
        let attempts = 0;
        const maxAttempts = 4;

        const secretMessageBase64 = "Q29uZ3JhdHVsYXRpb25zISBZb3UgZm91bmQgdGhlIHNlY3JldCBtZXNzYWdlOiAiVGhlIGZsYWcgaXMgZmxhZ3tjMGRlX2JyM2FrZXJ9Ig";

        function generateCode() {
            let code = '';
            for (let i = 0; i < 6; i++) {
                code += Math.floor(Math.random() * 10);
            }
            return code;
        }

        function decodeBase64(encoded) {
            return atob(encoded);
        }

        function makeGuess() {
            const guess = document.getElementById('guess').value;
            if (guess.length !== 6 || isNaN(guess)) {
                alert('Please enter a valid 6-digit number.');
                return;
            }

            attempts++;
            const feedback = evaluateGuess(guess);

            const feedbackList = document.getElementById('feedbackList');
            const feedbackItem = document.createElement('li');
            feedbackItem.textContent = `Attempt ${attempts}: ${guess} - ${feedback}`;
            feedbackList.appendChild(feedbackItem);

            if (guess === secretCode) {
                const secretMessage = decodeBase64(secretMessageBase64);
                alert(`🎉 Congratulations! You cracked the code: ${secretCode}\n\nSecret Message: ${secretMessage}`);
                resetGame();
            } else if (attempts >= maxAttempts) {
                alert(`😞 Game over! The code was: ${secretCode}`);
                resetGame();
            }

            document.getElementById('guess').value = '';
        }

        function evaluateGuess(guess) {
            let correctPosition = 0;
            let correctDigit = 0;
            const usedIndices = [];

            for (let i = 0; i < 6; i++) {
                if (guess[i] === secretCode[i]) {
                    correctPosition++;
                    usedIndices.push(i);
                }
            }

            for (let i = 0; i < 6; i++) {
                if (guess[i] !== secretCode[i] && secretCode.includes(guess[i])) {
                    const index = secretCode.indexOf(guess[i]);
                    if (!usedIndices.includes(index)) {
                        correctDigit++;
                        usedIndices.push(index);
                    }
                }
            }

            return `${correctPosition} digit(s) correct and in the right position, ${correctDigit} digit(s) correct but in the wrong position.`;
        }

        function resetGame() {
            attempts = 0;
            const feedbackList = document.getElementById('feedbackList');
            feedbackList.innerHTML = '';
            alert('Starting a new game...');
            window.location.reload();
        }
    </script>
</body>
</html>
