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

              
                <audio id="simonSound1" src="https://crossorigin.me/https://s3.amazonaws.com/freecodecamp/simonSound1.mp3" preload="auto" crossorigin="anonymous">
        <p>Your browser does not support the <code>audio</code> element.</p>
    </audio>

    <audio id="simonSound2" src="https://crossorigin.me/https://s3.amazonaws.com/freecodecamp/simonSound2.mp3" preload="auto" crossorigin="anonymous">
        <p>Your browser does not support the <code>audio</code> element.</p>
    </audio>

    <audio id="simonSound3" src="https://crossorigin.me/https://s3.amazonaws.com/freecodecamp/simonSound3.mp3" preload="auto" crossorigin="anonymous">
        <p>Your browser does not support the <code>audio</code> element.</p>
    </audio>

    <audio id="simonSound4" src="https://crossorigin.me/https://s3.amazonaws.com/freecodecamp/simonSound4.mp3" preload="auto" crossorigin="anonymous">
        <p>Your browser does not support the <code>audio</code> element.</p>
    </audio>

    <div class="container-fluid">
        <div class="row">
            <div class="col-xs-12">
                <div class="center">
                        <div class="simon-body">
                            <div class="components">
                                <div id="4" class="quad top-left-quad"></div>
                                <div id="1" class="quad top-right-quad"></div>
                                <div id="3" class="quad bot-left-quad"></div>
                                <div id="2" class="quad bot-right-quad"></div>
                                <div class="center-console-spacer-ring"></div>
                                <div class="center-console">
                                    <h2 class="toy-name">Simon</h2>
                                </div>
                                <div class="count-display">
                                    <p></p>
                                </div>
                                <div class="start-reset-button">
                                    <p>START</p>
                                </div>
                                <div class="strict-button">
                                    <p>STRICT</p>
                                </div>
                                <div class="strict-button-indicator"></div>
                                <div class="on-off-button">
                                    <p>ON/OFF</p>
                                </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
              
            
!

CSS

              
                * {
    /*border: 1px red dashed;*/
}

div h1 {
    text-align: center;
}

.wrapper {
    max-width: 50%;
    margin: 0 auto;

}

.simon-body {
    background-color: gray;
    width: 320px; height: 320px;
    border-radius: 50%;
    margin-top: 50px;
    /*position: relative;*/
}

/*retro-fitted this class to contain the other components as they had been positioned already! */
.components {
    position: relative;
    top: 10px;
    left: 10px;
}

.quad {
    height: 130px; width: 130px;

}

.top-left-quad {
    border-radius: 100% 0 0 0;
    background-color: #8FED28;
    position: absolute;
    top: 10px;
    left: 10px;

}

.top-right-quad {
    border-radius: 0% 100% 0 0;
    background-color: #FA023C;
    position: absolute;
    top: 10px;
    left: 160px;
}

.bot-left-quad {
    border-radius: 0 0 0 100%;
    background-color: #FFFF00;
    position: absolute;
    top: 160px;
    left: 10px;
}

.bot-right-quad {
    border-radius: 0 0 100% 0;
    background-color: #69D2E7;
    position: absolute;
    top: 160px;
    left: 160px;
}

/* TODO: can i make these quadrant curves match the outer curve? */
/* TODO: buttons should have shadows to give 3D look */
/* TODO: hover over quadrants should depress slightly to indicate they can be pressed */

.center-console-spacer-ring {
    height: 170px; width: 170px;
    background-color: gray;
    border-radius: 100%;
    position: absolute;
    top: 65px;
    left: 65px;
}

.center-console {
    height: 150px; width: 150px;
    background-color: silver;
    border-radius: 100%;
    position: absolute;
    top: 75px;
    left: 75px;
}

.toy-name {
    position: absolute;
    top: 0px;
    left: 30px;
}

.count-display {
    position: absolute;
    top: 135px;
    left: 90px;
    height: 25px; width: 35px;
    padding-left: 5px;
    padding-top: 5px;
    background-color: black;
    border-radius: 5px;
    font-family: monospace;
    font-size: 15px;
    font-weight: bold;
    color: #DC0007;

}

.start-reset-button {
    height: 20px; width: 20px;
    background-color: red;
    border: 3px solid black;
    border-radius: 100%;
    position: absolute;
    top: 140px;
    left: 150px;
}

.strict-button {
    height: 20px; width: 20px;
    background-color: yellow;
    border: 3px solid black;
    border-radius: 100%;
    position: absolute;
    top: 140px;
    left: 190px;
}

