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.

            
              
	<body>
		<h1>Simon Game</h1>
		<h1 id="won"></h1>
		<p id="count" class="blink">Count : </p>
		<p id="strict_state"></p>
		<div class="colors">
			<p id="red">
				<audio id="redSound" src="https://s3.amazonaws.com/freecodecamp/simonSound1.mp3"></audio>
			</p>
			<p id="blue">
				<audio id="blueSound" src="https://s3.amazonaws.com/freecodecamp/simonSound4.mp3"></audio>
			</p>
			<p id="yellow">
				<audio id="yellowSound" src="https://s3.amazonaws.com/freecodecamp/simonSound2.mp3"></audio>
			</p>
			<p id="green">
				<audio id="greenSound" src="https://s3.amazonaws.com/freecodecamp/simonSound3.mp3">
			</p>
		</div>
		<div class="game_btn">
			<button id="start">Start</button>
			<button id="strict">Strict Mode</button>
		</div>


	</body>


            
          
!
            
              #red{
	margin: 0;
	float:left;
	width: 200px;
	height: 100px;
	background-color: red;
}
#blue{
	margin: 0;
	float:left;
	width: 200px;
	height: 100px;
	background-color: blue;
	
}
#yellow{
	margin: 0;
	float:left;
	width: 200px;
	height: 100px;
	background-color: yellow;
	
}
#green{
	margin: 0;
	float:left;
	width: 200px;
	height: 100px;
	background-color: green;
}

.colors{
	width: 420px;
	height: 220px;
	border: 5px solid black;
	max-width: 50%;
	margin: 20px auto;
}

.colors p {
	border: 5px solid black;
}

body{
	text-align: center;
	background-color: pink;
}

#count{
	font-size: 25px;
	font-weight: bold;
}


            
          
!
            
              var soundRed = 'https://s3.amazonaws.com/freecodecamp/simonSound1.mp3';
var soundYellow = 'https://s3.amazonaws.com/freecodecamp/simonSound2.mp3';
var soundGreen = 'https://s3.amazonaws.com/freecodecamp/simonSound3.mp3';
var soundBlue = "https://s3.amazonaws.com/freecodecamp/simonSound4.mp3";


var game_start = false;
var strict = false;
var isWaitingForClick = false;

var red = $('#red');
var blue = $('#blue');
var yellow = $('#yellow');
var green = $('#green');

var redSound = $('#redSound');
var blueSound = $('#blueSound');
var yellowSound = $('#yellowSound');
var greenSound = $("#greenSound");

var pattern = [];
var player_pattern = [];
var count = 0;

displayTurn();
$("#strict_state").html('Strict Mode is OFF');
$('body').css('background-color', 'darkgrey');

function getRandomNumber(){
	let number = Math.floor(Math.random() * (5-1)) +1;
	count += 1;
	displayTurn();
	return number;
}

function addColorToPattern(number){
	if(number == 1){
		pattern.push('red');
	}else if(number == 2){
		pattern.push('blue');
	}else if(number == 3){
		pattern.push('yellow');
	}else if(number == 4){
		pattern.push('green');
	}
	if(pattern.length == 21){
		$('body').css('background-color', 'lightblue');
		$('#won').html('Congrats, you won !!!');
	}
}

function displayTurn(){
	$('#count').html('Count: ' + count);
}


function playPattern(){
	setTimeout(function(){
		for(let i = 0; i < pattern.length; i++){
		if(pattern[i] == 'red'){
			setTimeout(function(){
				red.css('background-color', '#E55451');
				redSound.get(0).play();
			}, 1500 * i);
			setTimeout(function(){
				red.css('background-color', 'red');
			},1500*i + 500);
			
		}else if(pattern[i] == 'blue'){
			setTimeout(function(){
				blue.css('background-color', '#728FCE');
			blueSound.get(0).play();
		}, 1500 * i );
			
			setTimeout(function(){
				blue.css('background-color', 'blue');
			},1500 * i + 500);
		}else if(pattern[i] == 'yellow'){
			setTimeout(function(){
				yellow.css('background-color', '#FFF380');
				yellowSound.get(0).play();
			}, 1500 * i);

			setTimeout(function(){
				yellow.css('background-color', 'yellow');
			},1500* i + 500);
		}else if(pattern[i] == "green"){
			setTimeout(function(){
				green.css('background-color', '#4CC417');
				greenSound.get(0).play();
			},1500 * i);

			setTimeout(function(){
				green.css('background-color', 'green');
			},1500 * i + 500);
		}
	}
	isWaitingForClick = true;
	}, 500);
	
}

