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 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">
    <link href="https://fonts.googleapis.com/css?family=Orbitron" rel="stylesheet">
    <link rel="stylesheet" href="./assets/main.css">
    <title>Simon Game</title>
</head>
<body>
    <div id="game-device">
        <div id="controls">
            <div id="panel">
                <div id="head">
                    SIMON
                </div>
                <div id="row-toggle-1">
                    <div class="display">
                        <div class="screen">
                            <div class="screen-content js-screen-content">--</div>
                        </div>
                    </div>
                    <div class="skill-level">
                        <span class="title">SKILL LEVEL</span>
                        <div class="colDiv">
                            <div class="slide__toggle pointer" data-position="5" data-btn="skill"></div>
                            <span class="first">1</span>
                            <span>2</span>
                            <span>3</span>
                            <span>4</span>
                            <span>5</span>
                        </div>
                    </div>
                </div>
                <div id="buttons">
                    <div class="button-block">
                        <div class="button pointer js-last"></div>
                        <div class="title">LAST</div>
                    </div>
                    <div class="button-block">
                        <div class="button pointer start js-start"></div>
                        <div class="title">START</div>
                    </div>
                    <div class="button-block">
                        <div class="button pointer js-strict"></div>
                        <div class="title">STRICT</div>
                    </div>
                </div>
                <div id="power">
                    <span>OFF</span>
                    <div class="colDiv">
                        <div class="slide__toggle pointer" data-position="2" data-btn="power"></div>
                    </div>
                    <span>ON</span>
                </div>
            </div>
        </div>
        <div data-key="green" class="game-button top-left pointer"></div>
        <div data-key="red" class="game-button top-right pointer"></div>
        <div data-key="yellow" class="game-button bottom-left pointer"></div>
        <div data-key="blue" class="game-button bottom-right pointer"></div>
    </div>

    <audio data-key="green" src="https://s3.amazonaws.com/freecodecamp/simonSound1.mp3"></audio>
    <audio data-key="red" src="https://s3.amazonaws.com/freecodecamp/simonSound2.mp3"></audio>
    <audio data-key="yellow" src="https://s3.amazonaws.com/freecodecamp/simonSound3.mp3"></audio>
    <audio data-key="blue" src="https://s3.amazonaws.com/freecodecamp/simonSound4.mp3"></audio>
</body>
<script src="./assets/script.js"></script>
</html>
              
            
!

CSS

              
                /*==========================
        General
==========================*/

body {
    background: url('http://images.all-free-download.com/images/graphiclarge/wood_grain_highdefinition_picture_3_169735.jpg');
}

.pointer {
    cursor: pointer;
}

/*==========================
        Board
==========================*/

#game-device {
    height: 700px;
    width: 700px;
    border-radius: 50%;
    margin: auto;
    background-color: #000;
    
    position: relative;
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    grid-template-rows: repeat(2, 1fr);
}

/* outer circle */
#controls {
    height: 50%;
    width: 50%;
    background-color: black;
    
    position: absolute;
    top: 25%;
    left: 25%;
    z-index: 1;
    
    border-radius: 50%;
}

/* inner circle */
#controls::before {
    content: '';
    height: 90%;
    width: 90%;
    
    position: absolute;
    top: 5%;
    left: 5%;
    z-index: 2;
    
    background-color: #C0C0C0;
    border-radius: 50%;
}

/*==========================
        Controls
==========================*/
#panel {
    height: 72%;
    width: 54%;
    
    position: absolute;
    bottom: 14%;
    left: 23%;
    z-index:3;
    
    display: grid;
    grid-template-rows: 3fr repeat(2, 1fr);
    text-align: center;
}

/* Head */

#head {
    font-size: 3em;
    align-self: center;
    font-family: 'Orbitron', sans-serif;
}

/* row toggle 1 */

#row-toggle-1 {
    /* grid */
    display: grid;
    grid-template-columns: 40% 56%;
    grid-column-gap: 4%;
}

.colDiv {
    display: grid;
    grid-template-rows: 1fr 1fr; 
}

/* Screen display */
.display {
    font-family: 'Orbitron', sans-serif;
    display: flex;
    justify-content: center;
    align-items: center;
}

