cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

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.

Quick-add: + add another resource

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.

Quick-add: + add another resource

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.

            
              <link href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700" rel="stylesheet">


<div id="playerSequence">player Sequence </div>
<div id="turnSequence">Turn Sequence </div>
<div class="simonGame">
  <div class = "resetButton" id = "resetButton"> RESET</div>
  <div class='toggleArea'>   
  <input id="modeToggle" type="checkbox" class="checkbox">
  <label for="modeToggle"><span class="label">STRICT MODE</span>
  </div>
    
  <div class="clear"></div>  
    <div id="count" class="count"></div>  
    
  <div id="1" class="gameButton blue "></div>
  <div id="2" class="gameButton red"></div>
  <div class="clear"></div>
  <div id="3" class="gameButton yellow"></div>
  <div id="4" class="gameButton green"></div>
  <div class = "clear"></div>
</div>  
            
          
!
            
              body {
  background-color: rgb(50,50,50);
  font-family: 'Roboto', sans-serif;
  text-align: center;
  padding-top: 40px;
}
.simonGame {
  margin: 0 auto;
  width: 320px;
  background-color: rgb(200,200,200);
  box-shadow: 5px 5px 15px rgba(0,0,0,0.3);
  border-radius: 5px; 
  padding: 20px;
}


.gameButton {
  width: 150px;
  height: 150px;
  
  
  float: left;
  cursor: pointer;
  transition: all 0.3s;
  -webkit-transition: all 0.3s;
  
  border-radius: 5px;
}

.clear {
  clear: both;
}
.blue {
  background-color: #568FB2;
}
.red {
  background-color: #B25856;
  margin-left: 20px;
}
/*.red:hover {
  background-color: #a95250;
}*/
.red:active {
  box-shadow: -5px -5px 5px rgba(0,0,0,0.2);
}
.yellow {
  background-color: #B28749;
  margin-top: 20px;
}
.green {
  background-color: #45B267;
  margin-left: 20px;
  margin-top: 20px;
 /* background-color: #95FFB6;*/
}


.gameButton.blue.js_active{
  background-color: #95D6FF;
}
.gameButton.red.js_active{
  background-color: #FF9695;
}
.gameButton.yellow.js_active{
  background-color: #FFD395;
}
.gameButton.green.js_active
{
  background-color: #95FFB6;
  /*background-color: #45B267;*/
}
.resetButton {
  padding: 5px;
  background-color: gray;
  cursor: pointer;
  width: 50px;
  border-radius: 5px;
  float: right; 
}
.checkbox {
  display: none;
}
.toggleArea {
  margin-top: 5px;
  margin-bottom: 30px;
  text-align: left;
}
.toggleArea label {
  position: relative;
  left: 30px;
  
}
.toggleArea label::before
 {
  content: "";
    display: inline-block;
    
    height: 16px;
    width: 16px;
    
    border: 1px solid;
    border-radius: 3px;
   top: 0px;
   left: -25px;
}
.toggleArea label::after{
  content: "";
    display: inline-block;
    height: 6px;
    width: 9px;
    border-left: 2px solid;
    border-bottom: 2px solid;
    left: -21px;
    top: 4px;
    transform: rotate(-45deg);
}
.toggleArea label::before,
.toggleArea label::after{
  position: absolute;
}

.toggleArea input[type="checkbox"] + label::after {
  content: none;
}

.toggleArea input[type="checkbox"]:checked + label::after {
    content: "";
}
.toggleArea span:hover{
  cursor: pointer;
}
.label{
  font-family: roboto;
}
.sequenceLength {
  width: 75px;
  height: 25px;
  margin: 5px auto;
  padding: 10px;
  text-align: center;
  
}
  .count {
    height: 30px;
    width: 320px;
    margin: 0 auto;
    margin-bottom: 20px;
    background-color: white;
    line-height: 30px;
    border-radius: 5px;
    
  }
#playerSequence,
#turnSequence {
  display: none;
}
            
          
!
            
              


