123

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

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

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <body>
    <div class="container bordered">
        <div class="green game bordered" type="button" id="0">
        </div><div class="red game bordered" type="button" id="1"></div>
        <div class="center-element bordered">
            <br>
            <div class="title">
                <h1>Simon<sup>&#x00AE;</sup></h1>
            </div>
            <div class="right">
            <div class="led inline">
                <input id="counter" value="0" class="invisible"/>
            </div>
            <div class="start" id="start"></div>
            <div class="start" id="strict"></div>
            </div>
            <div class="title">
                <h4 class="inline">Count</h4>
                <h4 class="inline margin">Start</h4>

                <h4 class="inline margin">Strict</h4>
                <div class="light inline" id="strict-light"></div>
            </div>
            <div class="title">
                <h4 class="inline">Off</h4>
            
            <label class="switch">
                <input type="checkbox" id="onBtn" value="0">
                <span class="slider"></span>
            </label>
            <h4 class="inline">On</h4>
</div>
        </div>
        <div class="blue game bordered" type="button" id="2">
        </div><div class="yellow game bordered" type="button" id="3"></div>
    </div>
</body>

            
          
!
            
              * {
    margin: 0;
    padding: 0;
}

body {
    background-color: gray;
    margin: 0 auto;
    padding-top: 75px;
    text-align: center;
    width: auto;
    z-index: 2;
}

.bordered {
    border: 10px solid black;
}

/*outer-most container*/
.container {
    border: 20px solid;
    border-radius: 50%;
    cursor: default;
    height: 520px;
    margin: 0 auto;
    overflow: hidden;
    position: relative;
    width: 520px;
}