.screen {
    box-sizing: border-box;
    height: 90%;
    width: 80%;
    border: 5px solid black;
    border-radius: 5px;
    background-color: #525c39;
    display: flex;
    justify-content: center;
    align-items: center;
}

.screen-content {
    color:#B80100;
    opacity: 0.5;
}

/* skill level toggle */
.skill-level .colDiv {
    grid-template-columns: repeat(7, 1fr);
}
.skill-level .slide__toggle {
    grid-column-start: 3;
}

.first {
    grid-column-start: 2;
}

.slide__toggle {
    height: 100%;
    grid-column: 1 / span 3;    /* Permalink - use to edit and share this gradient: http://colorzilla.com/gradient-editor/#580d00+0,ca1e00+45,ff4e2f+50,ff4e2f+50,ca1e00+55,761200+100 */
    background: #580d00; /* Old browsers */
    background: -moz-linear-gradient(left,  #580d00 0%, #ca1e00 45%, #ff4e2f 50%, #ff4e2f 50%, #ca1e00 55%, #761200 100%); /* FF3.6-15 */
    background: -webkit-linear-gradient(left,  #580d00 0%,#ca1e00 45%,#ff4e2f 50%,#ff4e2f 50%,#ca1e00 55%,#761200 100%); /* Chrome10-25,Safari5.1-6 */
    background: linear-gradient(to right,  #580d00 0%,#ca1e00 45%,#ff4e2f 50%,#ff4e2f 50%,#ca1e00 55%,#761200 100%); /* W3C, IE10+, FF16+, Chrome26+, Opera12+, Safari7+ */
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#580d00', endColorstr='#761200',GradientType=1 ); /* IE6-9 */
}

/* ON/OFF */

#power {
    margin: 0 auto;
    width: 60%;
    display: grid;
    grid-template-columns: 1fr 4fr 1fr;
    grid-column-gap: 3px;
}

#power .colDiv {
    grid-template-columns: repeat(4, 1fr);
    grid-template-rows: 1fr;
}

/* Last/Start/Longest */

#buttons {
    margin: 5px 0;
    /* grid */
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    justify-items: center;
}

.button {
    height: 22px;
    width: 22px;
    margin: auto;
    /* positioning */
    position: relative;
    /* styling */
    background-color:#000;
    border-radius: 50%;
}

.button::before {
    content: '';
    height: 60%;
    width: 60%;
    /* positioning */
    position: absolute;
    top: 20%;
    left: 20%;
    z-index: 2;
    /* styling */
    background-color:yellow;
    border-radius: 50%;
}

.js-strict::after {
    content: '';
    height: 20%;
    width: 20%;
    top: 40%;
    left: -40%;
    /* top: 40%;
    left: -20%; */
    position: absolute;
    background-color: #750100;
    border-radius: 50%;
}

.js-strict.active::after  {
    background-color: #FF0000;
}

.button.start::before {
    background-color: red;
}

.title {
    margin-top: 3px;
    font-size: 0.8em;
}

