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.

            
              <link href="https://gitcdn.github.io/bootstrap-toggle/2.2.0/css/bootstrap-toggle.min.css" rel="stylesheet">


<div class="container-fluid main" id="main">    
  <input class="sector" type="button" id="red" onclick="buttonPress('red')" value="">
  <input class="sector" type="button" id="blue" onclick="buttonPress('blue')" value="">
  <input class="sector" type="button" id="orange" onclick="buttonPress('orange')" value="">
  <input class="sector" type="button" id="green" onclick="buttonPress('green')" value="">

 <div class="control" id="control">  
  <div class="controls" id="display"></div>
  
   <div id="onoff-div">
   <input class="controls" id="onoff" type="checkbox" data-toggle="toggle">  
  </div>
   
   <p class="label" id="start-label">Start</p>
   <input class="controls" type="button" id="start" onclick="startGame()" >  
   <p class="label" id="strict-label">Strict</p>
   <input class="controls" type="button" id="strict">   
</div>
    </div>
            
          
!
            
              body {
  background-color: gray;
  color: white;
}

#display {
  font-family: Monospace;
  font-size: 20px;
  border: solid 1px;
  padding: 10px;
  width: 120px;
  bottom: 85px;
  left: 60px;
}

.sector {
  position: absolute;
  width: 230px;
  height: 230px;
  font-size: 40px;
  border-radius: 5px;
  margin: 5px;
  border: solid 0px;
  outline: none;
}

#main {
  position: relative;
  border-radius: 50%;
  border: solid 0px;
  width: 520px;
  height: 520px;
  background-color: #191700;
  margin-top: 50px;
}

.controls { 
  position: absolute;
  font-size: 40px;
  border-radius: 5px;
  margin: 5px; 
  outline: none;
}

.label {
  position: absolute;
  font-family: Monospace;
  font-size: 15px;
}

#start-label {
  top: 30px;
  left: 50px;
}

#strict-label {
  top: 30px;
  right: 50px;
}

#control {
  position: relative;
  border-radius: 50%;
  border: solid 0px;
  width: 250px;
  height: 250px;
  top: 132px;
  left: 122px;
  background-color: #191700;
}

#strict {
  width: 35px;
  height: 35px;
  border: solid 2px;
  border-radius: 50%;
  background-color: #7f7300;
  top: 50px;
  right: 60px;
}

#start {
  width: 35px;
  height: 35px;
  border: solid 2px;
  border-radius: 50%;
  background-color: darkred;
  top: 50px;
  left: 60px;
}

#start:active {
  background-color: red;
}

#onoff-div {  
  position: absolute;
  bottom: 30px;
  left: 95px;
}

#red {
  right: 20px;
  top: 20px;
  border-top-right-radius: 6em;
  border-bottom-left-radius: 3em;
  background-color: darkred;
}

#blue {
  top: 19px;
  left: 19px;
  border-top-left-radius: 6em;
  border-bottom-right-radius: 3em;
  background-color: darkblue;
}
#orange {
  bottom: 20px;
  right: 20px;
  border-bottom-right-radius: 6em;
  border-top-left-radius: 3em;
  background-color: darkorange;
}
#green {
  bottom: 20px;
  left: 20px;
  border-bottom-left-radius: 6em;
  border-top-right-radius: 3em;
  background-color: darkgreen;
}
            
          
!
            
              var soundURL = {"red": "https://s3.amazonaws.com/freecodecamp/simonSound1.mp3", "blue": "https://s3.amazonaws.com/freecodecamp/simonSound2.mp3", "orange": "https://s3.amazonaws.com/freecodecamp/simonSound3.mp3", "green": "https://s3.amazonaws.com/freecodecamp/simonSound4.mp3", "bzz": "http://soundbible.com/grab.php?id=91&type=wav"};
var keys = Object.keys(soundURL);
var start = false;
var strict = false;
var online = false;
var melody = [];
var playerMelody = [];
var turn = 0;
var playerTurn = 0;

off();

$('#start').click(function() {
  if (online) {
    start = true;
  }
  })

$('#strict').click(function() {
  if (online) {
    if (strict) {
      strict = false;
      $('#strict').css("background-color", "#7f7300");
    }
    else {
      strict = true;
      $('#strict').css("background-color", "yellow");
    }    
  }
  })

$('#onoff').change(function() {   //checkbox on-off switch
  if ($(this).prop('checked')) { //when "on", checked property = true
    on()
  }
  if (!$(this).prop('checked')) {
  off()
  }
  });

function startGame() {
  //first iteration of the game
  if (online) {
    lock();
    turn = 0;
    playerTurn = 0;
    melody = [];
    playerMelody = [];
    addNote();
    playMelody(0);
    setTimeout(function() {unLock();}, 800);
  }
}

function buttonPress(color) {
  if (start) {
    //run the game
    button(color);
    playerMelody.push(color);

    if (playerMelody[playerMelody.length-1] != melody[playerTurn] && !strict) { //check if player made mistake, play bzz, start over 
      setTimeout(function() {button("bzz")}, 200);
      playerTurn = 0;
      setTimeout(function() {playMelody(0)}, 1000);
    }
    else if (playerMelody[playerMelody.length-1] != melody[playerTurn] && strict) { //strict game condition
      setTimeout(function() {button("bzz")}, 200);
      setTimeout(function() {startGame();}, 1000);
    }
    else {
      playerTurn++; //player is right
    }
    
    if (playerTurn == turn) { //end of sequence, no mistakes - add a note and play it over.
      if (playerTurn == 20) { //game is over. player win! start over again, repeat until he goes mad.
        $("#display").html("yupi!");
        setTimeout(function() {startGame();}, 1000);
      }
      else {
        lock();
        addNote();
        setTimeout(function() {playMelody(0)}, 500);
        playerTurn = 0;
        setTimeout(function() {unLock();}, turn*800);
      }
    }
  }
  else { //if start is false, simply play the pressed note
  button(color);
  }
}

function button(color) {  
  var audio = new Audio(soundURL[color]);
  audio.play();
  $("#" + color).css("background-color", color);
  setTimeout(function(){$("#" + color).css("background-color", "dark" + color)}, 350);
}

function off() { //reset and off
  $("#display").html("offline");
  start = false;
  strict = false;
  $('#strict').css("background-color", "#7f7300");
  online = false;
  playerTurn = 0;
  turn = 0;
  lock();
}

function on() { 
  $("#display").html("ready");
  online = true;
  unLock();
}

function lock() { //locks buttons
  $(".sector").prop('disabled', true);
}

function unLock() { //unlocks buttons
  $(".sector").prop('disabled', false);
}

function randomColor() {
  var color = keys[Math.floor(Math.random() * 4)];
  return color;
}

function playMelody (i) {          //delayed loop
  setTimeout(function () {
      button(melody[i]); 
      if (i++ < melody.length-1) {
        playMelody(i); 
      }      
   }, 600); 
}

function addNote() {
    var color = randomColor();
    melody.push(color);
    turn++;
    setTimeout(function() {$("#display").html("count: " + turn);}, 1000);

}

//}}}}}
            
          
!
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