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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

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

              
                <!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Build a Simon Game</title>
  <link href="https://fonts.googleapis.com/css?family=Orbitron|Passion+One|Quicksand" rel="stylesheet">
  <link rel="stylesheet" href="reset.css">
  <link rel="stylesheet" href="style.css">
</head>

<body>
  <div id="gameboard">
  <button class="colorbtn" id="green" name="green" value="1" disabled=true onclick="playSound(this.value); userColorInput(this.name)">
  </button>
  <button class="colorbtn" id="red" name="red" value="2" disabled=true onclick="playSound(this.value); userColorInput(this.name)">
  </button>
  </br>
  <button class="colorbtn" id="yellow" name="yellow" value="4" disabled=true onclick="playSound(this.value); userColorInput(this.name)">
  </button>
    <button class="colorbtn" id="blue" name="blue" value="3" disabled=true onclick="playSound(this.value); userColorInput(this.name)">
  </button>
  <div id="center">
    <h1 id="simon">simon</h1>
  <button class="functionbtn" id="start" name="start" value="start" onclick="startGame()">
  </button>
  <button class="functionbtn" id="reset" name="reset" value="reset" onclick="resetGame()">
  </button>
  <button class="functionbtn" id="strict" name="strict" value="strict" onclick="toggleStrict()">
  </button>
    <p class="label" id="startlabel">START</p>
    <p class="label" id="resetlabel">RESET</p>
    <p class="label" id="strictlabel">STRICT</p>
  <div id="level">0</div>
  </div>
  </div>
  <script src="script.js"></script>
</body>

</html>
              
            
!

CSS

              
                body {
  background: #8e9eab;  /* fallback for old browsers */
  background: -webkit-linear-gradient(to left, #eef2f3, #8e9eab);  /* Chrome 10-25, Safari 5.1-6 */
  background: linear-gradient(to left, #eef2f3, #8e9eab); /* W3C, IE 10+/ Edge, Firefox 16+, Chrome 26+, Opera 12+, Safari 7+ */
}

#gameboard {
  background-color: #000;
  position: relative;
  height: 500px;
  width: 500px;
  border-radius: 100%;
  margin: auto;
  margin-top: 10px;
  -webkit-user-select: none;
  -moz-user-select: none;
  user-select: none;
}

.colorbtn {
  height: 200px;
  width: 200px;
  outline: none;
  border: none;
}

.functionbtn {
  height: 50px;
  width: 50px;
  outline: none;
  border-radius: 100%;
}

#green {
  background: green;
  border-radius: 100% 0 0 0;
  position: absolute;
  top: 35px;
  left: 40px;
}

#red {
  background-color: red;
  border-radius: 0 100% 0 0;
  position: absolute;
  top: 35px;
  left: 260px;
}

#blue {
  background-color: blue;
  border-radius: 0 0 100% 0;
  position: absolute;
  top: 260px;
  left: 260px;
}

#yellow {
  background-color: yellow;
  border-radius: 0 0 0 100%;
  position: absolute;
  top: 260px;
  left: 40px;
}

#center {
  height: 225px;
  width: 225px;
  background-color: black;
  border-radius: 100%;
  position: absolute;
  top: 135px;
  left: 135px;
  z-index: 1;
}

#simon {
  font-family: 'Passion One', cursive;
  font-size: 70px;
  color: white;
  position: absolute;
  top: 20px;
  left: 35px;
}

#start {
  background-color: #440000;
  position: absolute;
  top: 90px;
  left: 30px;
}

#reset {
  background-color: black;
  position: absolute;
  top: 90px;
  left: 90px;
}

#strict {
  background-color: white;
  position: absolute;
  top: 90px;
  left: 150px;
}

.label {
  font-family: 'Quicksand', sans-serif;
  font-size: 14px;
  color: white;
  position: absolute;
  top: 145px;
}

#startlabel {
  left: 35px;
}

#resetlabel {
  left: 95px;
}

#strictlabel {
  left: 153px;
}

#level {
  display:inline-block;
  height: 30px;
  width: 100px;
  line-height: 30px;
  font-family: 'Orbitron', sans-serif;
  font-size: 25px;
  text-align: center;
  color: red;
  background-color: black;
  border: 1px solid #fff;
  position: absolute;
  top: 170px;
  left: 65px;
}
              
            
!

JS

              
                /* Build a Simon Game

User Story: I am presented with a random series of button presses.

User Story: Each time I input a series of button presses correctly, I see the same series of button presses but with an additional step.

User Story: I hear a sound that corresponds to each button both when the series of button presses plays, and when I personally press a button.

User Story: If I press the wrong button, I am notified that I have done so, and that series of button presses starts again to remind me of the pattern so I can try again.

User Story: I can see how many steps are in the current series of button presses.

User Story: If I want to restart, I can hit a button to do so, and the game will return to a single step.

User Story: I can play in strict mode where if I get a button press wrong, it notifies me that I have done so, and the game restarts at a new random series of button presses.

User Story: I can win the game by getting a series of 20 steps correct. I am notified of my victory, then the game starts over.

Hint: Here are mp3s you can use for each button: https://s3.amazonaws.com/freecodecamp/simonSound1.mp3, https://s3.amazonaws.com/freecodecamp/simonSound2.mp3, https://s3.amazonaws.com/freecodecamp/simonSound3.mp3, https://s3.amazonaws.com/freecodecamp/simonSound4.mp3.
*/

/* Things to add:
1. Disable user click during button animation.
*/

/* Wish List:
1. Add game start and win/loss sounds.
2. Add game speed toggle.
*/

/* Variables to hold the color pool, computer's color pattern, user color pattern, start button flag, strict button flag, and level number */
const colorPool = ["green", "red", "blue", "yellow"];
let cpuPattern = [];
let userPattern = [];
let start = false;
let strict = false;
let level = 0;
const startElement = document.getElementById("start");
const strictElement = document.getElementById("strict");
const levelElement = document.getElementById("level");

