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.

            
              <!-- Press Start to play -->
<div class="simon">
  <div class="pad green"></div>
  <div class="pad red"></div>
  <div class="pad yellow"></div>
  <div class="pad blue"></div>
  <div class="center">
    <div class="center-inner">
      <div class="brand">Simon</div>
      <div id="controls">

        <div id="start">
          <div class="value"></div>
          <div class="label">start</div>
        </div>
        <div id="steps">
          <div id="display">
            <div class="digits">
              <div id="digit-1">
                <span class="d1"></span>
                <span class="d2"></span>
                <span class="d3"></span>
                <span class="d4"></span>
                <span class="d5"></span>
                <span class="d6"></span>
                <span class="d7"></span>
              </div>
              <div id="digit-2">
                <span class="d1"></span>
                <span class="d2"></span>
                <span class="d3"></span>
                <span class="d4"></span>
                <span class="d5"></span>
                <span class="d6"></span>
                <span class="d7"></span>
              </div>
            </div>
          </div>
          <div class="label">count</div>
        </div>
        <div id="strict">
          <div class="value">
            <div class="switch">
              <input id="strict-mode" type="checkbox">
              <label></label>
            </div>
          </div>
          <div class="label">strict</div>
        </div>
      </div>
    </div>
  </div>
</div>
<audio class="green-sound" src="https://s3.amazonaws.com/freecodecamp/simonSound1.mp3"></audio>
<audio class="red-sound" src="https://s3.amazonaws.com/freecodecamp/simonSound2.mp3"></audio>
<audio class="yellow-sound" src="https://s3.amazonaws.com/freecodecamp/simonSound3.mp3"></audio>
<audio class="blue-sound" src="https://s3.amazonaws.com/freecodecamp/simonSound4.mp3"></audio>
            
          
!
            
              @import url(https://fonts.googleapis.com/css?family=Candal);

$pad-size: 175px;
$pad-spacing: 5px;
$center-size: 155px;
$metal: #2c3e50;
$label: #ecf0f1;
$red: #c0392b;
$yellow: rgb(195, 195, 15);
$green: #27ae60;
$blue: #2980b9;
$width: $pad-size*2 + $pad-spacing*4;
$height: $pad-size*2 + $pad-spacing*4;

body {
  position: absolute;
  width: 100%;
  top: 50%;
  margin-top: -($height+20)/2;
}

div:focus {
  outline: 0;
}

div.simon {
  position: relative;
  width: $width;
  height: $height;
   margin: 0 auto;
  background: $metal;
  font-size: 0;
  border-radius: 100%;
  padding: 10px;

  > .pad {
    padding: 0;
    margin: $pad-spacing;
    display: inline-block;
    border-radius: 0;
    width: $pad-size;
    height: $pad-size;
    z-index: -2;
  }
  > .red {
    background: $red;
    border-top-right-radius: 100%;
  }

  > .blue {
    background: $blue;
    border-bottom-right-radius: 100%;
  }

  > .yellow {
    background: $yellow;
    border-bottom-left-radius: 100%;
  }

  > .green {
    background: $green;
    border-top-left-radius: 100%;
  }

  .center {
    background: $metal;
    text-align: center;
    color: white;
    width: $center-size;
    height: $center-size;
    position: relative;
    z-index: 10;
    display: block;
    padding: $pad-spacing*2;
    left: $width/2 - $center-size/2 - $pad-spacing*2;
    top: -$height/2 - $center-size/2 - $pad-spacing*2;
    border-radius: 50%;

    .center-inner {
      width: $center-size;
      height: $center-size;
      background: $label;
      border-radius: 50%;

      > .brand {
        font-family: 'Candal', sans-serif;
        padding-top: 26px;
        color: $metal;
        font-size: 36px;
        font-weight: 900;
        display: block;
      }

      #controls {
        .label {
          font-size: 12px;
          color: black;
        }
        > div {
          display: inline-block;
          padding: 0px;
        }

        > #steps {
          // Shown below, to keep code clean
        }

        > #start {
          padding: 5px;

          .value {
            margin: 0 auto;
            margin-bottom: 15px;

            position: relative;
            color: #FFF;
            display: block;
            border-radius: 100%;
            border: solid 1px #D94E3B;
            background: #cb3b27;
            width: 16px;
            height: 16px;
            box-shadow: 0px 5px 0px #84261a;

            &:active {
              box-shadow: 0px 2px 0px #84261a;
              position: relative;
              top: 5px;
            }
          }
        }
        > #strict {
          padding: 5px;
          .value {

          }
        }
      }
    }

  }

}

