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.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <h1>Simon Game</h1>
<div id="circle-container">
  <div class="quarter" id="top-left" onclick="quarterClicked(this.id)"></div>
  <div class="quarter" id="top-right" onclick="quarterClicked(this.id)"></div>
  <div class="quarter" id="bottom-left" onclick="quarterClicked(this.id)"></div>
  <div class="quarter" id="bottom-right" onclick="quarterClicked(this.id)"></div>
  <div id="inner">
    <div id="button-container">
      <button class="round-button" id="go" onclick="goButtonClicked()" title="Click to start or restart the game">GO!</button>
      <div id="display">--</div>
    </div>
  </div>
</div>

            
          
!
            
              body {
  font-family: Arial, sans-serif;
  background-color: black;
  vertical-align: middle;
}

h1 {
  text-align: center;
  line-height: 0.8em;
  color: white;
  margin: 3em;
}

.glow {
  box-shadow: 0 0 8px rgba(255, 255, 255, 0.8);
  transition: box-shadow linear 0.05s;
}

#circle-container {
  background-color: black;
  position: relative;
  top: -30px;
  margin-left: auto;
  margin-right: auto;
  width: 300px;
  height: 300px;
  border-radius: 50%;
  border: 2px solid black;
  /* box-shadow: 2px 4px 1px #888888; */
}

.quarter {
  width: 148px;
  height: 148px;
  border: 1px solid black;
  cursor: default;
}

#top-left {
  border-top-left-radius: 50px;
  background: darkgreen;
  float: left;
}

#top-right {
  border-top-right-radius: 50px;
  background: darkred;
  float: right;
}

#bottom-left {
  border-bottom-left-radius: 50px;
  background: goldenrod;
  float: left;
}

#bottom-right {
  border-bottom-right-radius: 50px;
  background: midnightblue;
  float: right;
}

#inner {
  position: relative;
  height: 160px;
  width: 160px;
  border-radius: 50%;
  left: 70px;
  top: 70px;
  background-color: #101010;
  border: 1px solid black;
  color: antiquewhite;
}

#display {
  border: 1px solid #f5f5f5;
  border-radius: 4px;
  width: 30px;
  height: 24px;
  margin: 0 auto;
  font-family: VT323, monospace;
  font-size: 22px;
  font-weight: bold;
  color: red;
  background-color: RGB(50, 5, 12);
  text-shadow: 1px 1px 2px #F00;
  position: relative;
  top: 10px;
}

#button-container {
  position: relative;
  top: -250px;
  left: 0;
  height: 160px;
  width: 160px;
  border-radius: 50%;
  color: antiquewhite;
  text-align: center;
}

.round-button {
  cursor: pointer;
  width: 60px;
  height: 60px;
  line-height: 40px;
  border: 1px solid #f5f5f5;
  border-radius: 50%;
  color: #f5f5f5;
  text-align: center;
  text-decoration: none;
  background: blue;
  box-shadow: 0 0 3px gray;
  font-size: 15px;
  font-weight: bold;
}

.round-button:hover {
  background: skyblue;
}

.onoffswitch {
  position: relative;
  margin: 0 auto;
  width: 73px;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
}

.onoffswitch-checkbox {
  display: none;
}

.onoffswitch-label {
  display: block;
  overflow: hidden;
  cursor: pointer;
  border: 2px solid #F5F5F5;
  border-radius: 18px;
}

.onoffswitch-inner {
  display: block;
  width: 200%;
  margin-left: -100%;
  transition: margin 0.3s ease-in 0s;
}

.onoffswitch-inner:before,
.onoffswitch-inner:after {
  display: block;
  float: left;
  width: 50%;
  height: 20px;
  padding: 0;
  line-height: 20px;
  font-size: 10px;
  color: white;
  font-family: Trebuchet, Arial, sans-serif;
  font-weight: bold;
  box-sizing: border-box;
}

.onoffswitch-inner:before {
  content: "STRICT";
  padding-left: 10px;
  background-color: #6495ED;
  color: #FFFFFF;
}

