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.

Quick-add: + add another resource

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.

Quick-add: + add another resource

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.

            
              <!DOCTYPE html>

<html lang="en">
<head>
    <meta charset="utf-8">
    <meta content="IE=edge" http-equiv="X-UA-Compatible">
    <meta content="width=device-width, initial-scale=1 , maximum-scale=1, user-scalable=0" name="viewport">
   
    <title>Simon Game</title>

    <link href="main.css" rel="stylesheet">
    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
   <link href="https://fonts.googleapis.com/css?family=Bungee|Bungee+Shade" rel="stylesheet">
</head>

<body>

<div class = "game-case">hello again
	<div id = "green" class = "green button"></div>
	<div id = "red" class = "red button"> </div>
	<div id = "yellow" class = "yellow button"> </div>
	<div id = "blue" class = "blue button"> </div>
	<div class = "center">
		<div class = "simon">Simon</div>
		<div class = "button-row">
			<div class = "button-item">
				<div id = "counter-screen" class = "count"></div>
				Count </div>
			<div class = "button-item">
				<div id = "start" class = "rnd-btn start"></div>
				Start </div>
			<div class = "two-stacked button-item ">
				<div id = "indicator" class = "indicator"></div>
				<div id = "strict" class = "rnd-btn strict"></div>
				Strict 
			</div>
		</div>
		<div class = "on-off">
			<span>on</span>
			<div class = "outer-switch">
				<div id = "inner-switch" class="inner-switch"></div>
			</div>
			<span>off</span</div>
	</div>
</div>
<script src="app.js"></script>
</body>
</html>
            
          
!
            
              .game-case{
	height: 500px;
	width: 500px;
	display: inline-block;
	margin: 0px;
	font-size: 0px;
	position: absolute;
	top: 50%;
	left: 50%;
	margin-right: -50%;
	transform: translate(-50%, -50%);
	border-radius: 100%;
	
}
.button{
	height: 210px;
	width: 210px;
	display: inline-block;
	border: 20px solid black;
	margin: 0px;
	cursor: pointer;
}
.green{
	background: #1F662A;
	border-top-left-radius: 100%;
}
.red{
	background: #7F1818;
	border-top-right-radius: 100%;
}
.yellow{
	background: #E5CE33;
	border-bottom-left-radius: 100%;
}
.blue{
	background: #1D25AB;
	border-bottom-right-radius: 100%;
}
.center{
	font-size: 14px;
	position: absolute;
	top: 25%;
	left: 25%;
	border-radius: 100%;
	width: 250px;
	height: 250px;
	background: white;
	z-index: 2;
	border: 4px solid black;
	
	
}
.simon{
	z-index: 3;
	font-size: 36px;
	font-weight: 800;
	text-align: center;
	padding: 5px 0px 0px 5px;
	margin: 5px 5px 0px 25px;
	font-family: 'Bungee Shade', cursive;
	max-width: 70%;
	border-radius: 45%;
}
.button-item{
	height: 30px;
	width: 28%;
	display: inline-block;
	margin: 11px 2px 2px 2px;
}
.on-off{
	display: inline-block;
}
.outer-switch{
	width : 50%;
	height: 30px;
	display: inline-block;
}
.count{
	background: #280a03;
	height: 40px;
	width: 40px;
	margin: 0px 4px 0px 4px;
	font-family: 'Bungee', cursive;
	color: red;
	text-align: center;
}
.rnd-btn{
	height: 14px;
	width: 14px;
	border-radius: 100%;
	margin: 10px;
	padding: 3px 0px 0px 0px;
	border: 2px solid black;
	cursor: pointer;
}
.indicator{
	height: 5px;
	width: 5px;
	background: #280a03;
	border-radius: 100%;
	margin-left: 17px;
}
.button-row{
	padding: 0px 2px 1px 12px;
	font-family: 'Bungee', cursive;
	border-radius: 45%;
}
.strict{
	background: yellow;
}
.start{
	background: red;
}
.on-off{
	padding: 5px 0px 0px 0px;
	margin-left: 33%;
	font-family: 'Bungee', cursive;

}
.outer-switch{
	height: 20px;
	width: 40px;
	background: gray;
	border: 2px solid black;
}
.inner-switch{
	height: 17px;
	width: 17px;
	background: dodgerblue;
	float: right;
	cursor: pointer;
	}
            
          
