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>
  <div class="game">
    <div id="center">
      <div class="count-wrapper">
        <div class="count"></div>
        <p class="count-text">0</p>
      </div>
      <div class="start-wrapper">
        <button type="button" class="btn btn-default" id="start"></button>
        <p class="start-text">START</p>
      </div>
      <div class="strict-wrapper">
        <button type="button" class="btn btn-default" id="strict"></button>
        <p class="strict-text">STRICT</p>
      </div>
      <div class="restart-wrapper">
        <button type = "button" class="btn btn-default" id="restart">RESTART</button>
      </div>
    </div>
      <div id="tl" class="arc 1">
        <audio id="yellow">
        <source src="https://s3.amazonaws.com/freecodecamp/simonSound1.mp3">
      </audio>
      </div>
      <div id="tr" class="arc 2">
        <audio id="blue">
        <source src="https://s3.amazonaws.com/freecodecamp/simonSound2.mp3">
      </audio>
      </div>
      <div id="bl" class="arc 4">
        <audio id="green">
        <source src="https://s3.amazonaws.com/freecodecamp/simonSound3.mp3">
      </audio>
      </div>
      <div id="br" class="arc 3">
        <audio id="red">
        <source src="https://s3.amazonaws.com/freecodecamp/simonSound4.mp3">
      </audio>
      </div>
  </div>
</body>
            
          
!
            
              .game {
  height: 500px;
  width: 600px;
  background-color: black;
  margin: 0px auto;
  transform: translateY(5%);
  border-radius: 0px;
  !important
}

#center {
  width: 200px;
  height: 200px;
  background-color: #ccc;
  border-radius: 100px;
  border: solid #fff 1px;
  margin: 0px auto 0px auto;
  -webkit-transform: translateY(75%);
  position: relative;
}

#tl,
#tr,
#bl,
#br {
  height: 200px;
  width: 200px;
  position: absolute;
  z-index: -1;
  opacity:0.6;
  cursor:pointer;
}

#tl {
  border-radius: 200px 0 0 0;
  background-color: yellow;
  margin: -152.5px auto 0px 95px;
}

#tr {
  border-radius: 0 200px 0 0;
  background-color: blue;
  margin: -152.5px auto 0px 305px;
}

#bl {
  border-radius: 0 0 0 200px;
  background-color: green;
  margin: 52.5px auto 0px 95px;
}

#br {
  border-radius: 0 0 200px 0;
  background-color: red;
  margin: 52.5px auto 0px 305px;
}

.count-wrapper {
  background-color: indigo;
  height: 70px;
  width: 75px;
  margin: 70px auto 0px 10px;
}

.count {
  height: 50px;
  width: 75px;
  background-color: orange;
  border-radius: 25%;
  !important margin: 100px auto 0px 0px;
  margin-left: 0px;
  margin-top: 80px;
  border:1px solid white;
}

.count-text {
  margin: 2.5px auto 0px 10px;
  font-size:1.5em;
  width:15px;
  margin:-40px auto 0px auto;
  text-align:center;
}

.start-wrapper{
  background-color:blue;
  display:inline-block;
  width:40px;
  margin-left:90px;
  height:70px;
  -webkit-transform: translateY(-100%);
}

#start{
  width:20px;
  height:20px;
  border-radius:50%;
  margin-top:20px;
  margin-left:7.5px;
  background-color:green;
}

.start-text{
  margin:12.5px 0px 0px 0px;
  color:white;
}

.strict-wrapper{
  background-color:orange;
  width:40px;
  margin-left:140px;
  -webkit-transform: translateY(-275%);
}

#strict{
  margin-left:7.5px;
  background-color:red;
}

.strict-text{
  color:white;
}

#restart{
  position:absolute;
  margin:-120px 0px 0px 55px;
  cursor:pointer;
}

.lit{
  opacity:1; 
}

            
          
!
            
              var user_light_timeout; //variable to keep track of the timeout method for when the user clicked light goes out. 
