|
let soundMP3 = [ |
|
"https://s3.amazonaws.com/freecodecamp/simonSound1.mp3", |
|
"https://s3.amazonaws.com/freecodecamp/simonSound2.mp3", |
|
"https://s3.amazonaws.com/freecodecamp/simonSound3.mp3", |
|
"https://s3.amazonaws.com/freecodecamp/simonSound4.mp3" |
|
]; |
|
let errorMP3 = "http://soundbible.com/grab.php?id=1198&type=mp3"; |
|
let errorSound = null; |
|
let sounds = [null, null, null, null]; |
|
let divIds = ["green-button", "red-button", "blue-button", "yellow-button"]; |
|
let litClasses = [ |
|
"green-lit-button", |
|
"red-lit-button", |
|
"blue-lit-button", |
|
"yellow-lit-button" |
|
]; |
|
let moves = []; |
|
let currMove = -1; |
|
let lastMove = -1; |
|
let replayIndex = 0; |
|
let strict = false; |
|
var f; |
|
const maxMoves = 20; //5; //number of moves after which you win |
|
|
|
function pad(num, size) { |
|
var s = num + ""; |
|
while (s.length < size) s = "0" + s; |
|
return s; |
|
} |
|
|
|
function setLastMove(index) { |
|
lastMove = index; |
|
$(".counter").text(pad(lastMove + 1, 2)); |
|
} |
|
|
|
function getNewRandomMove() { |
|
return Math.floor(Math.random() * 4); |
|
} |
|
|
|
function unlight() { |
|
for (var i = 0; i < 4; i++) $(".button").removeClass(litClasses[i]); |
|
} |
|
|
|
function reset() { |
|
clearTimeout(f); //so user can press reset in the middle of the winning display, or during the PC's move |
|
$(".reset").text("Reset"); |
|
moves.length = 0; |
|
for (var i = 0; i < maxMoves; i++) moves.push(getNewRandomMove()); |
|
setLastMove(-1); |
|
playMoves(); |
|
} |
|
|
|
function buttonPress(index) { |
|
$("#" + divIds[index]).addClass(litClasses[index]); |
|
sounds[index].pause(); |
|
sounds[index].currentTime = 0; |
|
sounds[index].play(); |
|
setTimeout(unlight, 150); |
|
} |
|
|
|
function nextButtonPress() { |
|
buttonPress(moves[replayIndex]); |
|
replayIndex++; |
|
if (replayIndex <= lastMove) f = setTimeout(nextButtonPress, 500); |
|
} |
|
|
|
function nextWinPress() { |
|
buttonPress(replayIndex % 4); |
|
replayIndex++; |
|
if (replayIndex <= 40) f = setTimeout(nextWinPress, 150); |
|
else setTimeout(reset, 500); |
|
} |
|
|
|
function playMoves() { |
|
//console.log(moves); |
|
setLastMove(lastMove + 1); |
|
currMove = 0; |
|
if (lastMove >= maxMoves) { |
|
//play winning tune |
|
//for (var i = 0; i <= 12; i++) { |
|
// setTimeout(function() { |
|
// buttonPress(moves[i % 4]); |
|
// }, i * 160); |
|
// } |
|
// setTimeout(function() { |
|
// reset(); |
|
//}, 13 * 160); |
|
$(".counter").text("You win!"); |
|
replayIndex = 0; |
|
setTimeout(nextWinPress, 400); |
|
} else { |
|
//for (var i = 0; i <= lastMove; i++) { |
|
// var move = moves[i]; |
|
// setTimeout(function() { |
|
// buttonPress(move); |
|
// console.log('move '+move); |
|
// }, i * 500); |
|
//} |
|
replayIndex = 0; |
|
setTimeout(nextButtonPress, 600); |
|
} |
|
} |
|
|
|
function userButtonPress(index) { |
|
//console.log(moves[currMove] + " " + index); |
|
if (currMove < 0) { |
|
//game has not started, just test/practise mode, user can press buttons, and hear sounds |
|
buttonPress(index); |
|
} else if (moves[currMove] == index) { |
|
buttonPress(index); |
|
currMove++; |
|
//console.log(currMove + " " + lastMove); |
|
if (currMove > lastMove) { |
|
playMoves(); |
|
} |
|
} else { |
|
errorSound.play(); |
|
lastMove--; |
|
if (strict) setTimeout(reset, 600); |
|
else setTimeout(playMoves, 500); |
|
} |
|
} |
|
|
|
$(document).ready(function() { |
|
for (var i = 0; i < 4; i++) { |
|
sounds[i] = document.createElement("audio"); |
|
sounds[i].setAttribute("src", soundMP3[i]); |
|
} |
|
errorSound = document.createElement("audio"); |
|
errorSound.setAttribute("src", errorMP3); |
|
|
|
$("#green-button").click(function() { |
|
userButtonPress(0); |
|
}); |
|
|
|
$("#red-button").click(function() { |
|
userButtonPress(1); |
|
}); |
|
|
|
$("#blue-button").click(function() { |
|
userButtonPress(2); |
|
}); |
|
|
|
$("#yellow-button").click(function() { |
|
userButtonPress(3); |
|
}); |
|
|
|
$(".strict").click(function() { |
|
if (strict) { |
|
strict = false; |
|
$(".strict").addClass("off"); |
|
} else { |
|
strict = true; |
|
$(".strict").removeClass("off"); |
|
} |
|
}); |
|
|
|
$(".reset").click(function() { |
|
reset(); |
|
}); |
|
}); |