$(document).ready(function(){
var button = $(".buttons");
var redbtn = $("#red");
var bluebtn = $("#blue");
var greenbtn = $("#green");
var yellowbtn = $("#yellow");
var allButtons = [redbtn, bluebtn, greenbtn, yellowbtn];
var sequence, playerTurn, wins, strict, seqPosition;
startGame();
function startGame() {
console.log("Starting game...");
strict = confirm("Would you like to play the difficult mode?");
sequence = [];
playerTurn = false;
wins = 0;
computerTurn();
}
function computerTurn() {
seqPosition = 0; // position of player in sequence
$("#count").html(wins);
console.log("It's the computer's turn");
if (wins === 20) {
alert("You win!");
startGame();
} else {
sequence.push(Math.floor(Math.random() * 4));
console.log("Picked new button");
playSequence();
}
}
function playSequence() {
console.log("Playing the sequence... " + sequence.toString());
playSound(allButtons[sequence[0]]);
var i = 0
var progress = false;
(function loop(){
if(i++ == sequence.length) return;
setTimeout(function(){
playSound(allButtons[sequence[i]])
loop();
},1000);
})();
playerTurn = true;
}
function playSound(btn) {
btn.toggleClass("act");
console.log("Playing the sound...");
var link;
switch (parseInt(btn.attr("val"))) {
case 0:
link = "https://s3.amazonaws.com/freecodecamp/simonSound1.mp3";
break;
case 1:
link = "https://s3.amazonaws.com/freecodecamp/simonSound2.mp3";
break;
case 2:
...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Fiddle Pages
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Get Fiddle Pages with PRO
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.