cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

Looking for quick-add? Try the external resource search, it's quicker and gives you access to the most recent version of thousands of libraries. ☝️

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Looking for quick-add? Try the external resource search, it's quicker and gives you access to the most recent version of thousands of libraries. ☝️

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              
    <audio id="fail">
        <source src="http://free-sounds.net/sound-files/special-effects/SHATTERE.WAV" type="audio/mpeg">
    </audio>
    <audio id="sound1">
        <source src="https://s3.amazonaws.com/freecodecamp/simonSound1.mp3" type="audio/mpeg">
    </audio>
    <audio id="sound2">
        <source src="https://s3.amazonaws.com/freecodecamp/simonSound2.mp3" type="audio/mpeg">
    </audio>
    <audio id="sound3">
        <source src="https://s3.amazonaws.com/freecodecamp/simonSound3.mp3" type="audio/mpeg">
    </audio>
    <audio id="sound4">
        <source src="https://s3.amazonaws.com/freecodecamp/simonSound4.mp3" type="audio/mpeg">
    </audio>
    <div id="canvas">

        <div class="game">
            <div class="background-circle"></div>
            <div id="1" class="quarter quarter1 noselect" data-number="1"></div>
            <div id="2" class="quarter quarter2 noselect" data-number="2"></div>
            <div id="3" class="quarter quarter3 noselect" data-number="3"></div>
            <div id="4" class="quarter quarter4 noselect" data-number="4"></div>
            <div class="cutout">

                <div class="game-panel">
                    <h1 id="title">
                        SIMON
                    </h1>
                    <div id="control-elements">
                        <div class="info-block">
                            <div id="info-display" class="elements"></div>
                            <div id="count" class="label">COUNT</div>
                        </div>
                        <div class="start-block">
                            <div id="start-restart-btn" class="button elements start-off noselect"></div>
                            <div id="start" class="label">START</div>
                        </div>
                        <div class="count-block">
                            <div id="strict-btn" class="button elements start-off strict-mode-off noselect"></div>
                            <div id="strick" class="label">STRICT</div>
                        </div>

                    </div>
                    <div id="switch-area">
                        <div class="power-element noselect">
                            OFF
                        </div>
                        <div>
                            <label class="switch">
                                <input type="checkbox">
                                <span class="slider round"></span>
                            </label>
                        </div>
                        <div class="power-element noselect">
                            ON
                        </div>
                    </div>
                </div>
            </div>

        </div>
    </div>
            
          
!
            
              * {
    margin: 0;
    padding: 0;
}

body {
    margin: 0;
    font-family: Arial, "Helvetica Nueu", sans-serif;
    padding: 0;
    font-size: 20px;
}

#canvas {
    margin-top: 20px;
}

.game {
    position: relative;
    width: 350px;
    height: 350px;
    margin: 0 auto;
}

.background-circle {
    position: absolute;
    width: 50%;
    height: 50%;
    top: 0;
    left: 0;
    border-radius: 100% 0 0 0;
}

.quarter {
    position: absolute;
    width: 50%;
    height: 50%;
}

.quarter1 {
    top: 0;
    left: 0;
    background-color: darkgreen;
    border-radius: 100% 0 0 0;
}

.quarter1-light {
    background-color: rgb(20, 214, 20);
}

.quarter2 {
    top: 0;
    right: 0;
    background-color: darkred;
    border-radius: 0 100% 0 0;
}

.quarter2-light {
    background-color: red;
}

.quarter3 {
    bottom: 0;
    left: 0;
    background-color: darkgoldenrod;
    border-radius: 0 0 0 100%;
}

.quarter3-light {
    background-color: rgb(255, 196, 0);
}

.quarter4 {
    bottom: 0;
    right: 0;
    background-color: darkblue;
    border-radius: 0 0 100% 0;
}

.quarter4-light {
    background-color: blue;
}

.cutout {
    width: 50%;
    height: 50%;
    background-color: rgb(220, 230, 243);
    position: absolute;
    top: 25%;
    left: 25%;
    border-radius: 50%;
}

.game-panel {
    margin-top: 20px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
}

#title {
    font-size: 25px;
}

#control-elements {
    display: flex;
    flex-direction: row;
    justify-content: center;
    align-content: center;
    align-items: center;
    min-width: 150px;
}