.onoffswitch-inner:after {
  content: "SLACK";
  padding-right: 10px;
  background-color: #EEEEEE;
  color: #999999;
  text-align: right;
}

.onoffswitch-switch {
  display: block;
  width: 18px;
  margin: 1px;
  background: #FFFFFF;
  position: absolute;
  top: 0;
  bottom: 0;
  right: 49px;
  border: 2px solid #F5F5F5;
  border-radius: 18px;
  transition: all 0.3s ease-in 0s;
}

.onoffswitch-checkbox:checked + .onoffswitch-label .onoffswitch-inner {
  margin-left: 0;
}

.onoffswitch-checkbox:checked + .onoffswitch-label .onoffswitch-switch {
  right: 0;
}
            
          
!
            
              "use strict";

/***********************************************************************
 GLOBAL VARIABLES
***********************************************************************/
var quarters = ["top-left", "top-right", "bottom-left", "bottom-right"],
  context = new AudioContext(),
  repetitions = 0, // Number of intended repetitions
  computerList = [], // This array is filled with random values later on
  playerList = [], // What the player actually clicked
  user = 0, // The variables user and computer are just intuitive aliases for the numbers
  computer = 1,
  activePlayer = computer,
  loopCounter = 0,
  clickCounter = 0,
  computerLoopTimer = null,
  maxRuns = 20, // Player wins after maxRuns
  strictMode = false,
  looped = false;

/***********************************************************************
 HELPER FUNCTIONS
***********************************************************************/
function zeroPad(n) {
  if (n < 10) {
    return "0" + n;
  } else {
    return n;
  }
}