.power-slide__toggle {
    width: 80%;
    height: 70%;
    align-self: center;
    /* Permalink - use to edit and share this gradient: http://colorzilla.com/gradient-editor/#580d00+0,ca1e00+45,ff4e2f+50,ff4e2f+50,ca1e00+55,761200+100 */
    background: #580d00; /* Old browsers */
    background: -moz-linear-gradient(left,  #580d00 0%, #ca1e00 45%, #ff4e2f 50%, #ff4e2f 50%, #ca1e00 55%, #761200 100%); /* FF3.6-15 */
    background: -webkit-linear-gradient(left,  #580d00 0%,#ca1e00 45%,#ff4e2f 50%,#ff4e2f 50%,#ca1e00 55%,#761200 100%); /* Chrome10-25,Safari5.1-6 */
    background: linear-gradient(to right,  #580d00 0%,#ca1e00 45%,#ff4e2f 50%,#ff4e2f 50%,#ca1e00 55%,#761200 100%); /* W3C, IE10+, FF16+, Chrome26+, Opera12+, Safari7+ */
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#580d00', endColorstr='#761200',GradientType=1 ); /* IE6-9 */
}

/*==========================
        Buttons
==========================*/

/* buttons color from: http://www.color-hex.com/color-palette/17204 */

.game-button {
    /* box styling */
    height: 85%;
    width: 85%;
    /* positioning */
    position: relative;
    transition:all 0.2s ease-in-out;
    opacity: 0.5;
}

.top-left {
    /* box styling */
    border-radius: 100% 0 0 0;
    margin: 0 3% 3% 0;
    /* styling */
    align-self: end;
    justify-self: end;
    background-color: rgb(62,221,75);
}

.top-right {
    /* box styling */
    border-radius: 0 100% 0 0;
    margin: 0 0 3% 3%;
    /* styling */
    align-self: end;
    justify-self: start;
    background-color: rgb(221,75,62);
}

.bottom-right {
    /* box styling */
    border-radius: 0 0 100% 0;
    margin: 3% 0 0 3%;
    /* styling */
    align-self: start;
    justify-self: start;
    background-color: rgb(75,62,221);
}

.bottom-left {
    /* box styling */
    border-radius: 0 0 0 100%;
    margin: 3% 3% 0 0;
    /* styling */
    align-self: start;
    justify-self: end;
    background-color: rgb(255,234,55);
}

.active {
    transition:all 0.1s ease-in-out;
    opacity: 1;
}

/* test */


              
            
!

JS

              
                // ========================
//        Controls
// ========================
const buttonToggle = Array.from(document.getElementsByClassName('slide__toggle'));
const screenContent = document.querySelector(".js-screen-content");
var environmentVar = {
    gameStart: false,
    strict: false,
};

buttonToggle.forEach(toggle => { 
    const name = toggle.dataset.btn;
    const currVal = parseInt(window.getComputedStyle(toggle).getPropertyValue('grid-column-start'));
    environmentVar[name] = currVal;
});

function toggleButton() {
    const curVal = parseInt(window.getComputedStyle(this).getPropertyValue('grid-column-start'));
    const nrPosition = this.dataset.position;
    const btnName = this.dataset.btn;
    // disable toggling buttons when the game has started
    if (environmentVar.gameStart && 
        environmentVar.power === 2 && 
        btnName !== 'power') return; // power === 1 is off, 2 is on

    var newVal = 1;
    if(!curVal) {
        newVal = 2;
    } else if (curVal < nrPosition) {
        newVal = curVal + 1;
    }
    // update toggle position
    this.style.gridColumnStart = newVal;
    // update variable's value
    environmentVar[btnName] = newVal;

    screenContent.classList.toggle("active");
    if (btnName === 'power' && environmentVar.gameStart) {
        environmentVar.gameStart = !environmentVar.gameStart;
        playerSequence = 0;         // reset player's sequence
        speed = 1000;               // reset speed
        computerSequence = [];      // reset the computer sequence
        display('--');              // reset the display
        playerTurn = !playerTurn;
    }
}

buttonToggle.forEach(toggle => toggle.addEventListener('click', toggleButton));

function startGame() {
    if (environmentVar.power === 1) return; // power === 1 is off, 2 is on
    environmentVar.gameStart = !environmentVar.gameStart;

    if(environmentVar.gameStart) {
        computerTurn();
    } else {
        reinitVar();
        display('--');              // reset the display
        playerTurn = !playerTurn;
    }
}

function reinitVar() {
    playerSequence = 0;         // reset player's sequence
    speed = 1000;               // reset speed
    computerSequence = [];      // reset the computer sequence
}

function strictMode() {
    environmentVar.strict = !environmentVar.strict;
    this.classList.toggle('active');
}

document.getElementsByClassName("js-start")[0].addEventListener('click', startGame);
document.getElementsByClassName("js-last")[0].addEventListener('click', replaySequence);
document.getElementsByClassName("js-strict")[0].addEventListener('click', strictMode);


// ========================
//      Game environment
// ========================

// Playing sounds
function playSound() {
    var key = this.dataset.key;
    if (!playerTurn) return; // power === 1 is off, 2 is on
    playButton(key);
}

const buttons = Array.from(document.querySelectorAll('.game-button'));
buttons.forEach(button => button.addEventListener('click',playSound));

// ========================
//        Game logics
// ========================
const buttonsKey = ['green', 'red', 'blue', 'yellow']       // playable buttons
let speed = 1000;
var computerSequence = [];                                  // Computer sequence
var longestSequence = [];                                   // Longest sequence played
var playerSequence = 0;                                     // Array to register the player
var playerTurn = false;

var skillLevel = {
    1: 8,
    2: 14,
    3: 20,
    4: 31,
    5: 42,
}


function computerTurn() {
    let max = 4;
    let newKey = getKey(buttonsKey, max);
    let seqLen = computerSequence.length;

    // check if game finished
    if(seqLen > skillLevel[environmentVar.skill] - 1) {
        display('WIN');
        return;
    }

    // increase speed
    if (seqLen && seqLen < 15 && seqLen % 4 === 0) {
        speed -= 200;
    }
    
    // check that the newKey is different than the two last one
    if ( seqLen > 1 &&
    computerSequence[seqLen-1] === computerSequence[seqLen-2] &&
    computerSequence[seqLen-1] === newKey) {
        // filter array
        let filtbuttons = buttonsKey.filter(key => key !== newKey);
        // redefine maximum choices and newKey
        newKey = getKey(filtbuttons, max--);
    }

    // replay sequence and play newKey
    display(('0' + (computerSequence.length + 1)).slice(-2));
    for (let i = 0; i <= seqLen; i++) {
        (function(val) {
            if (!environmentVar.gameStart) return;
            setTimeout(() => {
                if (i === seqLen) {
                    playButton(newKey);
                    computerSequence.push(newKey);
                    playerTurn = !playerTurn;
                } else {
                    playButton(val);
                }
            }, speed * i);
        })(computerSequence[i]);
    }
}

function getKey(arr, max) {
    var random = Math.floor(Math.random() * max);
    return arr[random];
}

function replaySequence() {
    playerTurn = !playerTurn;
    for (let i = 0; i <= computerSequence.length; i++) {
        (function(val) {
            setTimeout(() => {
                if (i === computerSequence.length) {
                    playerTurn = !playerTurn;
                } else {
                    playButton(val);
                }
            }, speed * i);
        })(computerSequence[i]);
    }
}

function playButton(key) {
    if (!environmentVar.gameStart) return; // power === 1 is off, 2 is on
    const audio = document.querySelector(`audio[data-key="${key}"]`);
    audio.currentTime = 0;
    audio.play();
    activateButton(key);
    
    // Check played key vs computer sequence
    if (environmentVar.gameStart && playerTurn) {
        if(computerSequence[playerSequence] === key) {                      // entered button is correct
            if(playerSequence === computerSequence.length - 1) {            // pressed button is last of sequence
                if (computerSequence.length > longestSequence.length) {
                    longestSequence = computerSequence.slice();
                }
                playerSequence = 0;                                         // resets playerSequence
                playerTurn = !playerTurn;
                setTimeout(() => {
                    computerTurn();
                }, speed * 2); 
            } else {
                playerSequence++;
            }
        } else {
            // display error
            display("! !",5);
            playerSequence = 0;
            if (environmentVar.strict) {
                playerTurn = !playerTurn;
                reinitVar();
                setTimeout(() => {
                    computerTurn();
                }, 1500);
            } else {
                setTimeout(() => {
                    replaySequence();
                }, speed * 1.5); 
            }
        }
    }
}

function activateButton (key) {
    const button = document.querySelector(`div[data-key="${key}"]`); 
    button.classList.toggle("active");
    setTimeout(() => button.classList.toggle("active"),500);
}


// Display the value in the screen area
function display(val, repeat = null) {
    const dispCurrVal = screenContent.textContent;
    screenContent.textContent = val;

    if(repeat) {
        if(repeat % 2 !== 0) { repeat++; }  // if repeat is odd, change value to next even nbr
        let times = 0;
        var blink = setInterval(()=> {
            if(times < repeat) {
                screenContent.style.display = (screenContent.style.display === 'none' ? '' : 'none');
                times++;
            } else {
                screenContent.textContent = dispCurrVal;
                clearInterval(blink);
            }
        }, 200);
    }

}

              
            
!
999px

Console