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.

            
              <div class="gameBox flex-container">
  <div id="red"></div>
  <div id="green"></div>
  <div id="blue"></div>
  <div id="yellow"></div>
</div>

<div id="leftSide">
  <h1 id="title">SIMON</h1>
  <div id="controls">
    <input type="checkbox" name="onOff-switch" data-off-color="dafault" data-size="small" data-label-text="GAME" data-label-width="50px">

    <input type="checkbox" name="strict-switch" data-off-color="dafault" data-size="small" data-label-text="STRICT" data-label-width="50px">

    <br><br>
    <button type="button" class="btn btn-success btn-sm" id="startBtn">
  PLAY
</button>
  </div>
  <br>
  <div id="score">SCORE: <span id="count">0</span></div>
</div>
            
          
!
            
              html, body {
  background: url("http://oxygennacdn3.oxygenna.com/wp-content/uploads/2015/11/18.jpg");
  height: 100%;
  width: 100%;
  background-repeat: no-repeat;
  background-size: cover;
}

#red, #green, #blue, #yellow {
  width: 225px;
  height: 225px;
  border: 10px solid #dddddd;
}

#red {
  background-color: red;
  border-radius: 100% 0 0 0;
  box-shadow: -10px -10px 2px #333333;
}

#green {
  background-color: green;
  border-radius: 0 100% 0 0;
  box-shadow: 10px -10px 2px #333333;
}

#blue {
  background-color: blue;
  border-radius: 0 0 0 100%;
  box-shadow: -10px 10px 2px #333333;
}

#yellow {
  background-color: yellow;
  border-radius: 0 0 100% 0;
  box-shadow: 10px 10px 2px #333333;
}

.flex-container {
  display: -webkit-flex;
  display: flex;
  -webkit-flex-wrap: wrap; /* Safari */
  flex-wrap: wrap;
  width: 450px;
  height: 450px;
  position: absolute;
  top:0;
  bottom: 0;
  left: 0;
  right: 0;
  margin: auto;
}

#leftSide {
  position: absolute;
  text-align: center;
  top: 150px;
  left: 60px;
  font-family: 'Limelight', cursive;
}

#title {
  font-size: 7em;
  font-family: 'Allerta Stencil', sans-serif;
  color: #551166;
  text-shadow: 5px 5px 1px #222222;
  letter-spacing: 2px;
}

#score {
  font-size: 2em;
  color: #da2313;
  text-shadow: 2px 2px 1px #222222;
  letter-spacing: 2px;
  border: 5px solid #551166;
  border-radius: 50px;
}

            
          
!
            
              var gameOnOff = false;
var gameStart = false;
$(document).ready(function() {
  $("[name='onOff-switch']").bootstrapSwitch();
  $("[name='strict-switch']").bootstrapSwitch();
  var randColorArr;
  var clickedColorArr;
  var countMoves;
  var currMoves;
  var i;
  var noOfColors;
  var gameStrict = false;

  $("[name='onOff-switch']").on('switchChange.bootstrapSwitch', function(event, state) {
    if (state === true) {
      gameOnOff = true;
    } else if (state === false) {
      gameOnOff = false;
      gameStart = false;
    }
  });

  $("[name='strict-switch']").on('switchChange.bootstrapSwitch', function(event, state) {
    if (state === true) {
      gameStrict = true;
    } else if (state === false) {
      gameStrict = false;
    }
  });

  $("#startBtn").click(function() {
    if (gameOnOff === true) {
      console.log("yup");
      gameStart = true;
      newGame();
    }
  });

  function newGame() {
    countMoves = 0;
    currMoves = 0;
    randColorArr = [];
    clickedColorArr = [];
    $("#count").html(countMoves);
    newColor();
  }

  // function to add more colors
  function newColor() {
    countMoves = countMoves + 1;
    if (countMoves == 21) {
      console.log("you win");
    } else {
      console.log(countMoves);
      var colorArray = ['red', 'green', 'blue', 'yellow'];
      var randColor = colorArray[Math.floor(Math.random() * colorArray.length)];
      randColorArr.push(randColor);
      console.log(randColorArr);
      setColorVar();
    }
  }

  function setColorVar() {
    clickedColorArr = [];
    currMoves = 0;
    i = 0;
    noOfColors = randColorArr.length;
    showColor();
  }
  // function to highlight color
  function showColor() {
    var colorDiv;
    colorDiv = "#" + randColorArr[i];
    var originalColor = $(colorDiv).css("background-color");
    $(colorDiv).css("background-color", "rgba(10, 10, 10, 0.6)");
    playSound(randColorArr[i]);
    setTimeout(function() {
      $(colorDiv).css("background-color", originalColor);
    }, 500);
    i++;
    if (i < noOfColors) {
      setTimeout(showColor, 900);
    }
  }

  function playSound(color) {
    if (gameStart && gameOnOff) {
      var soundUrl;
      if (color == 'red') {
        soundUrl = "https://s3.amazonaws.com/freecodecamp/simonSound1.mp3";
      } else if (color == 'green') {
        soundUrl = "https://s3.amazonaws.com/freecodecamp/simonSound2.mp3";
      } else if (color == 'blue') {
        soundUrl = "https://s3.amazonaws.com/freecodecamp/simonSound3.mp3";
      } else if (color == 'yellow') {
        soundUrl = "https://s3.amazonaws.com/freecodecamp/simonSound4.mp3";
      }
      var audio = new Audio(soundUrl);
      audio.play();
    }
  }
  // take user input
  $(document).on("click", ".gameBox", function(event) {
    var colorDiv;
    if (gameOnOff && gameStart && countMoves <= 20) {
      currMoves = currMoves + 1;
      console.log(event.target.id);
      var clickedColor = event.target.id;
      playSound(clickedColor);
      clickedColorArr.push(clickedColor);
      // console.log(clickedColorArr);
      colorDiv = "#" + clickedColor;
      var originalColor = $(colorDiv).css("background-color");
      $(colorDiv).css("background-color", "rgba(10, 10, 10, 0.6)");
      setTimeout(function() {
        $(colorDiv).css("background-color", originalColor);
      }, 500);
      if (currMoves === countMoves) {
        if (JSON.stringify(clickedColorArr) === JSON.stringify(randColorArr)) {
          console.log("true");
          $("#count").html(countMoves);
          setTimeout(newColor, 900);
        } else if (JSON.stringify(clickedColorArr) !== JSON.stringify(randColorArr)) {
          var originalHtml = $("#score").html();
          $("#score").html("!!!");
          $("#score").fadeIn(100).fadeOut(100).fadeIn(100).fadeOut(100).fadeIn(100).fadeOut(100).fadeIn(100);
          setTimeout(function() {
          $("#score").html(originalHtml);
        }, 900);  
          if (gameStrict === false) {
            console.log("again");
            setTimeout(setColorVar, 900);
          } else if (gameStrict === true) {
            console.log("game over");
            setTimeout(newGame, 900);
          }
        }
      }
    }
  });
});
            
          
!
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