var s, 
SimonGame = {
  
  init: function() {
    //kick things off
    console.log("kick things off");
    s = this.settings;
    this.addEventListeners();

    this.turnSequence.playAll();
    this.turnSequence.updateDOM();
    //tell turn sequence to play sound and light of its first random turn
  },
  settings: {
    mode: "notstrict",
    sounds: {
    '1': 'https://s3.amazonaws.com/freecodecamp/simonSound1.mp3',
    '2': 'https://s3.amazonaws.com/freecodecamp/simonSound2.mp3',
    '3': 'https://s3.amazonaws.com/freecodecamp/simonSound3.mp3',
    '4': 'https://s3.amazonaws.com/freecodecamp/simonSound4.mp3'
    },
  },  
//game methods
  addEventListeners: function() {
    //simon buttons
    listenToSimonButtons = function(){
      var buttons = document.getElementsByClassName("gameButton");
      for (var i=0; i<buttons.length; i++){
        buttons[i].addEventListener('click', SimonGame.clickResponse, false);
      }  
    },
    //reset button
    listenToResetButton = function(){
      document.getElementById("resetButton").addEventListener('click', SimonGame.reset, false);
    },  
    //mode toggle
    listenToModeToggle = function(){
      document.getElementById("modeToggle").addEventListener('click', SimonGame.toggleMode, false);
    }
    listenToSimonButtons();
    listenToResetButton();
    listenToModeToggle();
  },  
  reset : function(){
    SimonGame.turnSequence.reset();
    SimonGame.turnSequence.updateDOM();
    SimonGame.playerSequence.reset();
    SimonGame.playerSequence.updateDOM();
    SimonGame.init();
  },
  toggleMode : function(){
    if (document.getElementById("modeToggle").checked){
      SimonGame.settings.mode = "strict";
      console.log(SimonGame.settings.mode);
    }
    else {
      SimonGame.settings.mode = "notStrict";
    }
  },
  //play sound for this button
  //make light show for this button
  playLightAndSound : function(x){
    playLight = (function(id){
      console.log("play light "+id);
      document.getElementById(id).classList.add("js_active");
      setTimeout(function(){document.getElementById(id).classList.remove("js_active")}, 250);
    })(x);
    playSound = (function(id){
      console.log("play sound"+id);
      var url = SimonGame.settings.sounds[id];
      var a = new Audio(url);
      a.play();
    })(x);
  },
  clickResponse: function(){
    id = Number(this.id);
    SimonGame.clickResponse2(id);
  },
  clickResponse2 : function(id){
    //light on,off
    //fire sound
    //add to player sequence compare this with same index of other array 
    console.log(this);
    //player-facing response:
    this.playLightAndSound(id);
    
    //internal response:
    this.playerSequence.addTo(id);
    this.playerSequence.updateDOM();
    console.log(this.playerSequence.get());
      //game logic to see if play fits in turn sequence
    analyze = (function(){
      
      //check to see if this turn is right
      var count = SimonGame.turnSequence.getCount();
      var playerCount = SimonGame.playerSequence.get().length-1;
      console.log("count "+count);
      console.log("pseq "+SimonGame.playerSequence.get());
      //if player pressed right button
      if (SimonGame.playerSequence.get()[playerCount] == SimonGame.turnSequence.get()[playerCount]){ 
        console.log(SimonGame.playerSequence.get()[count]);
        console.log(SimonGame.turnSequence.get()[count]);
        console.log("pressed right button")
        //if player has pressed all buttons right
        if (SimonGame.playerSequence.get().length == count){
          if (count == 20){
            alert("you win!!!");
            SimonGame.reset();
          }
          console.log("last item of sequence, adding new element to sequence");
          SimonGame.playerSequence.reset();
          SimonGame.turnSequence.newTurn();
        }
        //else: we expect player is still pressing buttons
      }
      //else: player hit wrong button
      else if (SimonGame.settings.mode == "strict" ){
        document.getElementById("count").innerText = "WRONG, GAME OVER TRY AGAIN";
        setTimeout(function(){ SimonGame.reset(); }, 500);
      }
      else if (SimonGame.settings.mode != "strict"){
        document.getElementById("count").innerText = "TRY AGAIN";
        SimonGame.playerSequence.reset();
        SimonGame.turnSequence.playAll();
      }
    })();  
  },
  //properties of game  
  turnSequence : (function () {
      var _randomMove = Math.floor(Math.random()*4)+1;
      var seq = [_randomMove];
      var _addTo = function (){
        _randomMove = Math.floor(Math.random()*4)+1;
        seq.push(_randomMove);
      };
      var _updateDOM = function(){
          document.getElementById("turnSequence").innerText = seq;
          document.getElementById("count").innerText = seq.length;
        };
      var _playAll = function(){        
        for (i=0; i<seq.length; i++){
          (function(seq,i,time){
            setTimeout(function(){
              SimonGame.playLightAndSound(seq[i]);
            }, time);  
        })(seq,i,500+(500*i));
        
        }
      }
      
      return {
        reset: function(){
          _randomMove = Math.floor(Math.random()*4)+1;
          seq = [_randomMove];
        },
        getCount: function(){
          return seq.length;
        },
        get: function(){
          return seq;
        },
        updateDOM: function(){
          _updateDOM();
        },
        newTurn: function(){  
        _addTo();
        _playAll();
        _updateDOM();
        },
        playAll: function(){
          _playAll();
        }
      };
    })(),
  playerSequence : (function () {
      var seq = [];
      
      return {
        addTo: function(x){
          seq.push(x);  
        },
        reset: function(){
          seq = [];
        },
        get: function(){
          return seq;
        },
        updateDOM: function(){
          document.getElementById("playerSequence").innerText = seq;
        },
        
        
      };
    })(),
    
  
  
};

SimonGame.init();


            
          
!
999px
Loading ..................

Console