123

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.

            
              <div class="wrapper">
    <header>
      <h1>Simon Game</h1>
      <p>Created by Miguel T Rivera</p>
    </header>
    <main>
      <div id="colors">
        <button type="button" id="green"></button>
        <button type="button" id="red"></button>
        <button type="button" id="yellow"></button>
        <button type="button" id="blue"></button>
      </div>
      <div class="feedback">
        Count<span class="feedback__score-number"></span>
        Strict Status<span class="feedback__status-text"></span>
      </div>
      <div class="menu">
        <button type="button" class="menu__start-button">start</button>
        <button type="button" class="menu__reset-button">reset</button>
        <button type="button" class="menu__strict-button">strict</button>
      </div>
    </main>
</div>
            
          
!
            
              *, *::before, *::after {
  -webkit-box-sizing: border-box; 
  -moz-box-sizing: border-box; 
  box-sizing: border-box;
}
body {
  background: white;
  font: 1.0em Arial, sans-serif;
}
/* Grid styles **************************/
header { 
  grid-area: header;
  text-align: center;
}
main {
  grid-area: main;
}
.wrapper { 
  max-width: 960px;
  margin: 0 auto;
  display: grid;
  grid-template-rows: 150px;
  grid-template-columns: repeat(2, 1fr);
  grid-gap: 0 10px;
  grid-template-areas: 
  'header header'
  'main main';
}
#colors {
  max-width: 300px;
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  grid-template-rows: repeat(2, 1fr);
  margin: 0 auto;
 /* margin-bottom: 20px;*/
}
/* Button styles *************************/
.menu > button {
  width: 80%;
  margin-bottom: 3%;
  color: white;
  border-radius: 5px;
  border: 0;
  background: steelblue;
  padding: 5px 15px;
  font-weight: 700;
  text-transform: uppercase;
}
.menu > button:disabled {
  color: #666;
  background: #ccc;
  /*border: 1px solid #999;*/
}
#colors > button {
  height: 150px;
  width: 150px;
  border: 10px solid #000;
  box-shadow: 10px 10px 20px #2b242d;
}
#colors button:nth-of-type(1) {
  border-top-left-radius: 100%;
}
#colors button:nth-of-type(2) {
  border-top-right-radius: 100%;
}
#colors button:nth-of-type(3) {
  border-bottom-left-radius: 100%;
}
#colors button:nth-of-type(4) {
  border-bottom-right-radius: 100%;
}
.menu {
  margin-bottom: 10px;
  text-align: center;
}
.feedback {
  text-align: center;
  margin: 50px 10px;
}
.feedback__score-number,
.feedback__status-text {
  display: inline;
  margin-left: 0.5em;
  color: #000;
  padding: 5px;
  border: 1px solid #999;
  text-transform: uppercase;
}
.standard-green {
  background-color: #00924a;
}
.standard-red {
  background-color: #9f201a;
}
.standard-yellow {
  background-color: #cfa20d;
}
.standard-blue {
  background-color: #054894;
}
.blink-green {
  background-color: #649d81;
}
.blink-red {
  background-color: #9c7371;
}
.blink-yellow {
  background-color: #d5c797;
}
.blink-blue {
  background-color: #6a819a;
}
/* Helper Classes *****************************/
.txt-center {
  text-align: center;
}
.elm-center {
  margin: 0 auto;
}
.uppercase-txt {
  text-transform: uppercase;
}
/* Media Queries *****************************/
@media screen and (min-width: 768px) {
  .btn {
    width: 30%;
    font-size: 1.2em;
  }
  .wrapper { 
    max-width: 960px;
    margin: 0 auto;
    display: grid;
    grid-template-rows: 150px;
    grid-template-columns: 200px 200px;
    grid-template-areas: 
    'header header'
    'main main';
  }
  #colors > button {
    height: 250px;
    width: 250px;
    border: 15px solid #000;
    box-shadow: 10px 10px 20px #2b242d;
  }
}
/*
@media (max-width: 599px) {
}
@media (min-width: 600px) and (max-width: 899px) {
}
@media (min-width: 900px) and (max-width: 1199px) {
}
@media (min-width: 1200px) and (max-width: 1799px) {
} 
@media (min-width: 1800px) {
}
*/
            
          
!
            
              'use strict';