.info-block {
    min-height: 50px;
}

.button {
    cursor: pointer;
    font-size: 20px;
    background-color: #ec1010;
    padding: 10px;
    border: #464242 1px solid;
    border-radius: 50%;
    text-align: center;
    box-shadow: 0 3px #5c3232;
}

.button:hover {
    background-color: #ec1010;
}

.button:active {
    background-color: #ec1010;
    border: #473f37 1px solid;
    box-shadow: 0 2px #5c3232;
    transform: translateY(2px);
}

.strict-mode-off {
    background-color: #fff347;
}

.strict-mode-off:hover {
    background-color: #fff347;
}

.start-on {
    background-color: green;
}

.start-off {
    background-color: grey;
}

.elements {
    font-size: 15px;
    margin: 10px;
    min-width: 5px;
}

.start-block,
.count-block,
.info-block {
    text-align: center;
}

#info-display {
    height: 20px;
    border: #473f37 1px solid;
    border-radius: 20%;
    font-size: 17px;
    min-width: 20%;
    text-align: center;
    background-color: rgb(22, 21, 21);
    color: red;
}

.label {
    font-size: 10px;
    margin: 5px;
}

#switch-area {
    font-size: 10px;
    margin-top: 10px;
    width: 120px;
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: center;
    vertical-align: middle;
}

.power-element {
    font-size: 10px;
    margin-left: 5px;
    margin-right: 5px;
}

.noselect {
    -webkit-touch-callout: none;
    /* iOS Safari */
    -webkit-user-select: none;
    /* Safari */
    -khtml-user-select: none;
    /* Konqueror HTML */
    -moz-user-select: none;
    /* Firefox */
    -ms-user-select: none;
    /* Internet Explorer/Edge */
    user-select: none;
    pointer-events: none;
}


/* Power Slider*/

.switch {
    position: relative;
    display: inline-block;
    width: 60px;
    height: 34px;
}

.switch input {
    display: none;
}

.slider {
    position: absolute;
    cursor: pointer;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: #ccc;
    -webkit-transition: .4s;
    transition: .4s;
}

.slider:before {
    position: absolute;
    content: "";
    height: 26px;
    width: 26px;
    left: 4px;
    bottom: 4px;
    background-color: white;
    -webkit-transition: .4s;
    transition: .4s;
}

input:checked+.slider {
    background-color: #2196F3;
}

input:focus+.slider {
    box-shadow: 0 0 1px #2196F3;
}

input:checked+.slider:before {
    -webkit-transform: translateX(26px);
    -ms-transform: translateX(26px);
    transform: translateX(26px);
}


/* Rounded sliders */

.slider.round {
    border-radius: 34px;
}

.slider.round:before {
    border-radius: 50%;
}
            
          
!
            
              /* Tested in Chrome
Some lags in Safari
*/

