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="container-fluid">
    <div class="row">
      <div class="wrapper col-md-offset-4 col-md-4 col-sm-offset-4 col-sm-4 col-xs-6">
        <div id="green" class="circle greenCircle" data-circle="1"></div>
        <div id="red" class="circle redCircle" data-circle="2"></div>
        <div id="blue" class="circle blueCircle" data-circle="3"></div>
        <div id="yellow" class="circle yellowCircle" data-circle="4"></div>
        <div class="controls">
          <h1 class="title">Simon<sup class="fa fa-registered" aria-hidden="true"></sup></h1>
          <h3 class="count">COUNT</h3>
          <div id="counter" class="counter"></div>
          <h3 id="startTag" class="start">START</h3>
          <button id="start" class="startBtn btn btn-primary"type="button" name="start"></button>
          <h3 class="strict">STRICT</h3>
          <button id="strict" class="strictBtn btn btn-primary" type="button" name="strict"></button>
          <!-- Rectangular switch -->
          <h3 class="on">ON</h3>
          <h3 class="off">OFF</h3>
            <div class="onOff">
            <label class="switch">
              <input id="onSwitch" type="checkbox">
              <div class="slider"></div>
            </label>
            </div>
        </div>
      </div>
    </div>
  </div>
            
          
!
            
              /* Basic Reset */
html, body, div, span, applet, object, iframe,
 h1, h2, h3, h4, h5, h6, p, blockquote, pre,
 a, abbr, acronym, address, big, cite, code,
 del, dfn, em, img, ins, kbd, q, s, samp,
 small, strike, strong, sub, sup, tt, var,
 b, u, i, center,
 dl, dt, dd, ol, ul, li,
 fieldset, form, label, legend,
 table, caption, tbody, tfoot, thead, tr, th, td,
 article, aside, canvas, details, embed,
 figure, figcaption, footer, header, hgroup,
 menu, nav, output, ruby, section, summary,
 time, mark, audio, video {
 	margin: 0;
 	padding: 0;
 	border: 0;
 	font-size: 100%;
 	font: inherit;
 	vertical-align: baseline;

   /* Disable mouse selection */
  -webkit-user-select: none; /* Chrome/Safari */
  -moz-user-select: none; /* Firefox */
  -ms-user-select: none; /* IE10+ */

  /* Rules below not implemented in browsers yet */
  -o-user-select: none;
  user-select: none;
 }
/* Basic styles */
* {
  box-sizing: border-box;
}
body {
  font-size: 16px;
  font-family: Helvetica, Verdana, sans-serif;
}
h1 {
  font-size: 2.0em;
}
h3 {
  font-size: 0.6em;
}
.wrapper {
  height: 450px;
  width: 450px;
}
/* Quarter Circles */
.container-fluid {
  position:relative;
  margin-top: 150px;
}
.circle {
  position: absolute;
  border: 14px solid black;
  cursor: pointer;
}
.blueCircle {
  top: 150px;
  left: 150px;
  width: 200px;
  height: 200px;
  background: #094a8f;
  border-radius: 0 0 100px 0;
  -moz-border-radius: 0 0 100px 0;
  -webkit-border-radius: 0 0 100px 0;
}
.yellowCircle {
  top: 150px;
  left: 30px;
  width: 200px;
  height: 200px;
  background: #cca707;
  border-radius: 0 0 0 100px;
  -moz-border-radius: 0 0 0 100px;
  -webkit-border-radius: 0 0 0 100px;
}
.greenCircle {
  top: 30px;
  left: 30px;
  width: 200px;
  height: 200px;
  background: #00a74a;
  border-radius: 100px 0 0 0;
  -moz-border-radius: 100px 0 0 0;
  -webkit-border-radius: 100px 0 0 0;
}
.redCircle {
  top: 30px;
  left: 150px;
  width: 200px;
  height: 200px;
  background: #9f0f17;
  border-radius: 0 100px 0 0;
  -moz-border-radius: 0 100px 0 0;
  -webkit-border-radius: 0 100px 0 0;
}
#yellow.light{
  background-color : #fed93f;
}

#blue.light{
  background-color : #1c8cff;
}

#red.light{
  background-color : #ff4c4c;
}

#green.light{
  background-color : #13ff7c;
}
/*On/Off Switch Slider*/
/* The switch - the box around the slider */

.onOff {
  position: absolute;
  top:125px;
  left: 58px;
}
.on {
  position: absolute;
  top: 130px;
  left: 96px;
  font-weight: 900;
}
.off {
  position: absolute;
  top: 130px;
  left: 35px;
  font-weight: 900;
}
.switch {
  position: relative;
  display: inline-block;
  width: 36px;
  height: 20px;
}

/* Hide default HTML checkbox */
.switch input {display:none;}

/* The slider */
.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #ccc;
  -webkit-transition: .4s;
  transition: .4s;
}

.slider:before {
  position: absolute;
  content: "";
  height: 13px;
  width: 13px;
  left: 4px;
  bottom: 4px;
  background-color: white;
  -webkit-transition: .4s;
  transition: .4s;
}