const simon = {
  colors: ['green', 'red', 'yellow', 'blue'],
  speed: {
    normal: 2000, 
    moderate: 1667, 
    hard: 1333
  },
  sequence: [],
  sounds: {
    green: 'https://s3.amazonaws.com/freecodecamp/simonSound1.mp3',
    red: 'https://s3.amazonaws.com/freecodecamp/simonSound2.mp3',
    yellow: 'https://s3.amazonaws.com/freecodecamp/simonSound3.mp3',
    blue: 'https://s3.amazonaws.com/freecodecamp/simonSound4.mp3',
    error: 'https://cdn.rawgit.com/mtrivera/freecodecamp/master/simon/8-bit-error.mp3'
  },
  score: 0,
  step: 0,
  strictMode: false,
  WIN: 20,
  changeColor: (colorBtn) => {
    if (simon.isBlinkClass(colorBtn.className)) {
     colorBtn.className = `standard-${colorBtn.id}`;
     //console.log(`Change to ${colorBtn.className}`);
    } else {
     colorBtn.className = `blink-${colorBtn.id}`;
     //console.log(`Change to ${colorBtn.className}`);
    }
  },
  gameOver: (score) => {
    if (score == simon.WIN) {
      alert('You Win!');
      toggleBtn(startBtn);
      simon.init();
    }
  },
  getSpeed: (score) => { 
    if (score > 15) { 
      return simon.speed.hard; 
    } else if (score > 8 && score < 14) { 
      return simon.speed.moderate; 
    } else { 
      return simon.speed.normal; 
    } 
  }, 
  init: () => {
    simon.score = 0;
    simon.sequence = [];
    simon.step = 0;
    // Set default colors for the buttons
    greenBtn.className = `standard-${greenBtn.id}`;
    redBtn.className = `standard-${redBtn.id}`;
    yellowBtn.className = `standard-${yellowBtn.id}`;
    blueBtn.className = `standard-${blueBtn.id}`;
    // Set default values for the message boxes
    scoreMsg.textContent = '--';
    strictMsg.textContent = 'strict mode off'.toUpperCase();
  },
  isBlinkClass: (color_class) => {
    return /blink/.test(color_class);
  },
  nextSequence: () => {
    const nextColor = simon.colors[simon.rand()];
    simon.sequence.push(nextColor);
    simon.playSequence(simon.sequence);
    //console.log(`The sequence ${simon.sequence}`);
  },
  playSequence: (sequence, index = -1) => {
    if (index === sequence.length - 1) {  
      return; 
    } else {  
      // This solved the playback issue
      simon.playSequence(sequence, ++index);
      setTimeout(() => {  
        if (sequence[index] === greenBtn.id) {
         simon.changeColor(greenBtn);
         simon.playSound(greenBtn);
        } else if (sequence[index] === redBtn.id) {
          simon.changeColor(redBtn);
          simon.playSound(redBtn);
        } else if (sequence[index] === yellowBtn.id) {
          simon.changeColor(yellowBtn);
          simon.playSound(yellowBtn);
        } else if (sequence[index] === blueBtn.id) {
          simon.changeColor(blueBtn);
          simon.playSound(blueBtn);
        }
          //console.log(`play ${index} ${sequence[index]} at ${new Date().toLocaleString()}`);  
      }, simon.getSpeed(simon.score) * (index + 1) );  
      //simon.playSequence(sequence, ++index);  
    }  
  },
  playErrorSound: (errorURL) => {
    const audio = document.createElement('audio');
    audio.src = errorURL;
    audio.play();
  },
  playSound: (colorBtn) => {
    const audio = document.createElement('audio');
    //audio.src = url;
    audio.src = simon.sounds[colorBtn.id];
    // When sound ends, will change to default color
    audio.onended = () => {
      simon.changeColor(colorBtn);
      //console.log('Change color after sound');
    }
    audio.play();
  },
  rand: () => {
    return Math.floor(Math.random() * simon.colors.length);
  },
  renderScore: (score)=> {
    if (score < 10) {
      scoreMsg.textContent = `0${score}`;
    } else {
      scoreMsg.textContent = score;
    }
  },
  renderStrictMsg: (strict) => {
    if (simon.strictMode) {
      strictMsg.textContent = 'strict mode on'.toUpperCase();
      //console.log('strict mode on');
    } else {
      strictMsg.textContent = 'strict mode off'.toUpperCase();
      //console.log('strict mode off');
    }
  },
  sendColor: (color) => {
    if (!simon.sequence.length) {
      // Start game  
      simon.nextSequence();
    } else {
      if (color === simon.sequence[simon.step]) {
        // Go to next step
        if (simon.step === simon.sequence.length - 1) {
          //console.log('sequence complete');
          simon.step = 0;
          ++simon.score;
          simon.renderScore(simon.score);
          simon.gameOver(simon.score);
          simon.nextSequence();
        } else {
          simon.step += 1;
        }
      } else {
        simon.playErrorSound(simon.sounds.error);
        // Lose condition
        if (simon.strictMode) {
          toggleBtn(startBtn);
          simon.init();
          setTimeout(simon.nextSequence(), 3000);
        } else { 
          // Clears step for traversing sequence
          simon.step = 0;
          simon.playSequence(simon.sequence);
        }
      }
    }
    //console.log(`NEW COLOR ${color}`);
  }
};
const colorsDiv = document.getElementById('colors');
//const controlsDiv = document.getElementById('controls');
const controlsDiv = document.querySelector('.menu');
const colorsList = colorsDiv.children;
const controlsList = controlsDiv.children;
const greenBtn = document.getElementById('green');
const blueBtn = document.getElementById('blue');
const redBtn = document.getElementById('red');
const yellowBtn = document.getElementById('yellow');
const startBtn = document.querySelector('.menu__start-button');
const resetBtn = document.querySelector('.menu__reset-button');
const strictBtn = document.querySelector('.menu__strict-button');
const scoreMsg = document.getElementsByTagName('span')[0];
const strictMsg = document.getElementsByTagName('span')[1];