$(document).ready(function() {
  simon.init();
  $("#restart").on("click", function(){
    simon = new Simon();
    simon.startGame();
  });
});
//constructor that will recreate the Board at every instant 
var Simon = function() {
  this.sequence = [];
  this.copy = [];
  this.active = "true";
  this.mode = "normal";
  this.turn = "ai";
  this.player_time = true; //boolean to check if player has finished inputting
  var that = this;
  //function to initiate the game
  this.init = function() {
    $("#start").on("click", function() {
      that.startGame();
    });
    //click event handler to turn on strict mode
    $("#strict").on("click", function() {
      that.mode = "strict";
      $(this).css("background-color", "black");
      that.startGame();
    });
  };
  //method to start the game 
  this.startGame = function() {
    this.round = 0;
    $(".count-text").text(this.round);
    this.deactivateSimonBoard();
  };

  this.newRound = function() {
    if (this.player_time == true) {
      this.sequence.push(this.randomNumber(1, 4));
      if(this.sequence.length > 20){
        window.alert("You won! Congrats!");
        simon = new Simon();
        simon.startGame();
      }
      this.animate(this.sequence);
    }

  };
  //method to animate the board
  this.animate = function(sequence) {
    $(".count-text").text(this.round);
    if (this.player_time == true) {
      var i = 0;
      var that = this;
      var interval = setInterval(function() {
        that.lightUp(sequence[i]);

        i++;
        console.log("This is i: " + i);
        if (i > that.sequence.length) {
          clearInterval(interval);
          that.turn = "player";
          that.activateSimonBoard();
        }
      }, 4000);
    }

  };
  //method to light up each tile individually
  this.lightUp = function(tile) {
    if (tile != undefined) {
      console.log("this is the tile to light up: " + tile);
      $("." + tile).css("opacity", "1");
      this.playSound(tile);
      window.setTimeout(function() {
        $("." + tile).css("opacity", "0.6");
      }, 500);
    }

  };
  //method to generate a random number between 1 and 4. Does not need to be a public function. Make it private
  this.randomNumber = function(min, max) {
    return Math.floor(Math.random() * (max - min + 1) + min);
  };
  
  //method to play the audio file of each color
  this.playSound = function(sound){
    console.log(sound);
    switch(sound){
      case 1:
        $("#yellow")[0].play();
        break;
        
      case 2:
        $("#blue")[0].play();
        break;
        
      case 3:
        $("#red")[0].play();
        break;
        
      case 4:
        $("#green")[0].play();
        break;
    }
  }
  
  //function to enable the user to interact with the board
  this.activateSimonBoard = function() {
    this.player_time = false;
    var check_arr = []; //array to check for right inputs from user
    if (this.turn == "player") {
      var simon_this = this; //to assign the this of simon to a variable
      $(".arc").off().on('click', function() {
        console.log('check'); //to check if this function is entered without click

        var _this = this;
        $(this).css("opacity", "1");

        //switch case statement to change id to readable number
        switch ($(this).attr('id')) {
          case "tl":
            check_arr.push(1);
            simon_this.playSound(1);
            break;
          case "tr":
            check_arr.push(2);
            simon_this.playSound(2);
            break;
          case "br":
            check_arr.push(3);
            simon_this.playSound(3);
            break;
          case "bl":
            check_arr.push(4);
            simon_this.playSound(4);
            break;
          default:
            console.log('error');
        }

        user_light_timeout = window.setTimeout(function() {
          $(_this).css("opacity", "0.6");
        }, 1000);

        if (isEquals(check_arr, simon_this.sequence, _this)) {
          console.log("right input");
          simon_this.player_time = true;
          simon_this.turn = "ai";
          simon_this.deactivateSimonBoard();
        } 
      });

    }
  };

  //function to check equality of two arrays
  function isEquals(arr_1, arr_2, tile) {
    var ctr = 0;
    for (var i = 0; i < arr_1.length; i++) {
      ctr++;
      if (arr_1[i] != arr_2[i]) {
        if(simon.mode == "strict"){
          simon.player_time = true;
          simon.turn = "ai";
          $(tile).css("opacity", "0.6");
          clearTimeout(user_light_timeout);
          window.alert("Wrong input. You need to start over");
          simon.startGame();
        }
        else{
          simon.player_time = true;
        //introduced this line here to immediately recreate pattern if wrong element clicked on. 
          window.alert("Wrong input");
          simon.animate(simon.sequence);
        }
      } else if (arr_1[i] == arr_2[i]) {
        console.log("This input is correct");
        if (arr_1.length == arr_2.length && ctr == arr_1.length) {
          return true;
        }
      }
    }
  }

  //method of simon object to deactivate the simon board while player enters input
  this.deactivateSimonBoard = function() {
    console.log("in deactivateSimonBoard");
    var _this = this;
    if (this.turn == "ai") {
      $(".game").off("click");
      this.round++;
      $(".count-text").text(_this.round);
      _this.newRound();
    }
  };
};

var simon = new Simon();
            
          
!
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