input:checked + .slider {
  background-color: #2196F3;
}

input:focus + .slider {
  box-shadow: 0 0 1px #2196F3;
}

input:checked + .slider:before {
  -webkit-transform: translateX(13px);
  -ms-transform: translateX(13px);
  transform: translateX(13px);
}
/* CONTROL PANEL */
.controls {
  width: 150px;
  height: 150px;
  border-radius: 40px;
  background-color: #ddd;
  border: 1px solid black;
  position: absolute;
  top: 120px;
  left: 120px;
}
.title {
  position: absolute;
  top: 20px;
  left: 15px;
}
.startBtn {
  position: absolute;
  top: 80px;
  left: 70px;
  width: 30px;
  height: 30px;
  border-radius: 30px;
}
.start {
  position: absolute;
  top: 112px;
  left: 69px;
}
.strictBtn {
  position: absolute;
  top: 80px;
  left: 110px;
  width: 30px;
  height: 30px;
  border-radius: 30px;
}
.strict {
  position: absolute;
  top: 112px;
  left: 108px;
}
.counter {
  color: red;
  text-align: center;
  font-size: 20px;
  width: 40px;
  height: 20px;
  background-color: #000;
  position: absolute;
  top: 85px;
  left: 5px;
}
.count {
  position: absolute;
  top: 112px;
  left: 8px;
}

            
          
!
            
              // ************* MODEL *************
