css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

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.

            
              <!--DOCTYPE html>
<html>
    
    <head>
        <title>FCC Simon Game</title>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" type="text/css" href="FccSimon.css">
    </head-->
    
    <body class="flex">
        <div class="wrapall">
            <div class="wrapcolors">
                <div class="flex">
                    <div class="btn green" id=1></div>
                    <div class="btn red" id=2></div>
                </div>
                <div class="flex">
                    <div class="btn yellow" id=3></div>
                    <div class="btn blue" id=4></div>
                </div>
            </div>

            <div class="controls flex">
                <div class="logo">Simon</div>
                <div class="inline">
                    <div class="block lcd"></div>
                    <div class="block">Count</div>
                </div>
                <div class="inline">
                    <div class="block button start-button"></div>
                    <div class="block">Start</div>
                </div>
                <div class="inline">
                    <div class="block button strict-button"></div>
                    <div class="block">Strict</div>
                </div>
                <div>
                    <div class="block power-label">OFF&nbsp;&nbsp;&nbsp;&nbsp;ON</div>
                    <div class="block switch-holder">
                        <div class="power-switch"></div>
                    </div>
                </div>
            </div>
        </div>
        <!--script src="jquery-3.1.0.js"></script>
        <script src="FccSimon.js"></script-->
    </body>
  
<!--/html-->
            
          
!
            
              body {
    font-family: Gill Sans, sans-serif;
    background-color: #999;
    margin: 0;
    cursor: default; }

.flex {
    display: -webkit-box;
    display: -moz-box;
    display: -ms-flexbox;
    display: -webkit-flex;
    display: flex;
    align-items: center; 
    justify-content: center; }

.block {
    display: block; }

.inline {
    margin: 4vmin;
    font-size: 3vmin;
    display: inline; }

.wrapcolors {
    box-shadow: 0 0 8vmin #999;
    width: 100vmin;
    height: 100vmin;
    border-radius: 50%; }

.btn {
    cursor: pointer;
    width: 50vmin;
    height: 50vmin; }

.green {
    background-color: lime;
    opacity: 0.4;
    box-shadow: inset 0 0 0 2vmin black;
    border-top-left-radius: 100%; }

.red {
    background-color: red;
    opacity: 0.4;
    box-shadow: inset 0 0 0 2vmin black;
    border-top-right-radius: 100%; }

.yellow {
    background-color: yellow;
    opacity: 0.4;
    box-shadow: inset 0 0 0 2vmin black;
    border-bottom-left-radius: 100%; }

.blue {
    background-color: blue;
    opacity: 0.4;
    box-shadow: inset 0 0 0 2vmin black;
    border-bottom-right-radius: 100%; }