// Switch
// from: https://tympanus.net/codrops/2012/09/13/button-switches-with-checkboxes-and-css3-fanciness/
.switch {
  position: relative;
  width: 14px;
  height: 30px;
  margin: 0 auto 5px;
  background: black;
  opacity: 0.5;

  input {
    display: block;
    margin: 0 auto 5px;
    position: absolute;
    width: 100%;
    height: 100%;
    z-index: 100;
    opacity: 0;
    cursor: pointer;

    &:checked ~ label { /* Button */
      background: #cbc7bc;
      box-shadow: inset 0 1px 0 white,
      0 0 0 1px #999,
      0 0 5px 1px rgba(0, 0, 0, 0.2),
      0 2px 0 rgba(255, 255, 255, 0.6),
      inset 0 10px 1px #e5e5e5,
      inset 0 11px 0 rgba(255, 255, 255, 0.5),
      inset 0 -45px 3px #ccc;
    }
  }

  label {
    display: block;
    width: 100%;
    height: 100%;
    position: relative;
    border-radius: 5px;
    background: #d2cbc3;
    box-shadow: inset 0 1px 0 white,
    0 0 0 1px #999,
    0 0 5px 1px rgba(0, 0, 0, 0.2),
    inset 0 -10px 0 #aaa,
    0 2px 0 rgba(255, 255, 255, 0.1),
    inset 0 45px 3px #e0e0E0,
    0 8px 6px rgba(0, 0, 0, 0.18);
  }
}

/**
  CSS Digits for our counter.
  from: http://tutorialzine.com/2013/06/digital-clock/
  **/

$digit-width: 20px;
$digit-height: 36px;
$digit-border-width: 4px;
$digit-section-margin: 2px;

#display {
  background: #000000;
  padding: 2px;

  .digits {
    font-size: 0px;

    div {
      text-align: left;
      position: relative;
      width: $digit-width;
      height: $digit-height;
      display: inline-block;
      margin: 2px;

      span {
        opacity: 0.3;
        position: absolute;
        background: red;
        border-color: red;

        // used for the triangle bits that make it look digital
        &::before, &::after {
          content: '';
          position: absolute;
          width: 0;
          height: 0;
          border: 5px solid transparent;
        }

      }
    }

  }
}

// digit pieces

// *******************
// * **    TOP    ** *
// *   **       **   *
// *    **     **    *
// *      ** **      *
// * Left   *  Right *
// *      ** **      *
// *    **     **    *
// *   **       **   *
// * **  Bottom   ** *
// *******************

#display .digits .d1 {
  height: $digit-border-width;
  width: $digit-width - $digit-border-width*2 - 2*$digit-section-margin;
  top: 0;
  left: $digit-border-width + $digit-section-margin;
  &::before {
    border-width: 0 $digit-border-width $digit-border-width 0;
    border-right-color: inherit;
    left: -$digit-border-width;

  }
  &::after {
    border-width: 0 0 $digit-border-width $digit-border-width;
    border-left-color: inherit;
    right: -$digit-border-width;
  }
}

#display .digits .d2 {
  height: $digit-border-width;
  width: $digit-width - $digit-border-width*2 - 2*$digit-section-margin;
  top: $digit-height/2 - $digit-border-width/2;
  left: $digit-border-width + $digit-section-margin;
  &::before {
    border-width: $digit-border-width/2 $digit-border-width/2 $digit-border-width/2 0;
    border-right-color: inherit;
    left: -$digit-border-width/2;
  }
  &::after {
    border-width: $digit-border-width/2 0 $digit-border-width/2 $digit-border-width/2;
    border-left-color: inherit;
    right: -$digit-border-width/2;
  }
}

#display .digits .d3 {
  height: $digit-border-width;
  width: $digit-width - $digit-border-width*2 - 2*$digit-section-margin;
  bottom: 0;
  left: $digit-border-width + $digit-section-margin;
  &::before {
    border-width: $digit-border-width $digit-border-width 0 0;
    border-right-color: inherit;
    left: -$digit-border-width;

  }
  &::after {
    border-width: $digit-border-width 0 0 $digit-border-width;
    border-left-color: inherit;
    right: -$digit-border-width;
  }
}

#display .digits .d4 {
  height: ($digit-height - 3*$digit-border-width) / 2;
  width: $digit-border-width;
  top: $digit-border-width;
  left: 0;
  &::before {
    border-width: 0 $digit-border-width $digit-border-width 0;
    border-bottom-color: inherit;
    top: -$digit-border-width;

  }
  &::after {
    border-width: $digit-border-width/2 $digit-border-width 0 0;
    border-top-color: inherit;
    bottom: -$digit-border-width/2;
  }
}