function comparePlayerClickToPattern(){
	let n = player_pattern.length - 1;
	if(pattern[n] == player_pattern[n]){
		return true;
	}else{
		return false;
	}
}

function blinkCount(){
	$('.blink').css('color', 'red');
		$(".blink").animate({opacity:0},200,"linear",function(){
		  $(this).animate({opacity:1},200, "linear", function(){
		  	$(this).animate({opacity:0},200, "linear", function(){
		  		$(this).animate({opacity:1},200, "linear", function(){
		  	});
		  	});
		  });
		});
		setTimeout(function(){
			$('.blink').css('color', 'black')}, 900);
	}


function loseConditions(){
	if(strict == true){
		blinkCount();
		pattern = [];
		count = 0;
		displayTurn();
		player_pattern = [];
		isWaitingForClick = false;
	}else if(strict == false) {
		blinkCount();
		player_pattern = [];	
}
}

function newTurn(){
	setTimeout(function(){
	let number = getRandomNumber();
	addColorToPattern(number);
	playPattern();	
	}, 1000);
	
}


red.on('mousedown', function(){
	if(isWaitingForClick  && game_start){
		$(this).css('background-color', '#E55451');
		redSound.get(0).play();
		$(this).on('mouseup', function(){
			$(this).css('background-color', 'red');
		});
		player_pattern.push('red');
		let isColorGood = comparePlayerClickToPattern();
		if(!isColorGood){
			loseConditions();
		}
		if(player_pattern.length == pattern.length){
			isWaitingForClick = false;
			player_pattern = [];
			newTurn();
		}
	}
	
});

blue.on('mousedown', function(){
	if(isWaitingForClick && game_start){
		$(this).css('background-color', '#728FCE');
		blueSound.get(0).play();
		$(this).on('mouseup', function(){
			$(this).css('background-color', 'blue');
		});
		player_pattern.push('blue');
		let isColorGood = comparePlayerClickToPattern();
		if(!isColorGood){
			loseConditions();
		}

		if(player_pattern.length == pattern.length){
			isWaitingForClick = false;
			player_pattern = [];
			newTurn();
		}
	}
});

green.on('mousedown', function(){
	if(isWaitingForClick && game_start){
		$(this).css('background-color', '#4CC417');
		greenSound.get(0).play();
		$(this).on('mouseup', function(){
			$(this).css('background-color', 'green');
		});
		player_pattern.push('green');
		let isColorGood = comparePlayerClickToPattern();
		if(!isColorGood){
			loseConditions();
		}
		if(player_pattern.length == pattern.length){
			isWaitingForClick = false;
			player_pattern = [];
			newTurn();
		}
	}
});

yellow.on('mousedown', function(){
	if(isWaitingForClick  && game_start){
		$(this).css('background-color', '#FFF380');
		yellowSound.get(0).play();
		$(this).on('mouseup', function(){
			$(this).css('background-color', 'yellow');
		});
		player_pattern.push('yellow');
		let isColorGood = comparePlayerClickToPattern();
		if(!isColorGood){
			loseConditions();
		}

		if(player_pattern.length == pattern.length){
			isWaitingForClick = false;
			player_pattern = [];
			newTurn();
		}
	}
});




//game states

$('#start').click(function(){
	if(game_start){
		game_start = false;
		$('body').css('background-color', 'darkgrey');
		pattern = [];
		player_pattern = [];
		count = 0;
		displayTurn();
		isWaitingForClick = false;	
	}else if(!game_start){
		game_start = true;
		player_pattern = [];
		$('body').css('background-color', 'pink');
		let num = getRandomNumber();
		addColorToPattern(num);
		playPattern();
		isWaitingForClick = true;
	}
});

$("#strict").click(function(){
	if(strict == false){
		strict = true;
		$("#strict_state").html('Strict Mode is ON');
	}else if(strict == true){
		strict = false;
		$("#strict_state").html('Strict Mode is OFF');
	}
});


            
          
!
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