cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

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.

            
              

<title>Simon Game</title>


<body>
  <div class="container">
    <div class="controls">
      <span class="row">
    <span id="roundIndicator">0</span>
      </span>
      <div class="row">
    <button id="switchButton" class="controls" onclick="switcher()">turn on</button>    
    <button id="startButton" class="controls disabled" onclick="start()">start</button>
  <button id="resetButton" class="controls disabled" onclick="reset()">reset</button>
  <button id="strictButton" class="controls disabled" onclick="strict()">strict</button>
     </div>
      </div>
      <div class="buttons">
        <div class="row brow">
          <button id="b1" class="sound" onclick="s1()">&nbsp</button>
          <button id="b4" class="sound" onclick="s4()">&nbsp</button>
        </div>
        <div class="row brow">
          <button id="b2" class="sound" onclick="s2()">&nbsp</button>
          <button id="b3" class="sound" onclick="s3()">&nbsp</button>
        </div>
      </div>

    </div>

    <footer class="text-center">
      Made by <a href="https://www.freecodecamp.com/radekjohn" target="_blank">Radek John</a>.
    </footer>
  </body>
            
          
!
            
              body {
    text-align: center;
    background-color: #252525;
    color: #DADADA;
    font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
}
a {
    color: #DADADA;
    text-decoration: underline;
}
a:hover {
    color: #252525;
    background: #DADADA;
    text-decoration: underline;
}
button:focus {
    outline: 0;
}
footer {
    margin-top: 10ex;
}
#roundIndicator {
    font-size: 300%;
    color: #DADADA;
}
#b1 {
    background-color: blue;
    border-top-left-radius: 100%;
}
#b2 {
    background-color: yellow;
    border-bottom-left-radius: 100%;
}
#b3 {
    background-color: green;
    border-bottom-right-radius: 100%;
}
#b4 {
    background-color: red;
    border-top-right-radius: 100%;
}
#b1:active {
    background-color: #3333FF;
}
#b2:active {
    background-color: #FFFF64;
}
#b3:active {
    background-color: #2CB32C;
}
#b4:active {
    background-color: #FF6464;
}
.controls button {
    border: none;
    color: white;
    font-size: 105%;
    padding: 1% 2%;
    text-align: center;
    text-decoration: none;
    display: inline-block;
    margin-bottom: 5%;
    cursor: pointer;
}
#switchButton {
    background-color: blue;
}
#startButton {
    background-color: yellow;
    color: blue;
}
#resetButton {
    background-color: green;
}
#strictButton {
    background-color: red;
}
#switchButton:active {
    background-color: #3333FF;
}
#startButton:active {
    background-color: #FFFF64;
}
#resetButton:active {
    background-color: #2CB32C;
}
#strictButton:active {
    background-color: #FF6464;
}
.disabled {
    opacity: 0.6;
    cursor: not-allowed;
}
.buttons {
    font-size: 0;
}
.brow button {
    height: 25ex;
    width: 25ex;
    margin: 0px;
    border: 0px;
    padding: 0px;
}
            
          
!
            
              // Length: 410 ms 
var sound1 = new Audio("https://s3.amazonaws.com/freecodecamp/simonSound1.mp3");
sound1.id = "1";
var sound2 = new Audio("https://s3.amazonaws.com/freecodecamp/simonSound2.mp3");
sound2.id = "2";
var sound3 = new Audio("https://s3.amazonaws.com/freecodecamp/simonSound3.mp3");
sound3.id = "3";
var sound4 = new Audio("https://s3.amazonaws.com/freecodecamp/simonSound4.mp3");
sound4.id = "4";
// This one is longer but no need to measure it. 
var sound5 = new Audio("http://soundjax.com/reddo/79133%5Ebuzzer.mp3");
sound5.id = "5";
// Game state variables
var stimuli = [];
var responses = [];
var strictState = false; // Strict game mode 
var turn = "computer"; // Who's on the move 
var round = 0;
var playerClicks = -1;
var switcherClicks = 0;
var switchState = "off"; // Is the game on?
var error = false; // Did the player make a mistake?
var winner = false; // Was victory achieved?
// For the stimuli player 
var iterator = stimuli[Symbol.iterator]();
var stimuliMap = {
	1: sound1,
	2: sound2,
	3: sound3,
	4: sound4
};
var value, done;

// At first, turn everything off
document.getElementById("startButton").disabled = true;
document.getElementById("resetButton").disabled = true;
document.getElementById("strictButton").disabled = true;
document.getElementById("b1").disabled = true;
document.getElementById("b2").disabled = true;
document.getElementById("b3").disabled = true;
document.getElementById("b4").disabled = true;