#display .digits .d5 {
  height: ($digit-height - 3*$digit-border-width) / 2;
  width: $digit-border-width;
  top: $digit-border-width;
  right: 0;
  &::before {
    border-width: 0 0 $digit-border-width $digit-border-width;
    border-bottom-color: inherit;
    top: -$digit-border-width;

  }
  &::after {
    border-width: $digit-border-width/2 0 0 $digit-border-width;
    border-top-color: inherit;
    bottom: -$digit-border-width/2;
  }
}

#display .digits .d6 {
  height: ($digit-height - 3 * $digit-border-width) / 2;
  width: $digit-border-width;
  bottom: $digit-border-width;
  left: 0;
  &::before {
    border-width: 0 $digit-border-width $digit-border-width / 2 0;
    border-bottom-color: inherit;
    top: -$digit-border-width/2;

  }
  &::after {
    border-width: $digit-border-width $digit-border-width 0 0;
    border-top-color: inherit;
    bottom: -$digit-border-width;
  }
}

#display .digits .d7 {
  height: ($digit-height - 3*$digit-border-width) / 2;
  width: $digit-border-width;
  bottom: $digit-border-width;
  right: 0;
  &::before {
    border-width: 0 0 $digit-border-width/2 $digit-border-width;
    border-bottom-color: inherit;
    top: -$digit-border-width/2;

  }
  &::after {
    border-width: $digit-border-width 0 0 $digit-border-width;
    border-top-color: inherit;
    bottom: -$digit-border-width;
  }
}

// Which sections to turn on
// zero
#display .digits div.zero .d1,
#display .digits div.zero .d3,
#display .digits div.zero .d4,
#display .digits div.zero .d5,
#display .digits div.zero .d6,
#display .digits div.zero .d7 {
  opacity: 1;
}

// one
#display .digits div.one .d5,
#display .digits div.one .d7 {
  opacity: 1;
}

// two
#display .digits div.two .d1,
#display .digits div.two .d2,
#display .digits div.two .d3,
#display .digits div.two .d5,
#display .digits div.two .d6 {
  opacity: 1;
}

// three
#display .digits div.three .d1,
#display .digits div.three .d2,
#display .digits div.three .d3,
#display .digits div.three .d5,
#display .digits div.three .d7 {
  opacity: 1;
}

// four
#display .digits div.four .d2,
#display .digits div.four .d4,
#display .digits div.four .d5,
#display .digits div.four .d7 {
  opacity: 1;
}

// five
#display .digits div.five .d1,
#display .digits div.five .d2,
#display .digits div.five .d3,
#display .digits div.five .d4,
#display .digits div.five .d7 {
  opacity: 1;
}

// six
#display .digits div.six .d1,
#display .digits div.six .d2,
#display .digits div.six .d3,
#display .digits div.six .d4,
#display .digits div.six .d6,
#display .digits div.six .d7 {
  opacity: 1;
}

// seven
#display .digits div.seven .d1,
#display .digits div.seven .d5,
#display .digits div.seven .d7 {
  opacity: 1;
}

// eight
#display .digits div.eight .d1,
#display .digits div.eight .d2,
#display .digits div.eight .d3,
#display .digits div.eight .d4,
#display .digits div.eight .d5,
#display .digits div.eight .d6,
#display .digits div.eight .d7 {
  opacity: 1;
}

// nine
#display .digits div.nine .d1,
#display .digits div.nine .d2,
#display .digits div.nine .d4,
#display .digits div.nine .d5,
#display .digits div.nine .d7 {
  opacity: 1;
}

// left W
#display .digits div.win-left .d3,
#display .digits div.win-left .d4,
#display .digits div.win-left .d6,
#display .digits div.win-left .d7 {
  opacity: 1;
}

// right W
#display .digits div.win-right .d3,
#display .digits div.win-right .d5,
#display .digits div.win-right .d6,
#display .digits div.win-right .d7 {
  opacity: 1;
}

            
          
!
            
              var brighten = 60;

var Color = {
    red: ".red", blue: ".blue", green: ".green", yellow: ".yellow",
    isColor: function (value) {
        for (var k in this) {
            if (!this.hasOwnProperty(k)) continue;
            if (this[k] === value) {
                return true;
            }
        }
        return false;
    }
};

// This whole command thing wasn't really necessary, I just wanted to experiment
var Command = function (execute, undo, value) {
    this.execute = execute;
    this.undo = undo;
    this.value = value;
};

var press = function (value) {
    var originalColor = $(value).css("background");
    var lightColor = tintRGB(originalColor, brighten);
    console.log("LIGHTEN: " + originalColor + "transform to -> " + lightColor);
    $(value).css("background", lightColor);
    $(value).css("box-shadow", "0px 0px 5px #fff");

    $(value + "-sound").trigger('load');
    $(value + "-sound").trigger('play');

    return value;
};