.pushed {
    opacity: 1;
    box-shadow: inset 0 0 0 2vmin #595959; }

.controls {
    position: absolute;
    top: 25vmin;
    left: calc(50% - 25vmin);
    box-shadow: inset 0 0 0 3vmin #595959;
    background-color: #888;
    width: 50vmin;
    height: 50vmin;
    border-radius: 50%; 
    flex-wrap: wrap; 
    text-align: center; 
    align-content: center; }

.logo {
    font-family: Impact, fantasy;
    font-size: 8vmin; 
    flex: 0 0 100%; }

.button {
    box-shadow: .1vmin .1vmin .1vmin .2vmin black;
    cursor: pointer;
    margin: 0 auto;
    background-color: #777;
    width: 3vmin;
    height: 3vmin;
    border-radius: 50%; }

.lcd {
    font-family: Andale Mono, monospace;
    color: rgba(255,0,0,0.8);
    margin: 0 auto;
    border-radius: 5px;
    box-shadow: inset .1vmin .1vmin .1vmin .1vmin black;
    background-color: #777;
    width: 6vmin;
    height: 3vmin; }

.start-button:active, .strict-button:active {
    box-shadow: 0 0 .1vmin .1vmin black; }

.power-label {
    font-size: 3vmin; }

.switch-holder {
    box-shadow: inset .1vmin .1vmin .1vmin .1vmin black;
    margin: 0 auto;
    border-radius: 5px;
    background-color: #777;
    width: 98%;
    height: 2vmin; }

.power-switch {
    box-shadow: inset 0 0 .2vmin .1vmin white;
    cursor: pointer;
    width: 50%;
    border-radius: 5px;
    height: 100%;
    background-color: black; }

.button-on {
    background-color: rgba(255,0,0,0.5); }

.margin-toggle {
    margin-left: 50%; }
            
          
!
            
              $(document).ready(function () {
    'use strict';
    
    var interval, interval2, interval3, cpuindex = 0, playerindex = 0, redo = false, poweron = false, gamestarted = false, strict = false, computerdone = false, count = 0, sequence = [],
        sound = ["", new Audio("https://s3.amazonaws.com/freecodecamp/simonSound1.mp3"),
            new Audio("https://s3.amazonaws.com/freecodecamp/simonSound2.mp3"),
            new Audio("https://s3.amazonaws.com/freecodecamp/simonSound3.mp3"),
            new Audio("https://s3.amazonaws.com/freecodecamp/simonSound4.mp3")];

    function computerplay() {
        if (cpuindex < sequence.length) {
            setTimeout(function () {
                sound[sequence[cpuindex]].play();
                $("#" + sequence[cpuindex]).addClass("pushed");
            }, 300);

            setTimeout(function () {
                $("#" + sequence[cpuindex]).removeClass("pushed");
                cpuindex += 1;
            }, 600);
        } else {
            clearInterval(interval3);
            computerdone = true;
            playerindex = 0;
            redo = false;
        }
    } //end computerplay()
    
    function computerstart() {
        cpuindex = 0;
        if (!redo) { //do this if its not a redo -- set in $(".btn").click area based on conditions
            count += 1;
            $(".lcd").html(count);
            sequence.push(1 + Math.floor(Math.random() * 4));
        }
        interval3 = setInterval(computerplay, 900);
    } //end computerstart()

    function intro() { //some random patterns of lights when the power is turned on
        $("#1, #2, #3, #4").removeClass("pushed");
        var random = 1 + Math.floor(Math.random() * 3), random2 = 200 + Math.floor(Math.random() * 800);
        
        function intro1() {
            if ($("#1").hasClass("pushed")) {
                $("#1, #2, #3, #4").removeClass("pushed");
                $("#2, #3").addClass("pushed");
            } else {
                $("#1, #2, #3, #4").removeClass("pushed");
                $("#1, #4").addClass("pushed");
            }
        } //end intro1

        function intro2() {
            if ($("#1").hasClass("pushed")) {
                $("#1, #2, #3, #4").removeClass("pushed");
            } else {
                $("#1, #2, #3, #4").addClass("pushed");
            }
        } //end intro2()
        
        function intro3() {
            if ($("#1").hasClass("pushed")) {
                $("#1, #2, #3, #4").removeClass("pushed");
                $("#2").addClass("pushed");
            } else if ($("#2").hasClass("pushed")) {
                $("#1, #2, #3, #4").removeClass("pushed");
                $("#4").addClass("pushed");
            } else if ($("#4").hasClass("pushed")) {
                $("#1, #2, #3, #4").removeClass("pushed");
                $("#3").addClass("pushed");
            } else {
                $("#1, #2, #3, #4").removeClass("pushed");
                $("#1").addClass("pushed");
            }
        } //end intro3()
        
        switch (random) {
        case 1:
            clearInterval(interval2);
            interval2 = setInterval(intro1, random2);
            break;
        case 2:
            clearInterval(interval2);
            interval2 = setInterval(intro2, random2);
            break;
        case 3:
            clearInterval(interval2);
            interval2 = setInterval(intro3, random2);
            break;
        default:
            break;
        } //end switch(random)
    } //intro()
    
    $(".power-switch").click(function () { //turns power on and off
        $(".power-switch").toggleClass("margin-toggle");
        if (!poweron) { //power on -- start it up
            poweron = true;
            $(".wrapcolors").css("box-shadow", "0 0 20vmin white");
            $(".lcd").html("--");
            intro();
            interval = setInterval(intro, 8000);
        } else { //power off -- reset all
            clearInterval(interval);
            clearInterval(interval2);
            clearInterval(interval3);
            strict = false;
            poweron = false;
            gamestarted = false;
            computerdone = false;
            count = 0;
            sequence = [];
            $("#1, #2, #3, #4").removeClass("pushed");
            $(".start-button, .strict-button").removeClass("button-on");
            $(".wrapcolors").css("box-shadow", "0 0 8vmin #999");
            $(".lcd").empty();
        }
    }); //end actual-switch.click (power switch)
    
    $(".start-button").click(function () { //starts a game
        if (poweron && !gamestarted) { //button only works when power is on, and game not started yet
            gamestarted = true;
            clearInterval(interval);
            clearInterval(interval2);
            $("#1, #2, #3, #4").removeClass("pushed");
            $(".start-button").addClass("button-on");
            computerstart();
        }
    }); //end start-button.click
    
    $(".strict-button").click(function () { //toggles strict mode
        if (poweron) {
            $(".strict-button").toggleClass("button-on");
            if (strict) {
                strict = false;
            } else {
                strict = true;
            }
        }
    }); //end strict-button.click
    
    $(".btn").mousedown(function () {
        var id = $(this).attr("id");
        if (poweron && gamestarted && computerdone) { //can't use buttons when cpu is going or other conditions not met
            $(this).addClass("pushed");
            if (id == sequence[playerindex]) { //means it is the right button
                sound[id].play();
                if (playerindex === 19) { //player reached winning length - game won
                    gamestarted = false;
                    computerdone = false;
                    strict = false;
                    count = 0;
                    sequence = [];
                    $(".lcd").html("--");
                    $(".start-button, .strict-button").removeClass("button-on");
                    intro();
                    interval = setInterval(intro, 8000);
                    alert("YOU WIN!");
                }
            } else { //wrong button clicked
                sound[1].play();
                sound[2].play();
                sound[3].play();
                sound[4].play();
                console.log("mousedown wrongbutton");
                if (strict) { //wrong button and strict === true
                    $("#1, #2, #3, #4").addClass("pushed");
                    count = 0;
                    sequence = [];
                } else { //wrong button and strict === false
                    $("#1, #2, #3, #4").addClass("pushed");
                    redo = true;
                } //end wrong button and strict === false
            } //end wrong button clicked
        } //end if (poweron && gamestarted && computerdone)
    }).mouseup(function () { //end .mousedown
        var id = $(this).attr("id");
        if (poweron && gamestarted && computerdone) { //can't use buttons when cpu is going or other conditions not met
            $("#1, #2, #3, #4").removeClass("pushed");
            if (id == sequence[playerindex]) { //means right button was clicked
                if (playerindex == sequence.length - 1) { //right button clicked and turn is over
                    computerdone = false;
                    computerstart();
                } else { //sequence length not reached, this turn is not over
                    playerindex += 1;
                }
            } else { //end right button clicked -- means wrong button was clicked
                if (strict) { //wrong button and strict === true
                    gamestarted = false;
                    computerdone = false;
                    strict = false;
                    $(".lcd").html("--");
                    $(".start-button, .strict-button").removeClass("button-on");
                    intro();
                    interval = setInterval(intro, 8000);
                    alert("YOU LOSE!");
                } else { //wrong button and strict === false
                    computerdone = false;
                    computerstart();
                } //end wrong button and strict === false
            } //end wrong button clicked
        }
    }); //end .mouseup
}); //end document.ready
            
          
!
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