simon.init();

// Listen for colors being clicked
colorsDiv.addEventListener('click', (e) => {
    if (e.target.tagName === 'BUTTON') {
      if (e.target === greenBtn) {
        //console.log('Green button pressed');
        simon.sendColor(greenBtn.id);
        simon.changeColor(greenBtn);
        simon.playSound(greenBtn);
      }
      if (e.target === redBtn) {
        //console.log('Red button pressed');
        simon.sendColor(redBtn.id);
        simon.changeColor(redBtn);
        simon.playSound(redBtn);
      }
      if (e.target === yellowBtn) {
        //console.log('Yellow button pressed');
        simon.sendColor(yellowBtn.id);
        simon.changeColor(yellowBtn);
        simon.playSound(yellowBtn);
      }
      if (e.target === blueBtn) {
        //console.log('Blue button pressed');
        simon.sendColor(blueBtn.id);
        simon.changeColor(blueBtn);
        simon.playSound(blueBtn);
      }
    }
});

// Listen for control buttons being clicked
controlsDiv.addEventListener('click', (e) => {
  if (e.target.tagName === 'BUTTON') {
    if (e.target === startBtn) {
      toggleBtn(startBtn);
      simon.nextSequence();
      //console.log('Start button pressed\nGame On!');
    }
    if (e.target === resetBtn) {
      simon.init();
      //console.log('Reset button pressed\nReset the game!');
    }
    if (e.target === strictBtn) {
      simon.strictMode = !simon.strictMode;
      simon.renderStrictMsg(simon.strictMode);
      //console.log('Strict button pressed');
    }
  }
});

// Disables/enables a button element
function toggleBtn(btn) {
  if (btn.disabled) {
    btn.disabled = false;
  } else {
    btn.disabled = true;
  }
}
            
          
!
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