Pen Settings

HTML

CSS

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

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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

Behavior

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <h1>Free Code Camp Simon App</h1>
<svg>
  <path class="outer arc1default" id="arc1"/>
  <path class="outer" id="arc2"/>
  <path class="outer" id="arc3"/>
  <path class="outer" id="arc4"/>
  <circle id="center" cx="300" cy="275" r="160" />
  <text class="nameText" x="240" y="200">Simon</text>
  <rect x="180" y="220" fill="black" width="80" height="50" />
  <text id="countText" x="200" y="260" font-family="Verdana" font-size="35" fill="red">00</text>
  <text class="smallText" x="185" y="300">Count</text>
  <circle id="startButton" cx="300" cy="260" r="15" fill="red" stroke="black" stroke-width="5" />
  <text class="smallText" x="272" y="300">Start</text>
  <circle id="strictLight" cx="380" cy="225" r="5" fill="red" stroke="black" stroke-width="1" />
  <circle id="strictButton" cx="380" cy="260" r="15" fill="yellow" stroke="black" stroke-width="5" />
  <text class="smallText" x="350" y="300">Strict</text>
  <rect id="offButton" x="265" y="350" fill="lightblue" width="30" height="30" stroke="black" />
  <rect id="onButton" x="295" y="350" fill="blue" width="30" height="30" stroke="black" />
  <text class="smallText" x="225" y="372">Off</text>
  <text class="smallText" x="332" y="372">On</text>
</svg>

              
            
!

CSS

              
                body {
  text-align: center;
  background: lightgray;
}

svg {
	width  : 600px;
	height : 600px;
	border : 1px dashed lightblue; //DEBUG
}

.outer {
  stroke: black;
  stroke-width: 10;
}

#arc1 {
  
}

#arc2 {
  fill: blue;
}

#arc3 {
  fill: yellow;
}

#arc4 {
  fill: green;
}

.arc1default {
  fill: red;
}

.arc1flash {
  fill: pink;
}

.arc2flash {
  fill: lightblue;
}

.arc3flash {
  fill: lightyellow;
}

.arc4flash {
  fill: lightgreen;
}

#center {
  fill: white;
  stroke: black;
  stroke-width: 10;
}

.nameText {
  fill: black;
  font-size: 35px;
  font-family: Arial Black;
}

.smallText {
  fill: black;
  font-size: 20px;
  font-family: Arial Black;
}



              
            
!

