cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - Activehtmlicon-personicon-teamoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

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.

            
              
      <audio>
    <p>Your browser does not support the <code>audio</code> element.</p>
  </audio>

<div class="simon">
  <div class="buttons">
    <div class="button top left "></div>
    <div class="button top right"></div>
    <div class="button bottom left"></div>
    <div class="button bottom right"></div>
  </div>
  <div class="controls">
    <div class="name">Simon</div>

    <div class="counter"><span class="counter_span">- -</span></div>
  
    <div class="button-row">
      <div class="group start-group">
        <div class="start">Start</div>
        <span class="light start-light"></span>
      </div>
        
      <div class="group reset-group">
        <div class="reset">Reset</div>
        <span class="light reset-light"></span>
      </div>
      <div class="group strict-group">
        <div class="strict">Strict</div>
        <span class="light strict-light on"></span>
      </div>
    </div>

  </div>


            
          
!
            
              /*
@import url(https://fonts.googleapis.com/css?family=Lato:400,300,700);
*/
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: lato, san-serif;
}

.simon {
  position: relative;
  height: 500px;
  width: 500px;
  background-color: #333;
  border-radius: 50%;
  margin: 5% auto;
}

.top, .bottom, .right, .left {
  position: absolute;
  width: 45%;
  height: 45%; 
}

.top {
  top: 5%;
  border-bottom: #333 solid 10px;
}

.bottom {
  top: 50%;
    border-top: #333 solid 10px;
}

.right {
  left: 50%;
  border-left: solid #333 10px;
}

.left {
  left: 5%;
  border-right: solid #333 10px;
}

.top.left {
  background-color: green;
  border-radius: 100% 5% 5% 5%;
  
}

.top.right {
  background-color: red;
  border-radius:5%  100% 5% 5%;
  
}


.bottom.left {
  background-color: yellow;
  border-radius:  5%  5% 5% 100% ;
  
}

.bottom.right {
  background-color: blue;
  border-radius:  5% 5% 100% 5% ;
  
}

.controls {
  position: relative;
  background-color: white;
  height: 40%;
  width: 40%;
  border-radius: 50%;
  top: 30%;
  left: 30%;
  border: solid #333 15px; 
}

.name {
  text-align: center;
  margin: 10% 0;
  font-size: 2em;
}

.counter {
  position: absolute;
  margin: auto;
  background-color: #333;
  color: #49fb35;
  text-align: center; 
  height: 20%;
  width: 40%;
  left: 0;
  top: 0px;
  right: 0;
  bottom: 20px;
  border-radius: 5px;
}



.button-row {
  position: absolute;
  max-width: 150px;
  margin: 0 auto;
  //clear: both;
  left: 0;
  top: 0px;
  right: 0;
  bottom: 0px;
  top: 55%;
  left: 10%;
}

.light {
  float: left;
  padding: 0 4%;
}

.group {
  width: 30%;
  float: left;
  font-size: .8em;
  text-align: center;
}

.light {
  width: 18px;
  height: 18px;
  background-color: red;
  margin: 10px;
  border-radius: 50%;
  box-shadow: 3px 3px 3px #333333;
}

.on {
  background-color: green;
}

.btn-pressed {
  opacity: 0.3;
  filter: alpha(opacity=30); /* For IE8 and earlier */
  
}

.pressed {
  box-shadow: 1px 1px 1px #aaaaaa;
  box-shadow: -1px -1px 1px #aaaaaa;
  
}

