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 id="container">
	<div id="green" class="quarter green" onmousedown="display.playerDown('green')" onmouseup="display.playerUp('green')" onclick="game.player(this)">
	</div>
	<div id="red" class="quarter red" onmousedown="display.playerDown('red')" onmouseup="display.playerUp('red')" onclick="game.player(this)">
	</div>
	<div id="yellow" class="quarter yellow" onmousedown="display.playerDown('yellow')" onmouseup="display.playerUp('yellow')" onclick="game.player(this)">
	</div>
	<div id="blue" class="quarter blue" onmousedown="display.playerDown('blue')" onmouseup="display.playerUp('blue')" onclick="game.player(this)">
	</div>
	<div id="centerCircle">
		<h2>Simon</h2>
		<div id="count">
			<div id="countDisplay">
				<p id="display">- -</p>
			</div>
		</div>
		<div id="start" onclick="game.startGame()">
		</div>
		<div id="strict" onclick="display.strict()"></div>
		<div id="strictOn">
		</div>
		<div id="content">
			<p>Count</p>
			<p>Start</p>
			<p>Strict</p>
		</div>
		<div id="power" onclick="display.powerOn()">
			<div id="powerSwitch">
			</div>
			<p>OFF</p>
			<p>ON</p>
		</div>
	</div>
</div>
<audio id="greenAudio">
  
  <source src="https://s3.amazonaws.com/freecodecamp/simonSound1.mp3" type="audio/mpeg">
  Your browser does not support the audio element.
</audio>
<audio id="redAudio">
  
  <source src="https://s3.amazonaws.com/freecodecamp/simonSound2.mp3" type="audio/mpeg">
  Your browser does not support the audio element.
</audio>
<audio id="yellowAudio">
  
  <source src="https://s3.amazonaws.com/freecodecamp/simonSound3.mp3" type="audio/mpeg">
  Your browser does not support the audio element.
</audio>
<audio id="blueAudio">
  
  <source src="https://s3.amazonaws.com/freecodecamp/simonSound4.mp3" type="audio/mpeg">
  Your browser does not support the audio element.
</audio>

            
          
!
            
              @import url(https://fonts.googleapis.com/css?family=Patrick+Hand);
@import url(https://fonts.googleapis.com/css?family=Iceberg);

body {
	background-color: #deb887;
}

#container {
	margin: 0 auto;
	height: 500px;
	width: 500px;
	position: relative;
	background-color: #403E3B;
	border-radius: 50%;
}
.quarter {
	height: 220px;
	width: 220px;
}
#centerCircle {
	height: 250px;
	width: 250px;
	background-color: #ffffff;
	border: 15px solid #403E3B;	
	border-radius: 50%;
	position: relative;
	z-index: 10;
	top: 50%;
	left: 50%;
	transform: translate(-50%, -50%);	
}
#centerCircle h2 {
	position: absolute;    
    top: 0%;
    width: 100%;
    text-align: center;  
		font-family: "Patrick Hand";
		font-size: 3em;
}

.green {
	background-color: #239023; 	
	border-top-left-radius: 250px; 
	float: left;
	border: 15px solid #403E3B;	
}

.red {
	background-color: #800000;	
	float: right;	
	border-top-right-radius: 250px; 
	border: 15px solid #403E3B;	
}

.yellow {
	background-color: #b3b300;
	float: left;	
	border-bottom-left-radius: 250px;		border: 15px solid #403E3B;	
}

.blue {
	background-color: #003399;
	float: right;	
	border-bottom-right-radius: 250px;
	border: 15px solid #403E3B;	
}
#count {
	position: absolute;
	height: 40px;
	width: 50px;
	background-color: #330000;
	border: 5px solid #403e3b;
	border-radius: 10px;
	top: 50%;
	left: 10%;
}



#countDisplay {
	position: absolute;
	font-family: "IceBerg";
	font-size: 1.5em;
	top: -40%;
	left: 20%;
	color: #ff4d4d;
	width: 50px;
}

#start {
	position: absolute;
	height: 20px;
	width: 20px;
	border-radius: 20px;
	background-color: red;
	border: 3px solid #403e3b;
	top: 58%;
	left: 45%;
}

#start:active {
	transform: translateY(2px);
}

#strict {
	position: absolute;
	height: 20px;
	width: 20px;
	border-radius: 20px;
	background-color: yellow;
	border: 3px solid #403e3b;
	top: 58%;
	left: 65%;
}
#strict:active {
	transform: translateY(2px);
}

#strictOn {
	position: absolute;
	height: 10px;
	width: 10px;
	border: 1px solid #403e3b;
	background-color: black;
	border-radius: 10px;
	top: 50%;
	left: 67.5%;
}
#content p:nth-child(1){
	position: absolute;
	left: 13%;
	top: 64%;
	}