$(document).ready(function() {
    var simon = {
        deviceStatus: 'off',
        strictModeStatus: 'off',
        series: [],
        answers: [],
        stepCounter: 0,
        isMusicPlayed: true,
        isAnswerCorrect: true,
        sessionInterval: null,
        answerTimeout: null,
        waitingTimeout: null,
        isAnswerExpected: false,
        guessed: 0,
        randomNumber: 0,
        isStrictModeOn: false
    };

    $('#start-restart-btn').click(function() {
        startRestart();
    });

    function startRestart() {
        $('#info-display').text('---');
        simon.series = [];
        simon.answers = [];
        simon.stepCounter = 0;
        simon.isAnswerCorrect = true;
        $('.quarter').removeClass('noselect');
        simon.sessionInterval = setInterval(startGame, 2000);
    }

    /**
     * Main function. The whole logic resides here
     */
    function startGame() {
        $('#info-display').text(simon.stepCounter + 1);

        clearInterval(simon.sessionInterval);
        clearTimeout(simon.answerTimeout);
        clearAllIntervals();

        if (simon.isAnswerCorrect) {
            simon.randomNumber = getRandom();
            simon.series.push(simon.randomNumber);
        }

        playSounds();

        $('.quarter').removeClass('noselect');

        var clickCounter = 0;
        $('.quarter').off().click(function() {

            clickCounter++;

            /** A random number is generated */
            simon.guessed = parseInt($(this).data('number'));

            /** 
             * If the user press the correct tone, 
             * the game continues
             *  */
            if (simon.guessed === simon.series[clickCounter - 1]) {
                playSound('sound' + $(this).data('number'));
                highlightArea($(this).data('number'));
                if (clickCounter === simon.series.length) {
                    $('.quarter').addClass('noselect');
                    simon.answerTimeout = setTimeout(function() {
                        simon.isAnswerCorrect = true;
                        simon.stepCounter++;
                        simon.sessionInterval = setInterval(startGame, 500);
                    }, 2000);
                }
            /**
             * Otherwise either starts the game from the beginning if isStrictModeOn=true,
             * or offer a user to repeat the current sequence of music tones isStrictModeOn=false
             * 
             */
            } else {
                if (simon.isStrictModeOn) {
                    playSound('fail');
                    simon.sessionInterval = setInterval(startRestart, 1000);
                } else {
                    $('.quarter').addClass('noselect');
                    simon.answerTimeout = setTimeout(function() {
                        simon.isAnswerCorrect = false;
                        simon.sessionInterval = setInterval(startGame, 1000);
                    }, 1000);
                }

            }
        });
        /**
         * In case a user takes too much to repeat the sound sequence
         * the game takes their to a new game (if isStrictModeOn = true),
         * or offer to keep trying to repeat the current sequence (isStrictModeOn = false)
         */
        setTimeout(function() {
            if (clickCounter === 0 || clickCounter < simon.series.length) {
                playSound('fail');
                simon.waitingTimeout = setTimeout(function() {
                    clearTimeout(simon.answerTimeout);
                    clearAllIntervals();
                    if (!simon.isStrictModeOn) {
                        simon.isAnswerCorrect = false;
                        simon.sessionInterval = setInterval(startGame, 1000);
                    } else {
                        simon.sessionInterval = setInterval(startRestart, 1000);
                    }

                }, 1);
            }
        }, 5000 + (simon.stepCounter * 1000));
    }

    function playSounds() {
        for (var i = 0; i < simon.series.length; i++) {
            (function(index) {
                setTimeout(function() {
                    playSound('sound' + simon.series[index]);
                    highlightArea(simon.series[index]);
                }, i * 1000);
            })(i);
        }
    }

    function playSound(soundId) {
        var sound = document.getElementById(soundId);
        sound.play();
    }

    function highlightArea(id) {
        $('#' + id).addClass('quarter' + id + '-light');
        setTimeout(function() {
            $('#' + id).removeClass('quarter' + id + '-light');
        }, 500);
    }

    function clearAllIntervals() {
        for (var i = 1; i < 99999; i++) {
            window.clearInterval(i);
            window.clearTimeout(i);
        }
    }

    $('#strict-btn').click(function() {
        if (simon.isStrictModeOn) {
            simon.isStrictModeOn = false;
            $('#strict-btn').addClass('strict-mode-off');
        } else {
            simon.isStrictModeOn = true;
            $('#strict-btn').removeClass('strict-mode-off');
        }

    });

    function setupNewGame() {
        emptySequence();
        setCounterToZero();
    }

    function emptySequenceArray() {
        simon.series = [];
    }

    function setCounterToZero() {
        simon.stepCounter = 0;
    }

    function emptySequence() {
        simon.series = [];
    }

    function addNumberToSeries(number) {
        simon.series.push(getRandom());
    }

    function getRandom() {
        const min = 1;
        const max = 5;
        return Math.floor(Math.random() * (max - min)) + min;
    }

    /** Switch On the game */
    $('.switch input').click(function() {

        if ($('.switch input').is(':checked')) {
            $('#start-restart-btn').removeClass('noselect');
            $('#strict-btn').removeClass('noselect');
            $('#info-display').text('---');
            $('.button').removeClass('start-off ');
            simon.deviceStatus = 'on';
        } else {
            $('#start-restart-btn').addClass('noselect');
            $('#strict-btn').addClass('noselect');
            //$('.quarter').addClass('noselect');
            simon.deviceStatus = 'off';
            clearInterval(simon.sessionInterval);
            clearTimeout(simon.answerTimeout);
            clearAllIntervals();
            $('#info-display').text('');
            $('.button').addClass('start-off ');
        }

    });


});
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console