.counter_span {
  margin: 15px 0 0 0;
  display: inline-block;
}
            
          
!
            
              $(document).ready(function() {
  console.log("ready!");

  var lightSequence = []
  var round = 1
  var turn = 0
  var whoseTurn = 'computer'
  var gameOver = false
  var misses = 0 
  var isCorrect = false
  var strictMode = false

  var sounds = [
    new Audio('https://s3.amazonaws.com/freecodecamp/simonSound1.mp3'),
    new Audio('https://s3.amazonaws.com/freecodecamp/simonSound2.mp3'),
    new Audio('https://s3.amazonaws.com/freecodecamp/simonSound3.mp3'),
    new Audio('https://s3.amazonaws.com/freecodecamp/simonSound4.mp3')
  ];
  
  $('.start-light').click(function() {
    playGame()  
  });
  
   $('.reset-light').click(function() {
    gameOver = true
    updateCounter("R") 
  });
  
  $('.strict-light').click(function(){
    strictMode = (!strictMode)
  })

  function init() {
    console.log("initializing game")
     lightSequence = []
     round = 1
     turn = 1
     whoseTurn = "computer"
     gameOver = false
     misses=0
     isCorrect = false
  }
  
  function playGame(){
    init()
    lightSequence = generateLightSequence()
    playComputerSequence(lightSequence, round)
  }
  
  $('.button').click(function() {
    console.log("round: "+ round + ", turn: "+ turn)  
    if ((turn<=round) && (whoseTurn==="player") && (gameOver===false)){
      var pressed= $('.button').index(this)
      isCorrect = checkUserInput(pressed, turn) 
      if (!isCorrect) {  
        if (!strictMode) {
          if (misses===0) {
            misses +=1
            updateCounter("X")
            console.log("Missed once, try again after computer plays")
            whoseTurn="computer"
            repeatSequence(lightSequence, round)
          } else if (misses===1) {
            updateCounter("XX")
            console.log("Game Over!")
            gameOver =  true
            whoseTurn="computer"
            return
          }
        }
      } else {
        if (turn===round) {
          console.log("Next Round")
          turn = 0
          round += 1
          playComputerSequence(lightSequence, round)
        } else {
          console.log("Good job, keep going")
          turn += 1
        }
      }
    } else  if (whoseTurn==='computer') {
      playComputerSequence(lightSequence, round)
      
    } else if (gameOver) {
      updateCounter("W")
      gameOver=true
    }
  })

  

  function checkUserInput(pressed, turn) {
    console.log("checking input")
    console.log("Expected: " + lightSequence[turn-1])
    console.log("Submitted: " + pressed)
      if (pressed===lightSequence[turn-1]){
        console.log("correct button")
        return true
      } else {
        console.log("wrong button")
        return false
      }
    }
    
  function repeatSequence(lights,round){
    for (i=0; i<round; i++) {
          let button = jQuery('.button').eq(lights[i]);
            (function(i) {
              var timerID = setTimeout(function() {
                button.toggleClass("btn-pressed");
                console.log("Seq: " + i, "Light: " + lights[i]);
                playSound(sounds[lights[i]]);
                setTimeout(function(){
                   button.toggleClass("btn-pressed");
                },600)
              }, i * 1000);
            })(i);
        }
      console.log("Player gets 1 more turn")
      whoseTurn='player'
      turn=1
  }
  
  
  function playComputerSequence(lights, round) {
    if (!gameOver){
     setTimeout(function() {
       //this sets a delay so game is not rushed
               
    console.log("Playing Computer Sequence")
    console.log("Round: ", round)
    updateCounter(round)
    //console.log(lights)
      for (i=0; i<round; i++) {
          let button = jQuery('.button').eq(lights[i]);
            (function(i) {
              var timerID = setTimeout(function() {
                button.toggleClass("btn-pressed");
                console.log("Seq: " + i, "Light: " + lights[i]);
                playSound(sounds[lights[i]]);
                setTimeout(function(){
                   button.toggleClass("btn-pressed");
                },600)
              }, i * 1000);
            })(i);
        }
      console.log("Waiting for Player")
      whoseTurn='player'
      turn=1
      misses=0
       
        },800)
  
    }
  }
  
  function generateLightSequence() {
    console.log("generating light sequence")
    arr=[]
    for (var i = 0; i < 20; i++) {
      var sequence = Math.floor(Math.random() * 4)
      arr.push(sequence);
    }
    console.log(arr)
    return arr;
  }

  $('.light').click(function() {
    $(this).toggleClass("on");
  });

  $('.light').mousedown(function() {
    $(this).toggleClass("pressed");
  })

  $('.light').mouseup(function() {
    $(this).toggleClass("pressed");
  })

  $('.button').mousedown(function() {
    $(this).toggleClass("btn-pressed");
    console.log($('.button').index(this));
    playSound(sounds[$('.button').index(this)]);
  })

  $('.button').mouseup(function() {
    $(this).toggleClass("btn-pressed");
  })

  function updateCounter(step) {
    $('.counter').text(step);
  };

  function playSound(soundFile) {
    soundFile.play();
  };

});
            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console