!
            
              var	redSound = new Audio ('https://s3.amazonaws.com/freecodecamp/simonSound1.mp3');
var	blueSound = new Audio ('https://s3.amazonaws.com/freecodecamp/simonSound2.mp3');
var	yellowSound = new Audio ('https://s3.amazonaws.com/freecodecamp/simonSound3.mp3');
var	greenSound = new Audio ('https://s3.amazonaws.com/freecodecamp/simonSound4.mp3');
var liteColor = {
	red : {dark: "#7F1818",
			lit: "#c22525",
			sound: redSound},
	yellow: {dark: "#E5CE33",
			lit: "#F7F69E",
			sound: yellowSound},
	blue: {dark: "#1D25AB",
			lit: "#328EE0",
			sound: blueSound},
	green: {dark: "#1F662A",
			lit: "#1FA22A",
			sound: greenSound}			
};

var sequence = [];
var count;// says how many at each stage.  goes up to 20, set this max in the initialize function
var timetest;
var userSequence = [];
var strict = false;
var gameOn = false;
var isWon = false;
var lightsClickable = false;
var timesUp;

function lights(sequence){ //input array, allowed vals are 'red', 'blue', 'yellow', 'green' html must use those ids and colors

	function light(color){
	
		var panel = document.getElementById(color);
		liteColor[color].sound.play();
		panel.style.background = liteColor[color].lit;
		timetest = setTimeout(function(){panel.style.background = liteColor[color].dark;},800);
	}
	
	for (item in sequence){
		(function(item){//not the same item, this is a closure
			setTimeout(function(){
				light(sequence[item]);
			},(item +1)*200);// Probably not best practice, works on my pc, but others?
		})(item);
	}
}

function makeSequence(num){// returns an array of color names with length = num, make sequence of 20 for each new game
	var color,
		rnd,
		sequence = [],
		colors = (['red', 'blue', 'yellow', 'green']);
	for (var i = 0; i < num; i++){
		rnd = Math.floor(Math.random()*4);
		sequence.push(colors[rnd]);
	}
	console.log(sequence);
	return sequence;
}
function initializeGame(){
	sequence = makeSequence(20);//TODO change to 20 after done testing
	count = 1;  //
	userSequence = [];
	isWon = false;
	drawGame();

}
function playPattern(howMany){
	lightsClickable = false;//make user wait until finished
	userSequence = [];
	var partSequence = sequence.slice(0, howMany);
	lights(partSequence);
	console.log('playing Pattern');
	lightsClickable = true;//now is user's turn to click lights
	//TODO  MAKE THE GAME ERROR WHEN USER TAKES TOO LONG -- MAYBE after a timeout, 111push 'wrong' to user sequence and call compare function
	clearInterval(timesUp);//Try here to stop multiple instances in strict mode
	isTimeUp();
}
function isTimeUp(){
	if (gameOn){
		timesUp = setTimeout(function(){
			console.log('times up, count is: ' + count);
			userFails();
			}
		,(7000 + 1000 * count)
	
	);}
}
		
