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.

            
              <header>
<link href="https://fonts.googleapis.com/css?family=Oswald:700" rel="stylesheet">
  <link href="https://fonts.googleapis.com/css?family=Orbitron" rel="stylesheet">
  <base href="https://s3.amazonaws.com/freecodecamp/">
  </header>

<audio preload="auto" > simonSound1.mp3 </audio>
<audio preload="auto" > simonSound2.mp3 </audio>
<audio preload="auto" > simonSound3.mp3 </audio>
<audio preload="auto" > simonSound4.mp3 </audio>

<body>
<audio id="play1" src="simonSound1.mp3" ></audio>
<audio id="play2" src="simonSound2.mp3" ></audio>  
<audio id="play3" src="simonSound3.mp3" ></audio>
<audio id="play4" src="simonSound4.mp3" ></audio>
<div id="circle">  
    <div id="red" onclick="getChoice()"></div>
    <div id="green" onclick="getChoice()"></div>
    <div id="blue" onclick="getChoice()"></div>
    <div id="yellow" onclick="getChoice()"></div> 
    <div id="controls">
         <div id="simon">Simon</div>   
         <div id="count">
              <div id="countText">--</div>
         </div> <!-- end count -->
         <div id="start" onclick="startGame()"></div>
         <div id="strict" onclick="stricToggle()"></div><br>
         <div id="txtLine"><span id="wrdCount">Count</span><span id="wrdStart">Start</span><span id="wrdStrict">Strict</span></div> <!-- end txtLine -->
         <div id="onOffLine"><span id="wrdOff">OFF</span><span id="switchBox" onclick="toggle()"><span id="switch" class="powerOff"></span></span><span id="wrdOn">ON</span></div> <!-- end onOffLine -->
    </div> <!-- end controls -->
  </div> <!-- end circle -->
  <div id="shadow"></div>
  
<div id="feedback">Simon says:<span id="simonArray"></span><br>
    You say:<span id="userArray"></span><br>
    i:<span id="i"></span><br>
    j:<span id="j"></span><br>
  </div>
-->

  <button class="test" onclick="reset()">reset</button>
</body>
            
          
!
            
              body {
    font-family: 'Oswald', sans-serif;
  margin-top: 20px;
  background-image: url("http://www.richardkeightley.com/Images/walnut.jpg");
}

#circle {
  background: #2c3539;
  height: 400px;
  width: 400px;
  border-radius: 50%;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  box-shadow: inset 0px 0px 16px black;
  z-index: -2;
  }

#shadow {
  height: 400px;
  width: 400px;
  border-radius: 50%;
  box-shadow: 5px 5px 20px black;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  z-index: -3;
}

#red, #green, #blue, #yellow {
  height: 180px;
  width: 180px;
  border-radius: 0%;
  position: absolute;
}

#green {
  left: 3%;
  top: 3%;
  border-top-left-radius: 100%;
  background: green;
}

#red {
  left: 52%;
  top: 3%;
  border-top-right-radius: 100%;
  background: red;
}

#blue {
  top: 52%;
  left: 52%;
  border-bottom-right-radius: 100%;
  background: blue;
}

#yellow {
  top: 52%;
  left: 3%;
  border-bottom-left-radius: 100%;
  background: yellow;
}
.on {
  opacity: 1.0;
   transition: opacity 0.1s linear;
}

.off {
  opacity: 0.5;
   transition: opacity 0.1s linear;
}

#controls {
  background: white;
  height: 220px;
  width: 220px;
  border-radius: 50%;
  position: absolute;
  top: 20%;
  left: 20%;
  z-index: 1;
  border: 10px;
  border-style: solid;
  border-color: #2c3539;
}

#simon {
  font-size: 40px;
  color: black;
  z-index: 1;
  position: absolute;
  top: 10%;
  left: 30%;
}

#count {
  position: absolute;
  top: 90px;
  left: 15px;
  width: 60px;
  background: #0c090a;
  height: 40px;
  border-radius: 10%;
  border: 2px;
  border-color: gray;
  border-style: solid;
}

#countText {
  color: red;
  font-size: 30px;
  font-family: 'Orbitron', sans-serif;
  text-align: center;
}
.countTextOff {
    opacity: 0.2;
  }
.countTextOn {
  opacity: 1.0;
}

#start {
  position: absolute;
  top: 92px;
  left: 90px;
  height: 32px;
  width: 32px;
  background: darkgreen;
  border-radius: 50%;
  border-color: gray;
  border-style: solid;
  border-width: 4px;
  box-shadow: 2px 2px 4px;
  z-index: 2;
}
#start:active {
  transform: translateY(1px);
}

#strict {
  position: absolute;
  top: 96px;
  left: 154px;
  height: 30px;
  width: 30px;
  border-radius: 10%;
  border-color: gray;
  border-style: solid;
  border-width: 2px;
  box-shadow: 1px 1px 2px;
  z-index: 10;
}
.strictOn {
  background-color: red;
}
.strictOff {
  background-color: darkred;
}

#txtLine {
  font-size: 14px;
  color: #342;
  position: absolute;
  top: 62%;
  left: 14%;
}
#wrdStart{
  position: absolute;
  left: 67px;
}
#wrdStrict{
  position: absolute;
  left: 126px;
}