// Turn game on or off
function switcher() {
	++switcherClicks;
	console.log(switcherClicks % 2);
	if (switcherClicks % 2 !== 0) {
		document.getElementById("switchButton").innerHTML = "turn off";

		document.getElementById("startButton").disabled = false;
		document.getElementById("resetButton").disabled = false;
		document.getElementById("strictButton").disabled = false;

		document.getElementById("startButton").classList.remove("disabled");
		document.getElementById("resetButton").classList.remove("disabled");
		document.getElementById("strictButton").classList.remove("disabled");

		document.getElementById("b1").disabled = false;
		document.getElementById("b2").disabled = false;
		document.getElementById("b3").disabled = false;
		document.getElementById("b4").disabled = false;
	} else {
		reset();
	}
}
// Start game
function start() {
	document.getElementById("roundIndicator").innerHTML = round;
	computerTurn();
}

function computerTurn() {
	if (winner == false) {
		timer = 0;
		turn = "computer";
		if (error == false) {
			++round;
		}
		playerClicks = -1;
		responses = [];
		stimGen();
		playStimuli();
		document.getElementById("roundIndicator").innerHTML = round;
		error = false;
	}
}
// Generate stimuli
function stimGen() {
	if (error == false) {
		stimuli.push(Math.floor(Math.random() * 4) + 1);
	}
}
// Player reaction
function clicker(par) {
	++playerClicks;
	responses.push(par);
}
// Continue playing or call the victory sequence?
function endCheck() {
	setTimeout(restoreColors, 410);
	if (responses.length == stimuli.length) {
		setTimeout(computerTurn, 1500);
		if (responses.length == 20) {
			winner = true;
			victory();
		}
	}
}
// Turn on strict mode
function strict() {
	strictState = true;
}

// After players error …
function badMove() {
	sound5.play();
	if (strictState == false) {
		setTimeout(computerTurn, 1500);
		// In strict mode, end game
	} else {
		setTimeout(reset, 500);
		alert("Game over.");
	}
	error = true;
}
// Clicking on the sectors of the circle
function s1() {
	clicker(1);
	if (responses[playerClicks] === stimuli[playerClicks]) {
		sound1.play();
		document.getElementById("b1").style.backgroundColor = "#4949FC";
		endCheck();
	} else {
		document.getElementById("b1").style.backgroundColor = "#010195";
		badMove();
	}
}

function s2() {
	clicker(2);
	if (responses[playerClicks] === stimuli[playerClicks]) {
		sound2.play();
		document.getElementById("b2").style.backgroundColor = "#FFFF64";
		endCheck();
	} else {
		document.getElementById("b2").style.backgroundColor = "#9B9B00";
		badMove();
	}
}

function s3() {
	clicker(3);
	if (responses[playerClicks] === stimuli[playerClicks]) {
		sound3.play();
		document.getElementById("b3").style.backgroundColor = "#2CB32C";
		endCheck();
	} else {
		document.getElementById("b3").style.backgroundColor = "#004100";
		badMove();
	}
}

function s4() {
	clicker(4);
	if (responses[playerClicks] === stimuli[playerClicks]) {
		sound4.play();
		document.getElementById("b4").style.backgroundColor = "#FF6464";
		endCheck();
	} else {
		document.getElementById("b4").style.backgroundColor = "#9B0000";
		badMove();
	}
}

// Stimuli player. I used modified function by some guy called wastex (https://stackoverflow.com/questions/44445325/play-sounds-one-after-another-with-loop/44446063#44446063)
function playStimuli() {
	rotate(iterator.next()); // first call

	function rotate({
		value,
		done
	}) {
		// get next iteration

		function next(e) {
			e.target.removeEventListener("ended", next); // helps for garbage removal I guess
			rotate(iterator.next());
		}

		if (done) {
			turn = "player";
			iterator = stimuli[Symbol.iterator]();
			document.getElementById("b1").style.backgroundColor = "blue";
			document.getElementById("b2").style.backgroundColor = "yellow";
			document.getElementById("b3").style.backgroundColor = "green";
			document.getElementById("b4").style.backgroundColor = "red";
			return;
		}
		var current = stimuliMap[value];
		current.play();
		if (current == sound1) {
			blueLighter();
		} else if (current == sound2) {
			yellowLighter();
		} else if (current == sound3) {
			greenLighter();
		} else {
			redLighter();
		}
		current.addEventListener("ended", next);
	}
}