JS

              
                (function() {
  var idList = ["arc1", "arc2", "arc3", "arc4"];
  var moveList = []; 
  var playerSuccess = false;
  var index = 0;
  var failureTimer; //timeout for the user's attempt window, if it ends with playerSuccess == false it repeats the sequence
  var failureProgress = false; //Determines if the timeout for user input is in progress
  var sequence; //Interval for the AI sequence to iterate
  var sequenceProgress = false;
  var strictOn = false;
  var arcDisabled = false;
  var startDisabled = false;
  var strictDisabled = false;
  var buttonSound1 = new Audio("https://s3.amazonaws.com/freecodecamp/simonSound1.mp3");
  var buttonSound2 = new Audio("https://s3.amazonaws.com/freecodecamp/simonSound2.mp3");
  var buttonSound3 = new Audio("https://s3.amazonaws.com/freecodecamp/simonSound3.mp3");
  var buttonSound4 = new Audio("https://s3.amazonaws.com/freecodecamp/simonSound4.mp3");
  var errorSound = new Audio("http://soundbible.com/1198-Buzz-Fade-Out.html");
  var buttonDict = {
    "arc1" : {
      "sound" : buttonSound1,
      "flash" : "pink",
      "color" : "red"
    },
    "arc2" : {
      "sound" : buttonSound2,
      "flash" : "lightblue",
      "color" : "blue"
    },  
    "arc3" : {
      "sound" : buttonSound3,
      "flash" : "lightyellow",
      "color" : "yellow"
    },
    "arc4" : {
      "sound" : buttonSound4,
      "flash" : "lightgreen",
      "color" : "green"
    }
  };
  
  
  function polarToCartesian(centerX, centerY, radius, angleInDegrees) {
    var angleInRadians = (angleInDegrees-90) * Math.PI / 180.0;
    return {
      x: centerX + (radius * Math.cos(angleInRadians)),
      y: centerY + (radius * Math.sin(angleInRadians))
    };
  }

  function describeArc(x, y, radius, startAngle, endAngle){

      var start = polarToCartesian(x, y, radius, endAngle);
      var end = polarToCartesian(x, y, radius, startAngle);

      var largeArcFlag = endAngle - startAngle <= 180 ? "0" : "1";

      var d = [
          "M", start.x, start.y, 
          "A", radius, radius, 0, largeArcFlag, 0, end.x, end.y,
          "L", x, y,
          "L", start.x, start.y
      ].join(" ");

      return d;       
  }

  $('#arc1').attr('d', describeArc(300, 275, 250, 0, 90));
  $('#arc2').attr('d', describeArc(300, 275, 250, 90, 180));
  $('#arc3').attr('d', describeArc(300, 275, 250, 180, 270));
  $('#arc4').attr('d', describeArc(300, 275, 250, 270, 0));     

  function buttonFlash(button) {
    console.log("button is: " + button);
    //$("#" + button).attr("class", button + "flash outer");
    //$("#" + button).removeClass(button + "default");
    $("#" + button).addClass(button + "flash");
    //$("#" + button).css({ fill: buttonDict[button].flash });
    buttonDict[button].sound.play();
    setTimeout(function() {
      $("#" + button).attr("class", button + "default outer");
      //$("#" + button).css({ fill: buttonDict[button].color });
      buttonDict[button].sound.pause();
      buttonDict[button].sound.currentTime = 0;
    }, 300);
  }
  
  $("#arc1, #arc2, #arc3, #arc4").on("click", function() {
    if($("#arc1, #arc2, #arc3, #arc4").attr("disabled") !== "disabled" || arcDisabled === false) {
      if (playerSuccess === false) {
        console.log("Index is currently: " + index);
        buttonFlash(this.id);
        if(this.id === moveList[index]) {
          index++;
          if(index >= moveList.length) {
            playerSuccess = true;
            clearInterval(failureTimer);
            failureProgress = false;
            if(moveList.length < 20) {
              setTimeout(function() {
                buttonSequence(false);
              }, 100);
            }
            else {
              var i = 1;
              var victory = setInterval(function() {
                buttonFlash("arc" + i);
                i++;
                if(i > 4) {
                  clearInterval(victory);
                  resetGame();
                }
              }, 300);
            }
          }
        }
        else if(failureProgress === true) {
          console.log("entering if statement");
          disableButtons();
          setTimeout(function() {
            buttonSound4.play();
          }, 300);

          setTimeout(function() {
            buttonSound4.pause();
            buttonSound4.currentTime = 0;
            clearInterval(failureTimer);
            failureProgress = false;
            enableArcButtons();
            errorFlash();
          }, 600);
        }
      }
    }
  });
  
  function buttonSequence(repeat) {
    disableButtons();
    playerSuccess = false;
    failureProgress = false;
    var count = 0;
    if(repeat == false) {
      var random = Math.floor(Math.random() * 4);
      moveList.push(idList[random]);
      var total = moveList.length;
      if(total.toString().length == 1) {
        $("#countText").text("0" + total);
      }
      else {
        $("#countText").text(total)
      }
    }
    
    sequence = setInterval(function() {
      console.log("count is: " + count);
      console.log("total is: " + moveList.length);
      if(count < moveList.length) {
        console.log("moveList is: " + moveList[count]);
        buttonFlash(moveList[count]);
        count++;
      }
      else {
        clearInterval(sequence);
        enableArcButtons();
        count = 0;
        index = 0;
      }
    }, 1000);
    
    var delay = 3000 * moveList.length;
    failureProgress = true;
    
    failureTimer = setTimeout(function() {
      if(playerSuccess == false) {
        errorFlash();
      }
      else if(moveList.length <= 20){
        buttonSequence(false);
      }
    }, delay);
    console.log(moveList);
  }
  
  function errorFlash() {
    buttonFlash("arc1");
    buttonFlash("arc2");
    buttonFlash("arc3");
    buttonFlash("arc4");
    if(strictOn === false) {
      buttonSequence(true);
    }
    else {
      resetGame();
    }
  }
  
  function resetGame() {
    failureProgress = false;
    playerSuccess = false;
    index = 0;
    moveList = [];
    $("#countText").text("00");
    enableStartButton();
    enableStrictButton();
  }
  
  function disableButtons() {
    $("#arc1, #arc2, #arc3, #arc4").attr("disabled", "disabled");
    $("#startButton").attr("disabled", "disabled");
    $("#strictButton").attr("disabled", "disabled");
    arcDisabled = true;
    startDisabled = true;
    strictDisabled = true;
  }
  
  function enableArcButtons() {
    $("#arc1, #arc2, #arc3, #arc4").removeAttr("disabled");
    arcDisabled = false;
  }
  
  function enableStartButton() {
    $("#startButton").removeAttr("disabled");
    startDisabled = false;
  }
  
  function enableStrictButton() {
    $("#strictButton").removeAttr("disabled");
    strictDisabled = false;
  }

  function runGame() {
    if($("#startButton").attr("disabled") !== "disabled" || startDisabled === false) {
      buttonSequence(false);
    }
  }
  
  $("#offButton").on("click", function() {
    clearInterval(failureTimer);
    clearInterval(sequence);
    resetGame();
    disableButtons();
    $("#offButton").css({ fill: "blue" });
    $("#onButton").css({ fill: "lightblue" });
  });
  
  $("#onButton").on("click", function() {
    enableArcButtons();
    enableStartButton();
    enableStrictButton();
    $("#offButton").css({ fill: "lightblue" });
    $("#onButton").css({ fill: "blue" });
  });

  $("#startButton").on("click", runGame);
  
  $("#strictButton").on("click", function() {
    console.log(strictDisabled);
    if(strictDisabled === false) {
      if(strictOn === false) {
        $("#strictLight").css({ fill: "green" });
        strictOn = true;
      }
      else {
        $("#strictLight").css({ fill: "red" });
        strictOn = false;
      }  
    }
  });
}());
  
              
            
!
999px

Console