/* Start button checks to see if the game isn't already running then starts the game, animates the button with startButtonGlow(), turns on the colored buttons with enableButtons(), and starts adding colors to the CPU pattern with addColorToCpuPattern() */
const startGame = () => {
  if (start === false) {
    start = true;
    startElement.disabled = true;
    startButtonGlow();
    enableButtons();
    addColorToCpuPattern();
  }
}

/* Pushes a random color to the end of the cpuPattern array with createRandomColor() and increments the level counter, then starts animating buttons with animateAllButtons() */
const addColorToCpuPattern = () => {
  cpuPattern.push(createRandomColor());
  level++;
  levelElement.innerHTML = level;
  animateAllButtons();
}

/* Generates a random number between 0-3 and reates an array of colors corresponding to the random numbers, plays the corresponding sound, then returns a random color */
const createRandomColor = () => {
  var randomNumber = Math.floor(Math.random() * 4);
  return colorPool[randomNumber];
}

/* Animates all buttons by looping through the cpuPattern array and animating one button at a time */
const animateAllButtons = () => {
  /* No idea why let i = 0 works here, but var i = 0 doesn't do anything */
  for (let i = 0; i < cpuPattern.length; i++) {
    setTimeout(function() {
      setTimeout(function() {
        animateOneButton(cpuPattern[i]);
      }, 500 * i);
    }, 500 + (500 * i));
  }
}

/* Animates the current button to white for 1 second, then back to its original color */
const animateOneButton = (color) => {
  playSound(colorPool.indexOf(color)+1);
  document.getElementById(color).style.backgroundColor = "white";
  document.getElementById(color).style.boxShadow = "0 0 15px white";
  setTimeout(function() {
    document.getElementById(color).style.backgroundColor = color;
    document.getElementById(color).style.boxShadow = "none";
  }, 500);
}

/* Takes user input and pushes the color to the end of the userPattern array then runs patternMatchTest() function to test vs the cpu */
const userColorInput = (color) => {
  userPattern.push(color);
  animateOneButton(color);
  patternMatchTest();
}

/* Checks if the user input (pattern) matches the cpu output (pattern), if true the level number is incremented (game is won at level 20), and a new color is added to the cpu pattern, if false the game is reset */
const patternMatchTest = () => {
  for (var i = 0; i < userPattern.length; i++) {
    if (userPattern[i] !== cpuPattern[i]) {
      if (checkStrictMode()) {
        lostGame();
        return;
      }
      else {
        userPattern = [];
        animateAllButtons();
      }
    }
  }
  /* If the user's pattern is the same length as the cpu pattern, check if level 20 is completed (ends in a win), otherwise clear the user pattern and add another color to the cpu pattern */
  if (userPattern.length === cpuPattern.length) {
    if (level === 20) {
      wonGame();
      return;
    }
    userPattern = [];
    addColorToCpuPattern();
  }
}

/* If strict mode is on and the wrong button is pressed, returns true and the game ends in a loss. If strict mode is off and the wrong button is pressed, returns false and the cpu pattern is repeated */
const checkStrictMode = () => {
  if (strict === true) {
    return true;
  }
  else {
    return false;
  }
} 

/* Plays sounds on colored button presses */
const playSound = (number) => {
  var sound = new Audio(
    "https://s3.amazonaws.com/freecodecamp/simonSound" + number + ".mp3"
  );
  sound.play();
}

/* Resets the game by clearing both pattern arrays, setting start to false, resetting the level counter to 0, and resetting the button defaults */
const resetGame = () => {
  cpuPattern = [];
  userPattern = [];
  start = false;
  strict = false;
  level = 0;
  buttonDefaults();
}

/* Start button glow settings */
const startButtonGlow = () => {
    startElement.style.backgroundColor = "red";
    startElement.style.boxShadow = "0 0 30px red";
    levelElement.innerHTML = level;
}

/* Strict button glow settings */
const strictButtonGlow = () => {
    strictElement.style.backgroundColor = "white";
    strictElement.style.boxShadow = "0 0 30px white";
}

/* Disables the colored buttons */
const disableButtons = () => {
  colorPool.forEach(function(color) {
    document.getElementById(color).style.backgroundColor = color;
    document.getElementById(color).style.boxShadow = "none";
    document.getElementById(color).disabled = true;
  });
}

/* Enables the colored buttons */
const enableButtons = () => {
  colorPool.forEach(function(color) {
    document.getElementById(color).disabled = false;
  });
  strictElement.disabled = true;
}

/* Button default settings */
const buttonDefaults = () => {
  disableButtons();
  startElement.disabled = false;
  startElement.style.backgroundColor = "#440000";
  startElement.style.boxShadow = "none";
  strictElement.disabled = false;
  strictElement.style.backgroundColor = "white";
  strictElement.style.boxShadow = "none";
  levelElement.innerHTML = level;
}

/* Toggles the strict button on and off */
const toggleStrict = () => {
  if (strict === false) {
    strict = true;
    strictButtonGlow();
  } else {
    strict = false;
    strictElement.style.boxShadow = "none";
  }
}

/* Won the game, disables all buttons until reset button is pushed */
const wonGame = () => {
  levelElement.innerHTML = "WIN!";
  colorPool.forEach(function(color) {
    animateOneButton(color);
    document.getElementById(color).disabled = true;
  });
}

/* Lost the game, disables all buttons until reset button is pushed */
const lostGame = () => {
  levelElement.innerHTML = "LOSE!";
  colorPool.forEach(function(color) {
    document.getElementById(color).disabled = true;
  });
}
              
            
!
999px

Console