#content p:nth-child(2){
	position: absolute;
	left: 43%;
	top: 64%;
	}
#content p:nth-child(3){
	position: absolute;
	left:  63%;
	top: 64%;
	}

#power {
	position: absolute;
	height: 20px;
	width: 60px;
	border-radius: 5px;
	background-color: black;
	border: 3px solid #403e3b;
	top: 80%;
	left: 38%;
}

#powerSwitch {
	position: absolute;
	height: 16px;
	width: 25px;
	border-radius: 5px;
	background-color: blue;
	border: 1px solid #403e3b;
	top: 5%;
	left: 1%;
}

#power p:nth-child(2) {
	position: absolute;
	left: -60%;
	top: -70%;
	font-size: .9em;
}

#power p:nth-child(3) {
	position: absolute;
	left: 120%;
	top: -70%;
	font-size: .9em
		
}

            
          
!
            
              var colors = ["green", "red", "yellow", "blue"];
var clrArr = []; // will be array of the 20 different random colors
var power; // game on or off
var gameStarted = false; //trying to make it so player cant do mouse clicks before players turn
var round = 0;
var error = null;
var turnNumbers = document.getElementById("countDisplay");
var displayA = document.getElementById("display");
var powerSwitch = document.getElementById("powerSwitch");
var strictEnabled = "off";
var myVar;
powerSwitch.style.left = "1%";
displayA.style.visibility = "hidden";

var audio = {
	greenAudio: document.getElementById("greenAudio"),
	redAudio: document.getElementById("redAudio"),
	yellowAudio: document.getElementById("yellowAudio"),
	blueAudio: document.getElementById("blueAudio")
}

var display = {
		green: document.getElementById("green"),
		red: document.getElementById("red"),
		yellow: document.getElementById("yellow"),
		blue: document.getElementById("blue"),

		powerOn: function() {
			if (powerSwitch.style.left === "1%") {

				powerSwitch.style.left = "50%";
				displayA.style.visibility = "visible";
				displayA.innerHTML = "- -";
				power = "on";
				clrArr = [];
				cpArr = [];
				playerArr = [];

				this.rotate();

			} else {
				powerSwitch.style.left = "1%";
				displayA.style.visibility = "hidden";
				power = "off";
				clrArr = [];
				playerArr = [];
				round = 0;
				gameStarted = false;
				var highestTimeoutId = setTimeout(";"); // clears out all timeouts.
				for (var i = 0; i < highestTimeoutId; i++) {
					clearTimeout(i);
				}
				//clearTimeout(display.flash);

			}
		},
		strict: function() {
			var strictOn = document.getElementById("strictOn");
			if (power === "on") {
				if (strictEnabled === "off") {
					strictEnabled = "on";
					strictOn.style.backgroundColor = "red";
				} else {
					strictEnabled = "off";
					strictOn.style.backgroundColor = "black";
				}
			}
		},

		rotate: function() { // flahing of all colors when powering on.
			var timeOut;
			this.down('green');
			timeOut = window.setTimeout(this.up, 500, 'green');
			this.down('red');
			timeOut = window.setTimeout(this.up, 500, 'red');
			this.down('yellow');
			timeOut = window.setTimeout(this.up, 500, 'yellow');
			this.down('blue');
			timeOut = window.setTimeout(this.up, 500, 'blue');
		},
		playerDown: function(objColor) {
			if (game.turn === "player") {
				this.down(objColor);
			}
		},

		playerUp: function(objColor) {
			if (game.turn === "player") {
				this.up(objColor);
			}
		},

		down: function(objColor) { // highlight butons
			if (error === null) {
				if (objColor === "green") {
					green.style.backgroundColor = "#5bd75b";
					audio.greenAudio.play();
				}
				if (objColor === "red") {
					red.style.backgroundColor = "#e60000";
					audio.redAudio.play();
				}
				if (objColor === "yellow") {
					yellow.style.backgroundColor = "#ffff66";
					audio.yellowAudio.play();
				}
				if (objColor === "blue") {
					blue.style.backgroundColor = "#3377ff";
					audio.blueAudio.play();
				}
			}
		},
		up: function(objColor) { // set buttons back to original color
			if (error === null) {
				if (objColor === "green") {
					green.style.backgroundColor = "#239023";
				}
				if (objColor === "red") {
					red.style.backgroundColor = "#800000";
				}
				if (objColor === "yellow") {
					yellow.style.backgroundColor = "#b3b300";
				}
				if (objColor === "blue") {
					blue.style.backgroundColor = "#003399";
				}
			}
		},

		flash: function(objColor) { // animate computer button push

			var timeOut;
			this.down(objColor);
			timeOut = window.setTimeout(this.up, 300, objColor);
		},
		incorrect: function() {
			displayA.innerHTML = "! !";
			setTimeout(function() {
				displayA.style.visibility = "hidden";
			}, 100);
			setTimeout(function() {
				displayA.style.visibility = "visible";
			}, 150);
			displayA.innerHTML = "! !";
			setTimeout(function() {
				displayA.style.visibility = "hidden";
			}, 200);
			setTimeout(function() {
				displayA.style.visibility = "visible";
			}, 250);
			displayA.innerHTML = "! !";
			setTimeout(function() {
				displayA.style.visibility = "hidden";
			}, 300);
			setTimeout(function() {
				displayA.style.visibility = "visible";
			}, 350);
			displayA.innerHTML = "! !";
			setTimeout(function() {
				displayA.innerHTML = round;
			}, 700);
		},

		win: function() {
			turnNumbers.style.left = "15%"
			displayA.innerHTML = "WIN";
			setTimeout(function() {
				displayA.style.visibility = "hidden";
			}, 100);
			setTimeout(function() {
				displayA.style.visibility = "visible";
			}, 150);
			displayA.innerHTML = "WIN";
			setTimeout(function() {
				displayA.style.visibility = "hidden";
			}, 200);
			setTimeout(function() {
				displayA.style.visibility = "visible";
			}, 250);
			displayA.innerHTML = "WIN";
			setTimeout(function() {
				displayA.style.visibility = "hidden";
			}, 300);
			setTimeout(function() {
				displayA.style.visibility = "visible";
			}, 350);
			displayA.innerHTML = "WIN";
			setTimeout(function() {
				displayA.innerHTML = round;
			}, 700);
			setTimeout(function() {
				turnNumbers.style.left = "40%"
			}, 700);

		}
	} //end display obj

