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>
  <h1 id="winMsg">YOU WIN!</h1>
  <div class="container">
    <div id="controlCenter">
      <h1>SIMON</h1>
      <div id="counter">
        0
      </div>
      <div id="onButton">  
      </div>
      <div id="strictMode">
        STRICT
      </div>
    </div>
    
    
    <div class="row">
      <div class="col-md-6 col-xs-6">
        <div id="simonButton1">

        </div>
      </div>

      <div class="col-md-6 col-xs-6">
        <div id="simonButton2">

        </div>
      </div>


    </div>
    
    <div class="row">
      <div class="col-md-6 col-xs-6">
        <div id="simonButton3">

        </div>
      </div>

      <div class="col-md-6 col-xs-6">
        <div id="simonButton4">

        </div>
      </div>


    </div>


  </div>
  
  <audio id="button1Audio">
    <source src="https://s3.amazonaws.com/freecodecamp/simonSound1.mp3" type="audio/mpeg">
  </audio>
  <audio id="button2Audio">
    <source src="https://s3.amazonaws.com/freecodecamp/simonSound2.mp3" type="audio/mpeg">
  </audio>
  <audio id="button3Audio">
    <source src="https://s3.amazonaws.com/freecodecamp/simonSound3.mp3" type="audio/mpeg">
  </audio>
  <audio id="button4Audio">
    <source src="https://s3.amazonaws.com/freecodecamp/simonSound4.mp3" type="audio/mpeg">
  </audio>


</body>
            
          
!
            
              body {
  background-image:url("http://colonialflowers.net/wp-content/uploads/2013/04/wood-background.png");
  background-repeat:no-repeat;
  background-size:cover;
}

.container {
  width:50%;
  margin-top:25px;
}

#winMsg {
  position:absolute;
  top:50%;
  left:50%;
  z-index:101;
  color:white;
  transform:translate(-50%,-50%);
  font-family:cursive;
  letter-spacing:10px;
  font-weight:700;
  display:none;
  transition:transform 1.5s;
}

#controlCenter {
  z-index:100;
  height:275px;
  width:275px;
  background-color:#1a1a1a;
  position:absolute;
  left:50%;
  transform:translate(-50%,0);
  border-radius:100%;
  margin-top:150px;
  
}

#controlCenter h1 {
  color:white;
  font-family:impact;
  font-size:5em;
  text-align:center;
  margin-top:80px;
  letter-spacing:3px;
}

#counter {
  height:40px;
  width:40px;
  background-color:grey;
  margin-left:75px;
  text-align:center;
  line-height:40px;
  font-size:1.5em;
  border-radius:3px;
}

#onButton {
  width:25px;
  height:25px;
  background-color:green;
  border-radius:100%;
  position:absolute;
  top:175px;
  left:160px;
  border:1px solid white;
  cursor:pointer;
}

#strictMode {
  height:25px;
  width:50px;
  background-color:grey;
  border-radius:10px;
  position:relative;
  top:20px;
  margin:auto;
  font-size:13px;
  text-align:center;
  line-height:25px;
  font-family:impact;
  border:1px solid white;
  color:white;
  cursor:pointer;
}

.col-md-6 {
  height:300px;
}

#simonButton1 {
  height:300px;
  width:300px;
  background-color:green;
  border-top-left-radius:100%;
  position:absolute;
  right:0px;
  border:15px solid #1a1a1a;
  border-right-width:10px;
  border-bottom-width:10px;
  box-shadow: -3px 3px 5px 5px #262626;
  cursor:pointer;
}

#simonButton2 {
  height:300px;
  width:300px;
  background-color:red;
  border-top-right-radius:100%;
  position:absolute;
  left:0px;
  border:15px solid #1a1a1a;
  border-left-width:10px;
  border-bottom-width:10px;
  cursor:pointer;
}

#simonButton3 {
  height:300px;
  width:300px;
  background-color:yellow;
  border-bottom-left-radius:100%;
  position:absolute;
  right:0px;
  border:15px solid #1a1a1a;
  border-right-width:10px;
  border-top-width:10px;
  box-shadow: -3px 2px 5px 4px #262626;
  cursor:pointer;
}