var release = function (value) {
    var originalColor = $(value).css("background");
    var darkColor = tintRGB(originalColor, -brighten);
    console.log("DARKEN: " + originalColor + "transform to -> " + darkColor);
    $(value).css("background", darkColor);
    $(value).css("box-shadow", "none");

    return value;
};

var PressCommand = function (value) {
    return new Command(press, release, value)
};


var Simon = function () {
    var current = "";
    var commands = [];

    return {
        execute: function (command) {
            current = command.execute(command.value);
            commands.push(command);
        },
        undo: function () {
            var command = commands.pop();
            if (command === undefined) {
                current = "";
            } else {
                current = command.undo(command.value);
            }
        },
        getCurrentValue: function () {
            return current;
        },
        undoAll: function () {
            while (commands.length > 0) {
                this.undo();

            }
        }
    }
};

// Command handler
var curMove = 0;
var simon = new Simon();
var moves = [];
var speed = 900;
var showingMoves = false;
var gameStart = false;

// Player Controls
$(".pad").mousedown(function () {

    if (!showingMoves && gameStart) {
        var colorName = "." + $(this).attr('class').split(' ')[1];


        if (moves[curMove] === colorName) {
            // check if the move was correct
            simon.execute(new PressCommand(colorName));
            curMove++;
        } else {
            simon.execute(new PressCommand(".yellow"));
            simon.execute(new PressCommand(".red"));
            simon.execute(new PressCommand(".blue"));
            simon.execute(new PressCommand(".green"));


            // The player made a a mistake, so reset and show again
            setTimeout(function () {


                simon.undoAll();


                this.setTimeout(function () {
                    if ($("#strict-mode").prop('checked')) {
                        restart();
                    } else {
                        showMoves();
                        curMove = 0;
                    }
                }, 300);


            }, 500);
        }

        return false;
    }
});

$(document).mouseup(function () {

    function playerMove() {
        return !showingMoves && Color.isColor(simon.getCurrentValue()) && gameStart;
    }

    if (playerMove()) {
        simon.undo();
        if (curMove === moves.length) {
            if (moves.length === 20) {
                // WINNER
                $("#digit-1").attr("class", "win-left");
                $("#digit-2").attr("class", "win-right");
            } else {
                // All moves in current sequence input correctly, add another move and show
                curMove = 0;
                showingMoves = true;
                setTimeout(function () {
                    addMove();
                    showMoves();
                }, 1000);
            }
        }
    }
});

function restart() {
    curMove = 0;
    moves = [];
    speed = 900;
    showingMoves = false;
    simon.undoAll();
    $("#digit-1").attr("class", "");
    $("#digit-2").attr("class", "");

    gameStart = true;
    addMove();
    showMoves();
}

$("#start").find(".value").click(function () {
    if (!showingMoves) {
        restart();
    }
});

function showMoves() {
    showingMoves = true;
    var i = 0;
    simon.execute(new PressCommand(moves[i]));

    // Turn off and show next move
    (function myLoop() {
        setTimeout(function () {
            simon.undo();
            i++;
            if (i < moves.length) {
                simon.execute(new PressCommand(moves[i]));
                myLoop(i);
            } else {
                showingMoves = false;
            }
        }, speed)
    })();
}

function addMove() {
    moves.push(randomMove());
    setDisplay(moves.length);
    if (moves.length === 5) {
        speed = 600;
    }

    if (moves.length === 9) {
        speed = 400;
    }

    if (moves.length === 13) {
        speed = 200;
    }
}

function setDisplay(value) {
    var numbers = "zero one two three four five six seven eight nine".split(" ");
    value = value.toString();

    if (value.length == 2) {
        $("#digit-1").attr("class", numbers[value.charAt(0)]);
        $("#digit-2").attr("class", numbers[value.charAt(1)]);
    }
    else {
        $("#digit-2").attr("class", numbers[value]);
    }
}


// Utility functions
function tintRGB(color, amount) {
    var parts = color.split(",");
    var digit = /\d+/;

    for (var part = 0; part < parts.length; part++) {
        var value = parseInt(parts[part].match(digit));
        parts[part] = clamp(value + amount, 0, 255);
    }

    return "rgb(" + parts.join(",") + ")";
}

function clamp(num, min, max) {
    return num < min ? min : num > max ? max : num;
}

function random(start, end) {
    return Math.floor((Math.random() * end) + start);
}
function randomMove() {
    return Color[Object.keys(Color)[random(0, 4)]];
}
            
          
!
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