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

              
                main
  .game-buttons
    .red
    .blue
    .green
    .yellow
  .info
    h1 Simon Game
    .display
    button.reset Reset
    button.strict Strict Mode
  
              
            
!

CSS

              
                $red: #ec407a
$yellow: #ffea00
$green: #00bfa5
$blue: #7c4dff
$neutral: #333

@mixin color-button($color)
  background-color: darken($color, 7%)
  &.active
    background-color: $color

body
  background-color: #ccc
  
button
  &:hover
    cursor: pointer

body, main, .info
  display: flex
  justify-content: center
  align-items: center
  flex-wrap: wrap

body
  min-height: 100vh
  font-family: sans-serif
  
main
  height: 410px
  width: 410px
  border-radius: 100%
  background-color: $neutral
  
.info
  position: absolute
  box-sizing: border-box
  padding: 2em 0
  height: 12em
  width: 12em
  background-color: #fff
  border-radius: 100%
  border: 4px solid $neutral
  z-index: 1
  
.display
  background-color: $neutral
  color: #fff
  font-weight: bold
  text-align: center
  line-height: 2em
  height: 30px
  flex-basis: 90%
  
h1
  padding: 0
  margin: 0
  font-size: 140%
  
.game-buttons
  width: 100%
  height: 100%
  display: flex
  flex-wrap: wrap
  overflow: hidden
  border: 6px solid $neutral
  border-radius: 100%
  transform: rotate(45deg)
  
.game-buttons > div
  box-sizing: border-box
  width: 50%
  height: 50%
  &:hover
    cursor: pointer
  
.red
  @include color-button($red)
  border-right: 4px solid $neutral
  border-bottom: 4px solid $neutral
  
.blue
  @include color-button($blue)
  border-bottom: 4px solid $neutral
  
.yellow
  @include color-button($yellow)
  
.green
  @include color-button($green)
  border-right: 4px solid $neutral
  
.strict
  background-color: #fff
  border-radius: 5px
  border: 1px solid #ddd
  &.yes
    background-color: $red
              
            
!

JS

              
                game = {
  moves: [],
  playerMoves: [],
  turn: 1,
  playerTurn: false,
  strict: false,
  buttons: {
    '0': { 
      audio: new Audio('https://s3.amazonaws.com/freecodecamp/simonSound1.mp3'),
      color: 'red'
    },
    '1': {
      audio: new Audio('https://s3.amazonaws.com/freecodecamp/simonSound2.mp3'),
      color: 'yellow'
    },
    '2': {
      audio: new Audio('https://s3.amazonaws.com/freecodecamp/simonSound3.mp3'),
      color: 'green'
    },
    '3': {
      audio: new Audio('https://s3.amazonaws.com/freecodecamp/simonSound4.mp3'),
      color: 'blue'
    }
  }
}

function setGame() {
  game.moves = [];
  game.playerMoves = [];
  game.turn = 1;
  game.playerTurn = false;
  while (game.moves.length < 20) {
    game.moves.push(Math.floor(Math.random() * 4));
  }
}

function returnBtnNum(color) {
  if (color == game.buttons['0']['color']) {
    return '0';
  } else if (color == game.buttons['1']['color']) {
    return '1';
  } else if (color == game.buttons['2']['color']) {
    return '2';
  } else {
    return '3';
  }
}

function playSound(n) {
  game.buttons[n]['audio'].play();
  $('.' + game.buttons[n]['color']).addClass('active');
  setTimeout(function() {
    $('.active').removeClass('active');
  }, 400);
}

function playSounds(arr) {
  var intID = setInterval(function() {
    if (arr.length > 0) {
      playSound(arr.slice(-1));
      arr = arr.slice(0, -1);
    } else {
      clearInterval(intID);
    }
  }, 1000);
  setTimeout(function() {
    game.playerTurn = true;
  }, (arr.length * 1000));
}

function playGame(turn) {
  game.playerTurn = false;
  var a = game.moves.slice( game.turn * (-1) );
  $('.display').text(turn);
  if (game.turn < game.moves.length) {
    playSounds( a, -1);
  }
}

function checkMove(move) {
  if ( move == game.moves[ game.moves.length - (game.playerMoves.length + 1) ]) {
    return true;
  } else {
    return false;
  }
}

$('.game-buttons > div').on('click', function() {
  var c = ($(this).attr('class'));
  var n = parseInt(returnBtnNum(c), 10);
  if (game.playerTurn) {
    playSound(n);
    if (checkMove(n)) {
      game.playerMoves.unshift(n);
      if (game.playerMoves.length == game.turn) {
        if (game.turn == 20) {
          $('.display').text('You win!!!');
          setTimeout(function() { 
            setGame();
            playGame(game.turn);
          }, 2000);
        } else {
          game.playerMoves = [];
          game.playerTurn = false;
          game.turn++;
          playGame(game.turn);   
        }
      }
    } else {
      $('.display').text('Uh oh! Try again...');
      if (game.strict) {
        setTimeout(function(){
          setGame();
          playGame(game.turn);
        }, 3000);
      } else {
        setTimeout(function() {
          game.playerMoves = [];
          playGame(game.turn);
        }, 2000);
      }
    }
  }
});

$('.reset').on('click', function() {
  setGame();
  setTimeout(function(){
    playGame(game.turn)
  }, 3000);
});

$('.strict').on('click', function() {
  var g = game.strict;
  $(this).toggleClass('yes');
  setGame();
  game.strict = g ? false : true;
  setTimeout(function(){
    playGame(game.turn)
  }, 3000);
});

setGame();
playGame(game.turn);

              
            
!
999px

Console