#onOffLine {
  font-size: 14px;
  color: #342;
  position: absolute;
  top: 75%;
  left: 40%;
}
#wrdOff {
  position: absolute;
  left: -25px;
}
#wrdOn {
  position: absolute;
  left: 57px;
}

#switchBox {
  position: absolute;
  top: 0%;
  left: 0px;
  height: 20px;
  width: 50px;
  background: black;
  border-radius: 10%;
  border-color: gray;
  border-style: solid;
  border-width: 2px;
}
#switch {
  height: 18px;
  width: 18px;
  background: lightblue;
  z-index: 2;
  position: absolute;
  transition: left .1s linear;
}
.powerOff {
  left: 1px;
}
.powerOn {
  left: 30px;
}

#feedback {
  position: absolute;
  height: 100px; width: 250px;
  background-color: white;
  border: black solid 2px;
}
            
          
!
            
              // global variables
var power=0, strict=0, count, choice=0;
var winTurns = 5;
var arrSimon = [];
var arrUser = [];
var i,j; // initialized in reset() function
var ran = 0;
var choice = 0;


$(document).ready(function(){
    reset();
   dim();
   initText();
  });

function winner(){
  document.getElementById("countText").textContent = ": )"; // displays the count
    bright(1);
    bright(2);
    bright(3);
    bright(4);
}

function simonSays(){
  arrUser = [];
  document.getElementById("userArray").textContent = arrUser;
  count++; // increments the count
  document.getElementById("countText").textContent = count; // displays the count
  if (count>winTurns) 
    winner();
  else {
      ran = Math.floor(Math.random() * 4) + 1; // get a random number, 1-4
      arrSimon.push(ran); // push # to the array
      document.getElementById("simonArray").textContent = arrSimon; // display Simon's array
      j=0;
      cycleSim(); // play Simons array
  }
}

// recursive function that goes thorugh the Simon array, activating light and sound
function cycleSim() {
    j++;
    bright(arrSimon[j-1]); // turn on a light in the array
    if ( j < arrSimon.length) {
        setTimeout(cycleSim, 1050-(count*50)); // call self  again after a second
     }
}

function getChoice() {
  choice = 0;
  document.getElementById("i").textContent = i;
if($(event.target).is('#red'))
        choice = 1;
 if($(event.target).is('#blue'))
        choice = 2;  
  if($(event.target).is('#yellow'))
        choice = 3;  
  if($(event.target).is('#green'))
        choice = 4;
  arrUser.push(choice); //  add choice to end of user's array
  document.getElementById("userArray").textContent = arrUser;
  bright(choice); // play the light and sound for that choice
  checkIfCorrect();
}

function checkIfCorrect(){
  if (arrUser[i]!=arrSimon[i] && strict == 1) { // if choice is NOT the element under consideration and strict mode IS on
      reset();
      status();
      setTimeout(startGame,1000);
    }
  else if (arrUser[i]!=arrSimon[i] && strict == 0) { // same, but strict mode NOT on
      i = 0;
      arrUser = []; // reset the user's array
      j = 0;
      setTimeout(cycleSim,1000); // go through simon's Array again
  }
   else if (arrUser[i] == arrSimon[i]) {   // if choice == the element under consideration
          i++;
          if (i>=arrSimon.length) {
              i = 0;
              setTimeout(simonSays,1000);
          }
     }
}                                         

function stricToggle(){
  if (strict==0 && power==1) {
      document.getElementById("strict").className = "strictOn";
      strict = 1;
  }
  else {
      document.getElementById("strict").className = "strictOff";
      strict = 0;
  }
}

function toggle(){
  $(".powerOff").toggleClass("powerOn");
  if (power==0) {
    power = 1;
    document.getElementById("countText").className = "countTextOn";
    reset();
    status();
    document.getElementById("countText").textContent = "--";
  }
  else if (power==1) {
      power = 0;
      initText();
  }
}

function startGame(){ // resets to a new game
  if (power==1) {
    count = 0;    
    document.getElementById("countText").textContent = count;
    setTimeout(function() {simonSays()}, 750);
  }
}

function status(){
  document.getElementById("simonArray").textContent = arrSimon;
  document.getElementById("userArray").textContent = arrUser;
  document.getElementById("i").textContent = i;
  document.getElementById("j").textContent = j;
}

function reset(){
    count = 0;
    arrSimon = [];
    arrUser = [];
    i=0; j=0;
}

function bright(choice){
 switch (choice) {
      case 1: document.getElementById('red').className="on"; play1();
     break;
      case 2: document.getElementById('blue').className="on"; play2();
     break;
      case 3: document.getElementById('yellow').className="on"; play3();
     break;
      case 4: document.getElementById('green').className="on"; play4();
     break;
             }                   
  setTimeout(function() {dim()},500); // turn off after a half second
}

// turn off the color
function dim(){
  document.getElementById("green").className = "off";
   document.getElementById("red").className = "off";
   document.getElementById("blue").className = "off";
   document.getElementById("yellow").className = "off";
}

function initText(){
      document.getElementById("countText").className = "countTextOff";
      document.getElementById("strict").className = "strictOff";
      document.getElementById("countText").textContent = "--";
}

// play the sounds
function play1(){ document.getElementById("play1").play(); }
function play2(){ document.getElementById("play2").play(); }
function play3(){ document.getElementById("play3").play(); } 
function play4(){ document.getElementById("play4").play(); }
            
          
!
999px
Loading ..................

Console