function blueLighter() {
	setTimeout(
		(changeColors = function() {
			document.getElementById("b1").style.backgroundColor = "#4949FC";
			document.getElementById("b2").style.backgroundColor = "yellow";
			document.getElementById("b3").style.backgroundColor = "green";
			document.getElementById("b4").style.backgroundColor = "red";
		}),
		410
	);
	restoreColors();
}
// Lighting buttons
function yellowLighter() {
	setTimeout(
		(changeColors = function() {
			document.getElementById("b1").style.backgroundColor = "blue";
			document.getElementById("b2").style.backgroundColor = "#FFFF64";
			document.getElementById("b3").style.backgroundColor = "green";
			document.getElementById("b4").style.backgroundColor = "red";
		}),
		410
	);
	restoreColors();
}

function greenLighter() {
	setTimeout(
		(changeColors = function() {
			document.getElementById("b1").style.backgroundColor = "blue";
			document.getElementById("b2").style.backgroundColor = "yellow";
			document.getElementById("b3").style.backgroundColor = "#2CB32C";
			document.getElementById("b4").style.backgroundColor = "red";
		}),
		410
	);
	restoreColors();
}

function redLighter() {
	setTimeout(
		(changeColors = function() {
			document.getElementById("b1").style.backgroundColor = "blue";
			document.getElementById("b2").style.backgroundColor = "yellow";
			document.getElementById("b3").style.backgroundColor = "green";
			document.getElementById("b4").style.backgroundColor = "#FF6464";
		}),
		410
	);
	restoreColors();
}

function restoreColors() {
	document.getElementById("b1").style.backgroundColor = "blue";
	document.getElementById("b2").style.backgroundColor = "yellow";
	document.getElementById("b3").style.backgroundColor = "green";
	document.getElementById("b4").style.backgroundColor = "red";
}

function reset() {
	window.location.reload(true);
}
// Victory sequence
function victory() {
	setTimeout(
		(changeColor = function() {
			sound1.play();
			document.getElementById("b1").style.backgroundColor = "#4949FC";
		}),
		410
	);
	setTimeout(
		(changeColor = function() {
			sound2.play();
			document.getElementById("b1").style.backgroundColor = "blue";
			document.getElementById("b2").style.backgroundColor = "#FFFF64";
		}),
		820
	);
	setTimeout(
		(changeColor = function() {
			sound3.play();
			document.getElementById("b2").style.backgroundColor = "yellow";
			document.getElementById("b3").style.backgroundColor = "#2CB32C";
		}),
		1230
	);
	setTimeout(
		(changeColor = function() {
			sound4.play();
			document.getElementById("b3").style.backgroundColor = "green";
			document.getElementById("b4").style.backgroundColor = "#FF6464";
		}),
		1640
	);
	setTimeout(
		(changeColor = function() {
			sound1.play();
			document.getElementById("b4").style.backgroundColor = "red";
			document.getElementById("b1").style.backgroundColor = "#4949FC";
		}),
		2050
	);
	setTimeout(
		(changeColor = function() {
			sound2.play();
			document.getElementById("b1").style.backgroundColor = "blue";
			document.getElementById("b2").style.backgroundColor = "#FFFF64";
		}),
		2460
	);
	setTimeout(
		(changeColor = function() {
			sound3.play();
			document.getElementById("b2").style.backgroundColor = "yellow";
			document.getElementById("b3").style.backgroundColor = "#2CB32C";
		}),
		2870
	);
	setTimeout(
		(changeColor = function() {
			sound4.play();
			document.getElementById("b3").style.backgroundColor = "green";
			document.getElementById("b4").style.backgroundColor = "#FF6464";
		}),
		3280
	);
	setTimeout(
		(changeColor = function() {
			sound1.play();
			document.getElementById("b4").style.backgroundColor = "red";
			document.getElementById("b1").style.backgroundColor = "#4949FC";
		}),
		3690
	);
	setTimeout(
		(changeColor = function() {
			sound2.play();
			document.getElementById("b1").style.backgroundColor = "blue";
			document.getElementById("b2").style.backgroundColor = "#FFFF64";
		}),
		4100
	);
	setTimeout(
		(changeColor = function() {
			sound3.play();
			document.getElementById("b2").style.backgroundColor = "yellow";
			document.getElementById("b3").style.backgroundColor = "#2CB32C";
		}),
		4510
	);
	setTimeout(
		(changeColor = function() {
			sound4.play();
			document.getElementById("b3").style.backgroundColor = "green";
			document.getElementById("b4").style.backgroundColor = "#FF6464";
		}),
		4920
	);
	setTimeout(
		(end = function() {
			alert("You won!");
			reset();
		}),
		5330
	);
}
            
          
!
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.
Loading ..................

Console