.strict-button-indicator {
    height: 10px; width: 10px;
    background-color: black;
    border: 2px solid black;
    border-radius: 100%;
    position: absolute;
    top: 128px;
    left: 195px;
}

.strict-button p {
    font-size: 8px;
    position: relative;
    bottom: -20px;
    left: -6px;
}

.on-off-button {
    height: 20px; width: 30px;
    background-color: #ff0000;
    border: 2px solid black;
    position: absolute;
    top: 180px;
    left: 135px;
}

.on-off-button p {
    font-size: 8px;
    position: relative;
    bottom: -20px;
    left: -2px;
}

.start-reset-button p {
    font-size: 8px;
    position: relative;
    bottom: -20px;
    left: -6px;
}

/* TODO:  sort this css positioning of buttons and labels - make a top and bottom row, with divs and can then centre the text directly under the buttons. When make a slider for the on/off button, can put 'on' and 'off' on either end of the button, inline.
*/

/* TODO: think there is a tool that will determine which CSS is unused? */

.center {
    width: 300px;
    margin:0 auto;
}


body {
    margin: 0;
    background-image: url(http://cdn.backgroundhost.com/backgrounds/subtlepatterns/tileable_wood_texture.png);
}

.row {

    /*display: inline;
    clear: both;*/
}

.row:after {
    clear: both;
    content: " ";
    display: table;
}

.col {
    background: #333;
    border-radius: 5px;
    color: white;
    padding: 10px .5%;
    float: left;
    margin: 1%;
    width: 31%
}

footer {
    clear: both;
}

              
            
!

JS

              
                // Simon Game from FreeCodeCamp

// User Story: I am presented with a random series of button presses.
// User Story: Each time I input a series of button presses correctly, I see the same series of button presses but with an additional step.
// User Story: I hear a sound that corresponds to each button both when the series of button presses plays, and when I personally press a button.
// User Story: If I press the wrong button, I am notified that I have done so, and that series of button presses starts again to remind me of the pattern so I can try again.
// User Story: I can see how many steps are in the current series of button presses.
// User Story: If I want to restart, I can hit a button to do so, and the game will return to a single step.
// User Story: I can play in strict mode where if I get a button press wrong, it notifies me that I have done so, and the game restarts at a new random series of button presses.
// User Story: I can win the game by getting a series of 20 steps correct. I am notified of my victory, then the game starts over.

// IDEA: Timeout if not pressed a button in x secs then it resets to the start

'use strict';  // Recommended as best practice.

const MAX_LEVEL = 20;
let isOn = false;
let isStrict = false;
let sequenceArr = [];
let buttonPressesArr = [];

$(".on-off-button").click(function(){
	powerToggle();
});

$(".strict-button").click(function(){
	strictToggle();
});

$(".start-reset-button").click(function(){
	startReset();
});

function resetGameState() {
	sequenceArr = [];
	buttonPressesArr = [];
}

function congratulate() {
	// flash lights or something in sequence
	$(".count-display p").html("WIN");  // TODO: make this '20' 'YOU' 'WON' '20' 'YOU' 'WON'...
	$(".quad").fadeOut(200).fadeIn(100).fadeOut(200).fadeIn(100).fadeOut(200).fadeIn(100);
}

function powerToggle() {
	// FIXME: There is craziness if turning off you can then still press buttons and the clickhandler still fires. Maybe add an if isOn to the click handler?
	if (!isOn) {
		isOn = true;
		$(".on-off-button").css("background-color", "#ff8000");
		$(".count-display p").html("--").hide();
		$(".count-display p").fadeIn(300);
		resetGameState();
		console.log('isOn = ' + isOn + ': Powered On');
		// TODO: try making opacity when off: $(this).css({opacity: '0.6'}) so buttons look more like they're lighting up when pressed.
	} else {
		resetGameState();
		isOn = false;
		$(".on-off-button").css("background-color", "#ff0000");
		$(".count-display p").fadeOut(1000);
		console.log('isOn = ' + isOn + ': Powered Off');
	}
	// TODO: make this button into a slider switch
	// TODO: when the power button is switched off, any sequences currently playing should but cut short and stop
}

function strictToggle() {
	if (isOn && !isStrict) {
		isStrict = true;
		$(".strict-button-indicator").css("background-color", "#DC0007");
		console.log('isStrict = ' + isStrict + ': Strict mode is now on');
	} else {
		isStrict = false;
		$(".strict-button-indicator").css("background-color", "black");
		console.log('isStrict = ' + isStrict + ': Strict mode is now off');
		// note, to enable fade in/out of colours, you have to use a 3rd party plugin: https://github.com/jquery/jquery-color/
	}
}

function flashButton(buttonId) {
	$("#"+buttonId).fadeOut(300).fadeIn(200);
	document.getElementById('simonSound'+buttonId).play();
}

// flash and play sound for that quadrant
function flashButtonSequence(sequenceArr) {
	// iterate through the sequenceArr.shift? then with a timeout? to flash the lights? forEach is asynchronous I think, so won't work.
	let clonedSequenceArr = sequenceArr.slice(0);  // .slice(0) is a way to clone an array... only does a shallow clone
	function loop() {
	    setTimeout(function() {
			let button = clonedSequenceArr.shift();
			flashButton(button);
	        //console.log(button)
	        if (clonedSequenceArr.length > 0) {
	            loop();
	        }
	    }, 1000);
	}
	loop();
}

function startReset() {
	if (isOn) {
		// FIXME: this should cancel any sequences/sounds playing. Currently the flashButtonSequence will continue until finished. How do I cut it short?
		console.log('isOn = ' + isOn + ': system reset');
		$(".count-display p").html("1").hide().fadeIn(200).fadeOut(200).fadeIn(200);
		resetGameState();
		nextPlay();
	}
}

function nextPlay() {
	// Choose random quadrant, and append to the sequence
	let randomButton = Math.floor(Math.random() * 4) + 1;
	sequenceArr.push(randomButton);
	console.log('Sequence is: ' + sequenceArr);
	flashButtonSequence(sequenceArr);
	$(".count-display p").html(sequenceArr.length);
}

function compareIndexVals(buttonPressesArr, sequenceArr) {
	// TODO: Don't think this is very efficient as it checks every element, every time. Also, get rid of the for loop. Is there an inbuilt method to compare two arrays?
    for (let i = 0; i < buttonPressesArr.length; i++) {	// FIXME: dirty code smell? use .reduce instead?
        if ( buttonPressesArr[i] !== sequenceArr[i] ) {
            return false;
        }
    }
    return true;
}

// quadrant click handler
$(".quad").click(function(){
	// FIXME: Ohh-errr...there's some weirdness going on if you click too much... :-/ Perhaps a flag of some kind to prevent you clicking whilst a flashButtonSequence is playing?
	// FIXME: Should all this spaghetti code be in a click handler? Is this bad form?
	// TODO: 3D depresses on buttons
	$(this).fadeOut(200).fadeIn(200);
	document.getElementById('simonSound' + this.id).play();

	buttonPressesArr.push( parseInt(this.id) );
	console.log('buttonPressesArr: ' + buttonPressesArr);

	//if player got the sequence correct
	if ( compareIndexVals(buttonPressesArr, sequenceArr) ) {
		//if final level reached
		if (buttonPressesArr.length === sequenceArr.length && sequenceArr.length === MAX_LEVEL) {
			congratulate();
			//else if just the sequence was correct for that level
		} else if ( buttonPressesArr.length === sequenceArr.length ) {
			buttonPressesArr = [];
			nextPlay();
		}
	//if sequence was not correct
	} else {
		// TODO: play the error grrrrrr sound here!
		// TODO: break these fails out into functions, strictFail, nonStrictFail?
		// TODO: look at these multiple fadeIn/FadeOut bleuchhh... DRY it up.
		console.log('fail');
		$(".count-display p").html("!!!").fadeOut(400).fadeIn(20).fadeOut(400).fadeOut(400).fadeIn(20);
		$(".count-display p").html(sequenceArr.length);
		// if strict mode on
		if (isStrict) {
			$(".count-display p").html("!!!").fadeOut(400).fadeIn(20).fadeOut(400).fadeOut(400).fadeIn(20, function() {
				startReset();
			});
			//if strict mode off
		} else if (!isStrict) {
			$(".count-display p").html("!!!").fadeOut(400).fadeIn(20).fadeOut(400).fadeOut(400).fadeIn(20, function() {
				buttonPressesArr = [];
				$(".count-display p").html(sequenceArr.length).fadeIn(function() {
					flashButtonSequence(sequenceArr);
				});
			});
			console.log('Sequence is: ' + sequenceArr);
			console.log('buttonPressesArr: ' + buttonPressesArr);
		}
	}
});

              
            
!
999px

Console