function getRandomInt(min, max) {
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

function playSound(frequency) {
  var oscillator = context.createOscillator();

  oscillator.type = "sine";
  oscillator.frequency.value = frequency;
  oscillator.connect(context.destination);
  oscillator.start(context.currentTime);
  oscillator.stop(context.currentTime + 0.45);
}

function setBackgroundColor(id, color) {
  document.getElementById(id).style.backgroundColor = color;
}

function removeClass(id, cssClass) {
  var str = " " + cssClass + "$",
    re = new RegExp(str, "g");
  document.getElementById(id).className = document.getElementById(id).className.replace(re, '');
}

function setCursorForQuarters(cursor) {
  var index = 0,
    id = "";
  for (index in quarters) {
    id = quarters[index];
    document.getElementById(id).style.cursor = cursor;
  }
}

/***********************************************************************
 CORE GAME FUNCTIONS
***********************************************************************/

// This function makes an individual quarter (identified by the id parameter) 
// glow and plays its corresponding sound
function flashQuarter(id) {
  document.getElementById(id).className += " glow";
  if (id === "top-left") {
    setBackgroundColor(id, "lawngreen");
    playSound(500);
    setTimeout(setBackgroundColor, 450, id, "darkgreen");
  } else if (id === "top-right") {
    document.getElementById(id).style.backgroundColor = "tomato";
    playSound(450);
    setTimeout(setBackgroundColor, 450, id, "darkred");
  } else if (id === "bottom-left") {
    document.getElementById(id).style.backgroundColor = "#ffff66";
    playSound(400);
    setTimeout(setBackgroundColor, 450, id, "goldenrod");
  } else if (id === "bottom-right") {
    document.getElementById(id).style.backgroundColor = "deepskyblue";
    playSound(350);
    setTimeout(setBackgroundColor, 450, id, "midnightblue");
  }
  setTimeout(removeClass, 450, id, "glow");
}

// This function flashes all quarters of the circle twice when the page loads
function flashAll() {
  var index = 0,
    id = "";
  playSound(500);
  setBackgroundColor("go", "deepskyblue");
  setTimeout(setBackgroundColor, 700, "go", "blue");
  for (index in quarters) {
    id = quarters[index];
    if (id === "top-left") {
      document.getElementById(id).className += " glow";
      setBackgroundColor(id, "lawngreen");
      setTimeout(setBackgroundColor, 700, id, "darkgreen");
      setTimeout(removeClass, 700, id, "glow");
      continue; // we found the right one, no need to test the other cases
    } else if (id === "top-right") {
      document.getElementById(id).className += " glow";
      setBackgroundColor(id, "tomato");
      setTimeout(setBackgroundColor, 700, id, "darkred");
      setTimeout(removeClass, 700, id, "glow");
      continue;
    } else if (id === "bottom-left") {
      document.getElementById(id).className += " glow";
      setBackgroundColor(id, "#ffff66");
      setTimeout(setBackgroundColor, 700, id, "goldenrod");
      setTimeout(removeClass, 700, id, "glow");
      continue;
    } else if (id === "bottom-right") {
      document.getElementById(id).className += " glow";
      setBackgroundColor(id, "deepskyblue");
      setTimeout(setBackgroundColor, 700, id, "midnightblue");
      setTimeout(removeClass, 700, id, "glow");
    }
  }
  // Play it a second time after 1000 milliseconds:
  if (!looped) {
    setTimeout(flashAll, 1000);
  }
  looped = true;
  activePlayer = user;
}

// Called from computerMoves; flashes the quarters stored in computerList
function loopQuarters() {
  flashQuarter(quarters[computerList[loopCounter]]);
  loopCounter++;

  // Clear interval after given number of repetitions
  if (loopCounter >= repetitions) {
    clearInterval(computerLoopTimer);
    loopCounter = 0;
    return;
  }
}

function computerMoves() {
  activePlayer = computer;
  loopCounter = 0;
  computerLoopTimer = setInterval(loopQuarters, 750);
  repetitions++;
  document.getElementById("display").innerHTML = zeroPad(repetitions).toString();
  activePlayer = user;
  setCursorForQuarters("pointer");
}

function initializeGame() {
  repetitions = 0;
  playerList = [];
  activePlayer = computer;
  loopCounter = 0;
  clickCounter = 0;
  document.getElementById("display").innerHTML = "--";
}

// Fill the computerList array with maxRuns (20) random integers from 0 to 3
function initializeComputerList() {
  computerList = [];
  for (var i = 0; i < maxRuns; i++) {
    computerList.push(getRandomInt(0, 3));
  }
}

function goButtonClicked() {
  // If it's not the user's turn, they have no business pressing the go button
  if (activePlayer === user) {
    // Ask for confirmation if a game is already running
    if (repetitions > 0) {
      var answer = confirm("Do you want to start over?");
      if (answer === true) {
        initializeGame();
        initializeComputerList();
        computerMoves();
      } else {
        return;
      }
    } else {
      initializeComputerList();
      initializeGame();
      // Set button to darker color
      document.getElementById("go").style.backgroundColor = "CornflowerBlue";
      computerMoves();
    }
  }
}

// This function is called whenever the player clicks one of the quarters
function quarterClicked(id) {
  // return early if it's not the player's turn
  if (activePlayer !== user) {
    // console.log("returning");
    return;
  }
  flashQuarter(id);
  clickCounter++;
  playerList.push(id);
  // Player made a mistake
  if (id !== quarters[computerList[clickCounter - 1]]) {
    document.getElementById("display").innerHTML = "!!";
    playSound(200);
    if (strictMode === true) {
      initializeComputerList();
      setTimeout(initializeGame, 500);
      setTimeout(computerMoves, 900);
      return;
    }
    // repeat the old sequence if the user made a mistake
    repetitions--; // repetitions is incremented in computerMoves.
    activePlayer = computer;
    setTimeout(setCursorForQuarters, 500, "default");
    setTimeout(computerMoves, 1000);
    clickCounter = 0;
    return; // return early so the following isn't executed
  }
  // Player wins
  if (clickCounter == maxRuns) {
    document.getElementById("display").innerHTML = "WIN";
    looped = false;
    setTimeout(flashAll, 800);
    setTimeout(initializeGame, 4500);
    initializeComputerList();
    setTimeout(computerMoves, 7000);
    return;
  }
  // No mistake. Next round with one more flashing quarter.
  if (clickCounter >= repetitions) {
    activePlayer = computer;
    setTimeout(setCursorForQuarters, 500, "default");
    setTimeout(computerMoves, 1000);
    clickCounter = 0;
  }
}



window.onload = flashAll();
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console