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

              
                <div class="square green" id="green"></div>
<div class="square red" id="red"></div>
<div class="square yellow" id="yellow"></div>
<div class="square blue" id="blue"></div>

<button class="start-button" id="start-game">Start Game</button>
              
            
!

CSS

              
                .square {
  height: 100px;
  width: 100px;
  border: 1px solid black;
  position: absolute;
}

.green {
  background-color: green;
  top: 15px;
  left: 15px;
}

.red {
  background-color: red;
  top: 15px;
  left: 125px;
}

.yellow {
  background-color: yellow;
  top: 125px;
  left: 15px;
}

.blue {
  background-color: blue;
  top: 125px;
  left: 125px;
}

.start-button {
  position: absolute;
  top: 235px;
  left: 15px;
}

.dark {
  background-color: black;
}
              
            
!

JS

              
                // set global variables
var computerChoice = []; // holds the computer selected colors
var humanChoice = []; // holds the human selected colors
var computerPlaying = false; // determine whether or not the computer is playing
var color = "";
var turnCount = 0; // number of turns that have happened
var internalCount = 0; // number of human clicks that have happened

// 1: Start the game
$("#start-game").on("click", function() {
  computerPlaying = true;
  playTheGame();
});

// 2: Generate a random number that becomes a color
function getColor() {
  var randomNumber = Math.floor((Math.random() * 4) + 1);

  if (randomNumber === 1) {
    color = "green";
    //$("#green").addClass("dark");
    //setTimeout(function() {
    //    $("#green").removeClass("dark");
    //}, 1000);
  } else if (randomNumber === 2) {
    color = "red";
    //$("#red").addClass("dark");
    //setTimeout(function() {
    //    $("#red").removeClass("dark");
    //}, 1000);
  } else if (randomNumber === 3) {
    color = "yellow";
    //$("#yellow").addClass("dark");
    //setTimeout(function() {
    //    $("#yellow").removeClass("dark");
    //}, 1000);
  } else if (randomNumber === 4) {
    color = "blue";
    //$("#blue").addClass("dark");
    //setTimeout(function() {
    //    $("#blue").removeClass("dark");
    //}, 1000);
  } else {
    alert('an error occured while creating a color');
  }
  return color;
}

function replayColors() {
  for (var i = 0; i < computerChoice.length; i++) {
    if (computerChoice[i] === "green") {
      setTimeout(function() {
        $("#green").addClass("dark");
      }, 500);
      //$("#green").addClass("dark");
      setTimeout(function() {
        $("#green").removeClass("dark");
      }, 1000);
    } else if (computerChoice[i] === "red") {
      setTimeout(function() {
        $("#red").addClass("dark");
      }, 500);
      //$("#red").addClass("dark");
      setTimeout(function() {
        $("#red").removeClass("dark");
      }, 1000);
    } else if (computerChoice[i] === "yellow") {
      setTimeout(function() {
        $("#yellow").addClass("dark");
      }, 500);
      //$("#yellow").addClass("dark");
      setTimeout(function() {
        $("#yellow").removeClass("dark");
      }, 1000);
    } else if (computerChoice[i] === "blue") {
      setTimeout(function() {
          $("#blue").addClass("dark");
        })
        //$("#blue").addClass("dark");
      setTimeout(function() {
        $("#blue").removeClass("dark");
      }, 1000)
    }
  }
}

// 3: Play the Game
function playTheGame() {
  while (computerPlaying) {
    getColor();
    computerChoice.push(color);
    replayColors();
    computerPlaying = false;
    turnCount++;
    internalCount = 0;
    humanChoice = [];

    if (turnCount === 21) {
      alert("Game Over");
    }
  }
}

// 4: Accept human choice input
$("#green").on("click", function() {
  humanChoice.push("green");
  internalCount++;
  checkAnswer();
});

$("#red").on("click", function() {
  humanChoice.push("red");
  internalCount++;
  checkAnswer();
});

$("#yellow").on("click", function() {
  humanChoice.push("yellow");
  internalCount++;
  checkAnswer();
});

$("#blue").on("click", function() {
  humanChoice.push("blue");
  internalCount++;
  checkAnswer();
});

// 5: Compare human vs computer
function checkAnswer() {
  if (turnCount === 1) {
    if (humanChoice[0] === computerChoice[0]) {
      computerPlaying = true;
      playTheGame();
    } else if (humanChoice[0] !== computerChoice[0]) {
      alert("You Lose");
    }
  } else if (internalCount < turnCount) {
    if (humanChoice[internalCount - 1] !== computerChoice[internalCount - 1]) {
      alert("You Lose");
    }
  } else if (internalCount === turnCount) {
    computerPlaying = true;
    playTheGame();
  }
}
              
            
!
999px

Console