//here goes the melody, the audio and gameplay vars
var model = {
  moveCount: 0,
  strictMode: false,
  speed: 600,
  playing: false,
  melody: [],
  playerSequence: [],

  init: function() {
    var random = null;
    for (var i = 0; i<=19; i++) {
      random = Math.floor((Math.random()*4)+1);
      model.melody.push(random);
    }
  }
};
// ************* CONTROLLER *************
var controller = {
  //initialize all
  init: function() {
    this.onSwitch = document.getElementById("onSwitch");
    this.onSwitch.addEventListener("change", function() {
      if(this.checked) {
        model.init();
        view.init();
      }
      else {
        view.shutdown();
        controller.gameOver();
      }
    });
  },
  //restart the game
  gameOver: function() {
    model.melody = [];
    model.playerSequence = [];
    model.moveCount = 0;
    model.playing = false;
    model.strictMode = false;
    model.init();
    view.render();
  },
  //main gameplay function
  play: function() {
    model.moveCount++;
    view.updateCounter(model.moveCount);
    var i = 0;
    var interval = setInterval(function() {
      var currentTile = controller.translateColor(model.melody[i]);
      view.animate(currentTile, model.speed);
      controller.speedChange(model.moveCount);
      i++;
      if(i == model.moveCount){
        clearInterval(interval);
      }
    }, model.speed+50);
    model.playing = true;
  },
//verify that player input is complete
  checkPlayerTurnComplete: function() {
    if(model.moveCount === model.playerSequence.length) {//check if player has entered enough colors
      if(model.moveCount == 20) { //if this was the 20th turn, player has won, end the game
        controller.win();
      }
      else { //if 20 turns are not over yet, continue playing
        model.playerSequence = [];
        model.playing = false;
        setTimeout(function() {
          controller.play();
        }, 1000);
      }//end else
    }//end outer if
  },
//compare player and ai melody arrays and act according to result
  compareMelodyArrays: function() {
    var melodyCheck = model.playerSequence.every(function(element, index){
      if(element == model.melody[index]){
        return true;
      }
      return false;
    });
    if (!melodyCheck){
      audio.beep(model.speed, 40, 0.9, 0.05, "triangle");
      if(model.strictMode){
        controller.gameOver();
        setTimeout(function() {
          controller.play();
        }, 1000);
      }
      else {
        model.playerSequence = [];
        model.moveCount--;
        model.playing = false;
        setTimeout(function() {
          controller.play();
        }, 1000);
      }//end else
    }//end outer if
    else {
      this.checkPlayerTurnComplete();
    }
  },
//add player move to player array and compare with ai melody
  playerAction: function(tile) {
    model.playerSequence.push(tile);
    this.compareMelodyArrays();
  },
//provide color according to given value
  translateColor: function(value) {
    switch(value) {
      case 1:
      return "green";
      break;
      case 2:
      return "red";
      break;
      case 3:
      return "blue";
      break;
      case 4:
      return "yellow";
      break;
    }
  },
//check status of playing variable that allows user interaction
  checkStatus: function() {
    return model.playing;
  },
  //change the replay speed variable
  speedChange: function(value) {
    if (value >=6){
      model.speed = 520;
     }
    else if (value >=14){
      model.speed = 420;
     }
     return model.speed;
  },
  // toggle strict mode
  changeStrict: function() {
    if (model.strictMode){
      model.strictMode = false;
    }
    else {
      model.strictMode = true;
    }
    return model.strictMode;
  },
  //proclaim player winning and restart the game
  win: function() {
        alert("Congratulations! You've won the game!");
          this.gameOver();
        }
};
// ************* VIEW *************
var view = {
  gameStarted: false,
  init: function() {
    //setup click handler for buttons
    this.startTag = document.getElementById("startTag");
    this.startBtn = document.getElementById("start");
    this.counter = document.getElementById("counter");
    this.startBtn.addEventListener("click", function() {
      if(!view.gameStarted){
        setTimeout(function() {
          view.gameStarted = true;
             this.startTag.textContent = "Restart";
             controller.play();
           }, 1000);
      }
      else {
        console.log("restart");
        controller.gameOver();
        setTimeout(function() {
             controller.play();
           }, 1000);
      }
    });
    this.strictBtn = document.getElementById("strict");
    this.render();
  },

  render: function() {
    // this.startTag.textContent = "START";
    $(".strictBtn").css("background-color", "#337ab7");
    this.counter.textContent = "--";
    this.strictBtn.addEventListener("click", function() {
      var strictMode = controller.changeStrict();
      if (strictMode){
        $(".strictBtn").css("background-color", "pink");
        }
      else {
        $(".strictBtn").css("background-color", "#337ab7");
        }
      });
    //initializing the colors
    this.greenTile = document.getElementById("green");
    this.greenTile.addEventListener("click", function(event) {
      if (controller.checkStatus()) {
        view.animate(event.target.id);
        controller.playerAction("1");
      }
    });
    this.redTile = document.getElementById("red");
    this.redTile.addEventListener("click", function(event) {
      if (controller.checkStatus()) {
        view.animate(event.target.id);
          controller.playerAction("2");
      }
    });
    this.blueTile = document.getElementById("blue");
    this.blueTile.addEventListener("click", function(event) {
      if (controller.checkStatus()) {
        view.animate(event.target.id);
          controller.playerAction("3");
      }
    });
    this.yellowTile = document.getElementById("yellow");
    this.yellowTile.addEventListener("click", function(event) {
      if (controller.checkStatus()) {
        view.animate(event.target.id);
          controller.playerAction("4");
      }
    });
  },

  updateCounter: function(moveCount) {
    this.counter.textContent = moveCount;
  },

  animate: function(tile, speed) {
    var speed = controller.speedChange();
    //var currentTile = document.getElementById(tile);
    switch (tile) {
      case "green":
      audio.beep(speed-50, 391.995, 0.6, 0.05, "sine");
      // audio.green.play();
      break;
      case "red":
      audio.beep(speed-50, 329.628, 0.6, 0.05, "sine");
      // audio.red.play();
      break;
      case "blue":
      audio.beep(speed-50, 195.998, 0.6, 0.05, "sine");
      // audio.blue.play();
      break;
      case "yellow":
      audio.beep(speed-50, 261.626, 0.6, 0.05, "sine");
      // audio.yellow.play();
      break;
    }
    $("#"+tile).addClass("light");
    setTimeout(function() {
      $("#"+tile).removeClass("light");
    }, speed);
  },

  shutdown: function() {
    this.counter.textContent = "";
    this.startTag.textContent = "START";
  },


};
// ************* AUDIO *************
var audio = {
  AudioContext : window.AudioContext || window.webkitAudioContext,
  audioCtx: new AudioContext(),
	volume: 0.6,
  //Beep function courtesy of http://stackoverflow.com/a/29641185/6230785
  //All arguments are optional:
  //duration of the tone in milliseconds.
  //frequency of the tone in hertz.
  //volume of the tone. Default is 1, off is 0.
  //type of tone. Possible values are sine, square, sawtooth, triangle, and custom. Default is sine.
  //callback to use on end of tone
  beep: function(duration, frequency, volume, delayVal, type, callback) {
    var oscillator = this.audioCtx.createOscillator();
    var gainNode = this.audioCtx.createGain();
    var delay = this.audioCtx.createDelay();
    var speed = controller.speedChange();

    oscillator.connect(gainNode);

    gainNode.connect(delay);
    delay.connect(this.audioCtx.destination);

    if (volume){gainNode.gain.value = volume;};
    if (frequency){oscillator.frequency.value = frequency;}
    if (delayVal) {delay.delayTime.value = delayVal}
    if (type){oscillator.type = type;}
    if (callback){oscillator.onended = callback;}

    oscillator.start();
    setTimeout(function(){
      gainNode.gain.setTargetAtTime(0, audio.audioCtx.currentTime, 0.015);}, (duration ? duration : 420));
  }
  //   init: function() {
  //     this.green = new Audio(
  //   "https://s3.amazonaws.com/freecodecamp/simonSound1.mp3"), this.red = new Audio(
  //   "https://s3.amazonaws.com/freecodecamp/simonSound2.mp3"), this.blue = new Audio(
  //   "https://s3.amazonaws.com/freecodecamp/simonSound3.mp3"), this.yellow = new Audio(
  //   "https://s3.amazonaws.com/freecodecamp/simonSound4.mp3");
  // },
};
controller.init();

            
          
!
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