/*green button*/
.green {
    background: #67e209;
    /* Old browsers */
    background: -moz-linear-gradient(top, #67e209 0%, #61c419 55%, #b4e391 100%);
    /* FF3.6-15 */
    background: -webkit-linear-gradient(top, #67e209 0%, #61c419 55%, #b4e391 100%);
    /* Chrome10-25,Safari5.1-6 */
    background: linear-gradient(to bottom, #67e209 0%, #61c419 55%, #b4e391 100%);
    /* W3C, IE10+, FF16+, Chrome26+, Opera12+, Safari7+ */
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#67e209', endColorstr='#b4e391', GradientType=0);
    /* IE6-9 */
    border-left: none;
    border-top: none;
}

.green:active,
.greenactive {
    background: green;
}

/*red button*/
.red {
    background: #f3c5bd;
    /* Old browsers */
    background: -moz-linear-gradient(top, #f3c5bd 0%, #e86c57 11%, #ea2803 30%, #ff6600 75%, #c72200 100%);
    /* FF3.6-15 */
    background: -webkit-linear-gradient(top, #f3c5bd 0%, #e86c57 11%, #ea2803 30%, #ff6600 75%, #c72200 100%);
    /* Chrome10-25,Safari5.1-6 */
    background: linear-gradient(to bottom, #f3c5bd 0%, #e86c57 11%, #ea2803 30%, #ff6600 75%, #c72200 100%);
    /* W3C, IE10+, FF16+, Chrome26+, Opera12+, Safari7+ */
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#f3c5bd', endColorstr='#c72200', GradientType=0);
    /* IE6-9 */
    border-right: none;
    border-top: none;
}

.red:active,
.redactive {
    background: red;
}

/*game buttons*/
.game {
    background-clip: content-box;
    cursor: pointer;
    display: inline-block;
    height: 250px;
    overflow: hidden;
    vertical-align: top;
    width: 250px;
}

.center-element {
    background-color: black;
    border-radius: 50%;
    color: white;
    cursor: default;
    height: 220px;
    left: 27%;

    position: absolute;
    top: 27%;
    vertical-align: top;
    width: 220px;
    z-index: 3;
}

.title {
    border: none;
    height: auto;
    margin: 0 auto;
    margin-top: 10px;
    position: relative;
    width: auto;
}

sup {
    font-size: 0.5em;
}

.led {
    background-color: black;
    border: 1px solid;
    box-shadow: inset 0 0 1px 3px rgba(255,255,255,0.5);
    height: 35px;
    margin: 0 auto;
    margin-bottom: 10px;
    margin-top: 10px;
    padding-bottom: 5px;
    width: 70px;
}

input {
    background-color: transparent;
    border-width: 0;
    color: red;
    font-family: Orbitron;
    font-size: 2em;
    margin: 0 0 .1em 0;
    text-align: center;
    width: 100%;
}

.inline {
    display: inline-block;
}

div h3 {
    margin-bottom: 10px;
    margin-top: 10px;
}

.margin {
    margin-left: 20px;
}

.switch {
    border: 3px solid black;
    display: inline-block;
    height: 17px;

    position: relative;
    width: 30px;
}

.switch input {
    display: none;
}

.slider {
    background-color: #ccc;

    bottom: 0;
    cursor: pointer;
    left: 0;
    position: absolute;
    right: 0;
    top: 0;
    transition: .4s;
    -webkit-transition: .4s;
}

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

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

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

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

.start {
    background-color: #fff721;
    border: 1px solid;
    border-radius: 50%;
    box-shadow: 0 0 1px 1px rgba(255, 255, 255, 0.5);
    cursor: pointer;
    display: inline-block;
    height: 15px;
    margin-left: 25px;
    width: 15px;
}

.start:active {
    box-shadow: 0 0 0 0;
}

.light {
    background-color: #3e4b60;
    border: 1px solid;
    border-radius: 50%;
    box-shadow: 0 0 1px 1px rgba(255, 255, 255, 0.5);
    height: 7px;
    width: 7px;
}
.right {
    margin-right: 10px;
}

.lightactive {
    background-color: #d8313a;
    border: 1px solid;
    border-radius: 50%;
    box-shadow: 0 0 1px 1px rgba(255, 255, 255, 0.5);
    height: 7px;
    width: 7px;
}

.invisible {
    visibility: hidden;
}

.blue {
    background: #1e5799;
    /* Old browsers */
    background: -moz-linear-gradient(top, #1e5799 0%, #2989d8 30%, #207cca 66%, #7db9e8 100%);
    /* FF3.6-15 */
    background: -webkit-linear-gradient(top, #1e5799 0%, #2989d8 30%, #207cca 66%, #7db9e8 100%);
    /* Chrome10-25,Safari5.1-6 */
    background: linear-gradient(to bottom, #1e5799 0%, #2989d8 30%, #207cca 66%, #7db9e8 100%);
    /* W3C, IE10+, FF16+, Chrome26+, Opera12+, Safari7+ */
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#1e5799', endColorstr='#7db9e8', GradientType=0);
    /* IE6-9 */
    border-bottom: none;
    border-left: none;
}

.blue:active,
.blueactive {
    background: blue;
}

.yellow {
    background: rgba(241, 231, 103, 1);
    background: -moz-linear-gradient(left, rgba(241, 231, 103, 1) 0%, rgba(224, 224, 54, 1) 30%, rgba(255, 255, 33, 1) 63%, rgba(194, 194, 58, 1) 100%);
    background: -webkit-gradient(left top, right top, color-stop(0%, rgba(241, 231, 103, 1)), color-stop(30%, rgba(224, 224, 54, 1)), color-stop(63%, rgba(255, 255, 33, 1)), color-stop(100%, rgba(194, 194, 58, 1)));
    background: -webkit-linear-gradient(left, rgba(241, 231, 103, 1) 0%, rgba(224, 224, 54, 1) 30%, rgba(255, 255, 33, 1) 63%, rgba(194, 194, 58, 1) 100%);
    background: -o-linear-gradient(left, rgba(241, 231, 103, 1) 0%, rgba(224, 224, 54, 1) 30%, rgba(255, 255, 33, 1) 63%, rgba(194, 194, 58, 1) 100%);
    background: -ms-linear-gradient(left, rgba(241, 231, 103, 1) 0%, rgba(224, 224, 54, 1) 30%, rgba(255, 255, 33, 1) 63%, rgba(194, 194, 58, 1) 100%);
    background: linear-gradient(to right, rgba(241, 231, 103, 1) 0%, rgba(224, 224, 54, 1) 30%, rgba(255, 255, 33, 1) 63%, rgba(194, 194, 58, 1) 100%);
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#f1e767', endColorstr='#c2c23a', GradientType=1);
    border-bottom: none;
    border-right: none;
}

.yellow:active,
.yellowactive {
    background: goldenrod;
}
            
          
!
            
              $(document).ready(function() {
    //initial function to turn game on
    $('#onBtn').click(function() {
        var on = $('#onBtn').val();
        if (on == 0) {
            $('#counter').val('0' + 0);
            isRunning = true;
            readyStatus();
            on += 1;
            $('#onBtn').val(on);
        } else {
            offStatus();
            on -= 1;
            $('.game').off('click');
            $('.start').off('click');
            $('#onBtn').val(on);
            isRunning = false;
        }
    });
});

//audio to load for buttons and end-of-game
const audio0 = new Audio('https://s3.amazonaws.com/freecodecamp/simonSound1.mp3');
const audio1 = new Audio('https://s3.amazonaws.com/freecodecamp/simonSound2.mp3');
const audio2 = new Audio('https://s3.amazonaws.com/freecodecamp/simonSound3.mp3');
const audio3 = new Audio('https://s3.amazonaws.com/freecodecamp/simonSound4.mp3');
const loseAudio = new Audio('https://actions.google.com/sounds/v1/weapons/big_explosion_cut_off.ogg');
const winAudio = new Audio('https://www.zapsplat.com/wp-content/uploads/2015/sound-effects-four/cartoon_success_fanfair.mp3');
const repeatDelay = createTimer();

//make game faster with each loop
var round = 0;

//allow for exiting of game structure when game is off
var isRunning = false;
var isStrict = false;

var combination;
var i = 0; //increment

function readyStatus() {

    if (isRunning) {
        $('#counter').removeClass('invisible');

        $('#strict').click(function() {
            $('#strict-light').toggleClass('light');
            $('#strict-light').toggleClass('lightactive');

            strictify();
        });
        $('#start').off("click");
        $('#start').click(function(event) {
            /* Act on the event */
            repeatDelay.reset();
            $('#counter').val('0' + 0);
            gameStart();
        });
    }
}

function strictify() {
    isStrict = isStrict == false ? true : false;
    return;
}

function gameStart(combo) {
    combination = combo || [];
    var next = callNext();
    i = 0;
    var click = false; //var to check game time out    

    if (round == 20) {
        winAudio.play();
        return;
    } else {
        combination.push(next);

        counterUp(combination);

        //interval to space out pattern activation
        patternInditcator(combination);

        //end-of-game timeout due to inactivity
        if (!click) {
            repeatDelay.reset();
            repeatDelay.start();
        }
        //user input

        $('.game').off('click');
        $('.game').click(function() {

            repeatDelay.reset();
            repeatDelay.start();

            var userClick = $(this).attr('id');
            click = true;

            clickSound(userClick);

            if (userClick == combination[i]) {
                i++;
                if (i == combination.length) {
                    $('.game').off();
                    repeatDelay.reset();

                    //recursivly call new round
                    round++;
                    gameStart(combination);
                }
            } else {
                if (isStrict) {
                    repeatDelay.reset();
                    $('#counter').val('! !');
                    loseAudio.play();
                    $('.game').off();
                    return;
                } else {
                    //delay to account for already playing sounds
                    setTimeout(function() {
                        $('#counter').val('! !');
                        loseAudio.play();
                        //revert back to ready status
                        i = 0;
                        patternInditcator(combination);
                    }, 500);
                }
            }
        });
    }
}


function callNext() {
    var nextNumber = Math.floor(Math.random() * 4);
    return nextNumber;
}

function patternInditcator(combination) {
    var j = 0; //increment


    var delay = setInterval(function() {
        repeatDelay.reset();
        if (j < combination.length) {
            activate(combination[j]);
        } else {
            clearInterval(delay);
        }
        j++;
        repeatDelay.start();
    }, 750);

}

//button indication of simon's pattern
function activate(button) {

    switch (button) {
        case 0:
            $('#0').addClass('greenactive');
            audio0.play();
            setTimeout(function() {
                $('#0').removeClass('greenactive');
            }, 500);

            break;
        case 1:
            $('#1').addClass('redactive');
            audio1.play();
            setTimeout(function() {
                $('#1').removeClass('redactive');
            }, 500);

            break;
        case 2:
            $('#2').addClass('blueactive');
            audio2.play();
            setTimeout(function() {
                $('#2').removeClass('blueactive');
            }, 500);
            break;
        case 3:
            $('#3').addClass('yellowactive');
            audio3.play();
            setTimeout(function() {
                $('#3').removeClass('yellowactive');
            }, 500);
            break;
    }
}

function clickSound(button) {
    if (button == 0) {
        audio0.play();
    } else if (button == 1) {
        audio1.play();
    } else if (button == 2) {
        audio2.play();
    } else {
        audio3.play();
    }
}

function counterUp(combo) {
    var score = combo.length;
    score = score < 10 ? '0' + score : score;
    $('#counter').val(score);
}

function offStatus() {
    repeatDelay.reset();
    combination = [];
    $('.game').off();
    $('#counter').addClass('invisible');
}

function createTimer() {
    var interval = null;
    var countdown = 5;

    var runOut = function() {
        if (countdown == 0) {
            $('#counter').val('! !');
            loseAudio.play();
            countdown = 5;
            i = 0;

            if (isStrict) {
                timer.reset();
                $('.game').off();
            } else {
                patternInditcator(combination);
            }

        } else {
            --countdown;
        }
    };

    var timer = {
        start: function() {
            if (!interval) {
                runOut();
                interval = setInterval(runOut, 1000);
            }
        },
        pause: function() {
            if (interval) {
                clearInterval(interval);
                interval = null;
            }
        },
        reset: function() {
            countdown = 5;
            this.pause();
        }
    };
    return timer;
}

            
          
!
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.

Console