#simonButton4 {
  height:300px;
  width:300px;
  background-color:blue;
  border-bottom-right-radius:100%;
  position:absolute;
  left:0px;
  border:15px solid #1a1a1a;
  border-left-width:10px;
  border-top-width:10px;
  cursor:pointer;
  box-shadow:-5px 3px 3px 1px #262626;
}
            
          
!
            
              $(document).ready(function() {
  var game = {
    moves: [], //step list
    step: 0, //track the steps in playMoves function
    currentStep: 0, //step player is on while entering moves
    started: false, //has game started
    strictMode: false //strict mode game flag
  };

  var btn1Event = function() {
    playButton1();
    if (game.started) {
      checkBtn(1); //check if 1 is correct
    }
  }
  var btn2Event = function() {
    playButton2();
    if (game.started) {
      checkBtn(2); //check if 1 is correct
    }
  }
  var btn3Event = function() {
    playButton3();
    if (game.started) {
      checkBtn(3); //check if 1 is correct
    }
  }
  var btn4Event = function() {
    playButton4();
    if (game.started) {
      checkBtn(4); //check if 1 is correct
    }
  }

  //buttons bound below
  $("#simonButton1").bind("click", btn1Event);
  $("#simonButton2").bind("click", btn2Event);
  $("#simonButton3").bind("click", btn3Event);
  $("#simonButton4").bind("click", btn4Event);
  $("#strictMode").bind("click",function() {
          if(game.strictMode) {
            game.strictMode=false; //turn off strict mode
            $(this).css("color","white");
          }
          else {
            game.strictMode = true; //set strict mode
            $("#strictMode").css("color","#990000");
          }
        });

  var playButton1 = function() { //plays button1
    $("#button1Audio")[0].play();
    $("#simonButton1").css("background-color", "#004d00");
    setTimeout(function() {
      $("#simonButton1").css("background-color", "green");
    }, 250);

  }
  var playButton2 = function() { //plays button2
    $("#button2Audio")[0].play();
    $("#simonButton2").css("background-color", "#cc0000");
    setTimeout(function() {
      $("#simonButton2").css("background-color", "red");
    }, 250);

  }
  var playButton3 = function() { //plays button3
    $("#button3Audio")[0].play();
    $("#simonButton3").css("background-color", "#cccc00");
    setTimeout(function() {
      $("#simonButton3").css("background-color", "yellow");
    }, 250);

  }
  var playButton4 = function() { //plays button4
    $("#button4Audio")[0].play();
    $("#simonButton4").css("background-color", "#0000cc");
    setTimeout(function() {
      $("#simonButton4").css("background-color", "blue");
    }, 250);

  }

  var addTurn = function() { //adds a step
    var rand = Math.floor(Math.random() * 4) + 1;
    switch (rand) {
      case 1:
        playButton1();
        break;
      case 2:
        playButton2();
        break;
      case 3:
        playButton3();
        break;
      case 4:
        playButton4();
        break;
    }
    game.moves.push(rand); //add to moves list
    var count = $("#counter").html();
    $("#counter").html((Number.parseInt(count) + 1).toString()); //increment counter

  }

  var playMoves = function() { //play all steps so far
    //no clicks while playing, disable btns here!!

    $("#simonButton1").unbind("click", btn1Event);
    $("#simonButton2").unbind("click", btn2Event);
    $("#simonButton3").unbind("click", btn3Event);
    $("#simonButton4").unbind("click", btn4Event);

    game.currentStep = 0; //need to reset this for next player try
    game.step = 0;
    if (game.moves.length == 0) { //if no moves, don't play
      return;
    }
    var play = setInterval(function() { //play all steps
      switch (game.moves[game.step]) {
        case 1:
          playButton1();
          break;
        case 2:
          playButton2();
          break;
        case 3:
          playButton3();
          break;
        case 4:
          playButton4();
          break;
      }
      game.step = game.step + 1;
      if (game.step == game.moves.length) { //end of play sequence
        //ENABLE THE BTNS HERE!!

        $("#simonButton1").bind("click", btn1Event);
        $("#simonButton2").bind("click", btn2Event);
        $("#simonButton3").bind("click", btn3Event);
        $("#simonButton4").bind("click", btn4Event);
        clearInterval(play);
      }
    }, 1000);

  }

  var playMoves_addTurn = function() { //play all steps so far AND add a step at the end
    //no clicks while playing, disable btns here!!

    $("#simonButton1").unbind("click", btn1Event);
    $("#simonButton2").unbind("click", btn2Event);
    $("#simonButton3").unbind("click", btn3Event);
    $("#simonButton4").unbind("click", btn4Event);

    game.currentStep = 0; //need to reset this for next player try
    game.step = 0;
    if (game.moves.length == 0) { //if no moves, don't play
      return;
    }
    var play = setInterval(function() { //play all steps
      switch (game.moves[game.step]) {
        case 1:
          playButton1();
          break;
        case 2:
          playButton2();
          break;
        case 3:
          playButton3();
          break;
        case 4:
          playButton4();
          break;
      }
      game.step = game.step + 1;
      if (game.step == game.moves.length) { //end of play sequence
        clearInterval(play);
        setTimeout(addTurn, 700); //add turn at the end!!!
        $("#simonButton1").bind("click", btn1Event); //enable btns
        $("#simonButton2").bind("click", btn2Event);
        $("#simonButton3").bind("click", btn3Event);
        $("#simonButton4").bind("click", btn4Event);
      }
    }, 700);

  }
  
  var winAnimation = function() { //win msg and reset game
    $(".container").fadeOut(1500);
    setTimeout(function() {
      $("#winMsg").css("display","block");
      $("#winMsg").animate({fontSize:"75px"},1000);
      setTimeout(function() {
        game.moves = []; //reset game
    game.currentStep = 0; //reset current step
    game.step = 0;
    $("#counter").html("0");
        $("#winMsg").css("display","none");
        $(".container").fadeIn();
      },2000);
    },1500);
   
    
  }

  var checkBtn = function(num) { //check btn num
    if (game.moves[game.currentStep] == num) { //if correct
      game.currentStep = game.currentStep + 1;
      if (game.currentStep == 21) { //if 20 steps completed, end the game!
        winAnimation(); //player wins, start over

      } else if (game.currentStep == game.moves.length) { //if all correct
        setTimeout(function() {
          playMoves_addTurn(); //play the moves so far and add a turn              
        }, 1000);
      }
    } else { //if wrong
      if (game.strictMode) { //if in strict mode, reset
        $("#counter").html("X");
        setTimeout(function() {
          $("#onButton").click();
        },1500);
        return;
      }
      game.currentStep = 0; //reset currentStep
      var temp = $("#counter").html();
      $("#counter").html("X");
      setTimeout(function() {
        $("#counter").html(temp);
        playMoves();
      }, 2000);

    }

  }

  $("#onButton").on("click", function() { //start or reset
    $(this).css("border-color", "black");
    setTimeout(function() {
      $("#onButton").css("border-color", "white");
    }, 200);
    //clearInterval(play); //what if reset while playing all?
    game.moves = []; //reset game
    game.currentStep = 0; //reset current step
    game.step = 0;
    $("#counter").html("0");

    setTimeout(function() { //play the first step
      addTurn();
      game.started = true;
    }, 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