function compareUserSequence(clickedButton){
	clearInterval(timesUp);
	userSequence.push(clickedButton);
	console.log(userSequence);
	//console.log(userSequence.join(',') + ' ' + sequence.slice(0,count).join(','));
	//if ((userSequence[userSequence.length - 1] === (sequence[userSequence.length - 1]))){
	
	if (userSequence.join(',') === sequence.slice(0,count).join(',')){
		console.log(userSequence);
		console.log('good choice ' +  userSequence  + ' vs ' + sequence.slice(0,count));
		count = count + 1;
		drawGame();
		console.log('count = ' + count);
		if (!gameOn){
			console.log('game is off')
			return; //stop function if user has turned off the game
		}
		if (count === 21){//TODO change 4 to 21 when done testing
			console.log('winner - pattern matched');
			isWon = true;
			lightsClickable = false;
			drawGame();
			return;
		}
		(function(){
			setTimeout(function(){
				playPattern(count);
			}, 1000);
		})();

	} else {
		//console.log('wrong color ' + userSequence + ' vs ' + sequence.slice(0,count));//+ userSequence.join(',') === sequence.slice(0,count).join(',')}
		if (clickedButton === sequence[userSequence.length - 1]){//tests portion of full pattern is matched with each move
			console.log('okydoky');
		}else{
			userFails();
		};
	};
}

function userFails(){
	//make a horrible cacophony of light and sound, then play correct sequence again
	console.log('wrong button');
	lights(['red']);
	lights(['yellow']);
	lights(['blue']);
	lights(['green']);
	clearTimeout(timesUp);

	(function(){  //play pattern after error flashing lights end
		setTimeout(function(){
			playPattern(count);
		}, 1000);
	})(); 
	//if in strict mode, start a new pattern
	if (strict){
		clearTimeout(timesUp);
		initializeGame();
			(function(){
				setTimeout(function(){
					playPattern(count);
			}, 1000);
		})(); 
	}
		
}

document.getElementById("red").addEventListener("click", function(event){
	if (lightsClickable){
		event.preventDefault(); // prevents resize on iphone dblclick
		lights(['red']);
		compareUserSequence('red');
	}
});

document.getElementById("blue").addEventListener("click", function(event){
	if (lightsClickable){
		event.preventDefault(); // prevents resize on iphone dblclick
		lights(['blue']);
		compareUserSequence('blue');
	}
});

document.getElementById("yellow").addEventListener("click", function(event){
	if (lightsClickable){
		event.preventDefault(); // prevents resize on iphone dblclick
		lights(['yellow']);
		compareUserSequence('yellow');
	}
});

document.getElementById("green").addEventListener("click", function(event){
	if (lightsClickable){
		event.preventDefault(); // prevents resize on iphone dblclick
		lights(['green']);
		compareUserSequence('green');
	}
});

document.getElementById("start").addEventListener("click", function(event){//start
	if (gameOn){
		event.preventDefault(); // prevents resize on iphone dblclick LOL - doubt this works on a phone that is a leftovr from other thing
		initializeGame();
		playPattern(count);
	}
});
	
document.getElementById("strict").addEventListener("click", function(event){//strict
	if (gameOn){
		event.preventDefault(); // prevents resize on iphone dblclick
		strict =! strict;
		drawGame();
	}
});

document.getElementById("inner-switch").addEventListener("click", function(event){//on-off
	event.preventDefault(); // prevents resize on iphone dblclick
	gameOn =!gameOn;
	count = 0;
	sequence = [];
	isWon = false;
	clearInterval(timesUp);
	drawGame();
});
		
function drawGame(){

	if (isWon === true){
		document.getElementById("counter-screen").innerHTML = 'WON';
	}
	else if (count) {
		document.getElementById("counter-screen").innerHTML = count;
	}
	
	if (gameOn){
		document.getElementById("inner-switch").style.float = 'left';
	} else{
		document.getElementById("inner-switch").style.float = 'right';
		document.getElementById("counter-screen").style.background = '#280a03';
		document.getElementById("counter-screen").innerHTML = '';
		document.getElementById("indicator").style.background = '#280a03';// dark like unlit indicator
		
	}
	if (strict && gameOn){
		document.getElementById("indicator").style.background = 'red';// like an indicator light
	} else{
	document.getElementById("indicator").style.background = '#280a03';// dark like unlit indicator
	}
	if (!count && gameOn){
		document.getElementById("counter-screen").innerHTML = '- -';
	}
}
            
          
!
999px
Loading ..................

Console