var game = {
	turn: "computer",
	cpArr: [], // will hold computer button selections
	playerArr: [], // will hold player button selections
	startGame: function() {
		if (power === "on" && gameStarted === false) {
			gameStarted = true;
			this.createArr(); // create array of 20 random colors.
			displayA.innerHTML = round; // show round in the displaybox.
			turnNumbers.style.left = "40%"; // adjust the display to be centered for only one number		
			this.computer(); // start the computer turn
		}
	},
	setPlayerTurn: function() {
		this.turn = "player";
	},
	computer: function() {
		if (round < 20) {
			round++;

			displayA.innerHTML = round;
			this.cpArr = clrArr.slice(0, round); // as round increases cpArr increases also.

			this.iterate();
		}

		//this.setPlayerTurn();
		this.playerArr = []; // clear playerArr in order to check for errors

	},

	player: function(obj) {
		error = null; // allows game to continue after an error by the player.
		if (this.turn === "player") {
			this.playerArr.push(obj.id);
			this.check(this.playerArr, this.cpArr);

			if (error === null) {

				if (this.playerArr.length === round) {
					if (this.playerArr.length === 20) {
						display.rotate();
						display.win();
						round = 0;
						game.turn = "computer";
						setTimeout(function() {
							game.computer();
						}, 2000);
					} else {
						game.turn = "computer";
						setTimeout(function() {
							game.computer();
						}, 2000);
					}
				}
			}
		}
	},

	check: function(arr1, arr2) { // checks if playerArr matches cpArr

		for (var i = 0; i < arr1.length; i++) {
			if (arr1[i] !== arr2[i]) {
				display.incorrect();
				error = true;
				game.turn = "computer";
				this.playerArr = [];
				if (strictEnabled === "on") { // check for strict mode.
					round = 0;
					clrArr = [];
					setTimeout(function() {
						game.startGame();
					}, 2000);
				} else {
					setTimeout(function() {
						game.iterate();
					}, 2000);
				}
			}
		}

	},

	reduction: function() {
		if (round < 9) {
			return 300;
		}
		if (round < 15) {
			return 700;
		} else {
			return 900;
		}

	},

	iterate: function() { // flashes each color from cpArray

		setTimeout(function() {
			game.setPlayerTurn()
		}, 1000 * this.cpArr.length - this.reduction);
		for (var i = 0; i < this.cpArr.length; i++) {
			setTimeout(function(x) {
				return function() {
					if (game.cpArr[x] === "green") {
						display.flash('green');
					}
					if (game.cpArr[x] === "red") {
						display.flash('red');
					}
					if (game.cpArr[x] === "yellow") {
						display.flash('yellow');
					}
					if (game.cpArr[x] === "blue") {
						display.flash('blue');
					}
				}
			}(i), game.speed() * i);
		}
		error = null;

	},
	speed: function() {
		if (round < 9) {
			return 700;
		}
		if (round < 15) {
			return 500;
		} else {
			return 350;
		}
	},

	createArr: function() {
		for (var i = 0; i < 20; i++) {
			clrArr.push(colors[Math.floor(Math.random() * colors.length)])
		}
	}

}
            
          
!
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