Pen Settings

HTML

CSS

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

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <head><link href="https://fonts.googleapis.com/css?family=VT323" rel="stylesheet"></head>

<div style="height: 5vh;"></div>
<div id = "container">
  <div id= "sound-element"></div>
  <div id = "options"> 
    <div id ="display"> Moves: <span id="moves">1</span></div>
    <div class="holder">
      <button id="reset"></button>
      <br>Reset
    </div>
    <div class="holder">
      <button id="strict"></button>
      <br>Strict
    </div>
  </div>
  <div class="box" id="red"></div>
  <div class="box" id = "blue"></div>
  <div class="box" id="green"></div>
  <div class="box" id="yellow"></div>
</div>
              
            
!

CSS

              
                body {
  background: #3b3b3b;
  font-family: VT323, monospace;
}

#container {
  margin: 0 auto;
  width: 400px;
  height: 400px;
}

.box {
  width: 200px;
  height: 200px;
  margin: 0;
  padding: 0;
  float: left;
}

#red    {background: #FA1111;}
#blue   {background: #11FAFA;}
#yellow {background: #FCE42B;}
#green  {background: #86FA11;}

#options {
  position: relative;
  top: 250px;
  left: 125px;
  width:  150px;
  height: 100px;
  background: white;
}

#display {
  padding: 10px 10px;
  font-size: 1.5em;
  text-align: center;
}

.holder {
  text-align: center;
  margin-top: 5px;
  float: left;
  width: 50%;
}

button {
  margin:0;
  border-radius: 100%;
  background: red;
  margin-bottom: 5px;
  border: none;
  height: 20px;
  width: 20px;
}
button:focus {
  outline: none;
}
#sound-element {
  width: 0;
  height: 0;
  position: absolute;
  opacity: 0;
}
              
            
!

JS

              
                /* Original box colors
   #red    {background: #FA1111;}
   #blue   {background: #11FAFA;}
   #yellow {background: #FCE42B;}
   #green  {background: #86FA11;}
   
   Colors on flash up:
   #red    {background: #BF0000}
   #blue   {background: #00BFBF}
   #yellow {background: #C4AE00}
   #green  {background: #60BF00} */

document.addEventListener("DOMContentLoaded", function(){
  var strictMode = false; // Tracks toggle of Strict mode (Max error=0)
  var colorDict  = ["red", "blue", "yellow", "green"];
                   //Array position corresponds to id in moveList array
  var colors   = [["#FA1111", "#11FAFA", "#FCE42B", "#86FA11"], 
                  ["#BF0000", "#00BFBF", "#C4AE00", "#60BF00"]];
  
  // Array of square ID's
  var buttons = [document.getElementById("red"),
                 document.getElementById("blue"),
                 document.getElementById("yellow"),
                 document.getElementById("green")]
  
  //Move counter id
  var moveCounter = document.getElementById("moves");
  // Add listeners for square click
  for (var i = 0; i<buttons.length; i++)
  {
    buttons[i].addEventListener("click", function() {
     game.validateMoves(colorDict.indexOf(this.id))
    })
  }
  
  //Array of sound files
  var sounds = ["https://s3.amazonaws.com/freecodecamp/simonSound1.mp3", "https://s3.amazonaws.com/freecodecamp/simonSound2.mp3", "https://s3.amazonaws.com/freecodecamp/simonSound3.mp3", "https://s3.amazonaws.com/freecodecamp/simonSound4.mp3"]
  
  document.getElementById("strict").addEventListener("click", function() {
    strictMode  = !strictMode;
    game.strict = strictMode;
    document.getElementById("strict").style.background = (strictMode) ? "#86FA11" : "#FA1111";
  })
  
  function Simon() {
    this.resetColors();
    this.movePosition = 0;
    this.moveList   = [];
    this.errorCount = 0;
    this.strict = strictMode;
    this.generateMove();
  }
  
  Simon.prototype.resetColors = function() {
    for (var i = 0; i < buttons.length; i++)
      buttons[i].style.background = colors[0][i];
  }
  
  Simon.prototype.generateMove = function() {
    this.movePosition = 0;
    this.moveList.push(Math.floor(Math.random()*4));
    moveCounter.innerText = this.moveList.length;
    this.lightSquares(0);
  }
 
  Simon.prototype.lightSquares = function(movePos) {
    //Reset last square
    if (movePos !== 0)
      buttons[this.moveList[movePos-1]].style.background = colors[0][this.moveList[movePos-1]];
    //Light up current square
    if (movePos < this.moveList.length)
      {
        buttons[this.moveList[movePos]].style.background = colors[1][this.moveList[movePos]];
        window.setTimeout(function(){
          game.lightSquares(movePos+1);
        }, 500)
      }
  }
  
  Simon.prototype.gameOver = function(winLoss) {
    moveCounter.innerText = winLoss ? "WIN!" : "LOSE";
    window.setTimeout(function(){game = new Simon();}, 500);
    
  }
  
  Simon.prototype.invalidMove = function() {
    var self = this;
    this.errorCount++;
    if (this.errorCount === 3 || this.strict === true)
      this.gameOver(false);
    this.movePosition = 0;
    window.setTimeout(() => {self.lightSquares(0)}, 500);
  }
  
  Simon.prototype.validateMoves = function(click) {
    document.getElementById("sound-element").innerHTML= 
"<embed src='"+sounds[click]+"' hidden=true autostart=true loop=false>";
    if (click === this.moveList[this.movePosition])
      this.movePosition++;
    else
      this.invalidMove();
    if (this.movePosition === 20)
      this.gameOver(true);
    if (this.movePosition === this.moveList.length)
      window.setTimeout(()=>{this.generateMove()},500);
  }
  
  //Listener for reset button
  document.getElementById("reset").addEventListener("click", function() {
    game = new Simon();
  })
  
  var game = new Simon();
  
})
              
            
!
999px

Console