|
"use strict"; |
|
const WINNING_SCORE = 19; |
|
let isOn = false; |
|
let notePlaying = false; |
|
let isStrict = true; |
|
let gameWon = false; |
|
const notes = ["f", "g", "a", "b"]; |
|
const keyColors = ["tomato", "dodgerblue", "lightseagreen", "goldenrod"]; |
|
let sequence = []; |
|
let guessSequence = []; |
|
let idx = 0; |
|
let guessIdx = 0; |
|
|
|
window.onload = function() { |
|
gameInit(); |
|
}; |
|
|
|
function resetGame() { |
|
sequence = []; |
|
$(".scoreboard").text("!!"); |
|
continueGame(); |
|
} |
|
|
|
function gameInit() { |
|
$(".on-off button").click(function() { |
|
let onOff = $(".on-off button").text(); |
|
if (onOff != "On") { |
|
// Reset game to ready state |
|
$(".on-off button").text("On"); |
|
$(".on-off button").css("background", "lightgreen"); |
|
$('h1').css('color', 'black'); |
|
$('body').css("background-image", "none"); |
|
isOn = true; |
|
gameWon = false; |
|
sequence = []; |
|
addtoSequence(true); // start game so pass true |
|
} else { |
|
$(".on-off button").text("Off"); |
|
$(".on-off button").css({ |
|
background: "gray", |
|
color: "black" |
|
}); |
|
isOn = false; |
|
gameWon = false; |
|
// clear sequence |
|
sequence = []; |
|
$(".scoreboard").text("00"); |
|
} |
|
}); |
|
|
|
$(".strict-mode button").click(function() { |
|
let strictMode = $(".strict-mode").text().trim(); |
|
console.log(strictMode); |
|
if (strictMode === "Strict Mode") { |
|
$(".strict-mode button").text("Easy Mode"); |
|
$(".strict-mode button").css("background", "lightseagreen"); |
|
isStrict = false; |
|
} else { |
|
$(".strict-mode button").text("Strict Mode"); |
|
$(".strict-mode button").css({ |
|
background: "gray", |
|
color: "black" |
|
}); |
|
isStrict = true; |
|
} |
|
|
|
}); |
|
} |
|
|
|
function playSequence(note) { |
|
// for(var i=0; i<sequence.length; i++) { |
|
// const randomNote = notes[randomNum]; |
|
const audio = document.querySelector(`audio[data-key=${note}]`); |
|
audio.play(); |
|
notePlaying = true; |
|
// Work out correct colour |
|
$("#" + note).css("background", keyColors[notes.indexOf(note)]); |
|
idx++; |
|
audio.onended = function() { |
|
audio.currentTime = 0; |
|
$("#" + note).css("background", "none"); |
|
notePlaying = false; |
|
}; |
|
if (idx >= sequence.length) { |
|
guessIdx = 0; |
|
return; |
|
} |
|
// else |
|
setTimeout(() => { |
|
playSequence(sequence[idx]); |
|
}, 1100); |
|
} |
|
|
|
function addtoSequence(reStart) { |
|
if (!notePlaying) { |
|
|
|
const randomNum = Math.floor(Math.random() * notes.length); |
|
sequence.push(notes[randomNum]); |
|
// set the first note of the sequence to be the first index |
|
idx = 0; |
|
// play the whole sequence using this recursive function: |
|
playSequence(sequence[0]); |
|
|
|
let scoreboard = 0; |
|
if (reStart || document.querySelector(".scoreboard").innerText === '!!') |
|
{ |
|
scoreboard = 1; |
|
} else { |
|
scoreboard = Number(document.querySelector(".scoreboard").innerText) + 1; |
|
} |
|
$(".scoreboard").text(scoreboard); |
|
|
|
} |
|
} |
|
|
|
function compareGuess(note) { |
|
console.log("user guessed", note); |
|
|
|
guessSequence.push(note); |
|
console.log("seq:", sequence); |
|
console.log("Guess:", guessSequence); |
|
// const audio = document.querySelector(`audio[data-key="wrong"]`); |
|
// audio.play(); |
|
|
|
if (sequence[guessIdx] === note) { |
|
guessIdx++; |
|
// got (5) correct. Get 20 correct to win |
|
if (sequence.length > WINNING_SCORE && guessIdx > WINNING_SCORE) { |
|
// winner!! |
|
|
|
gameWon = true; |
|
isOn = false; |
|
return true; |
|
} |
|
|
|
return true; |
|
} else { |
|
return false; |
|
} |
|
} |
|
|
|
function wrongAnswer() { |
|
console.log("Wrong!!"); |
|
const audio = document.querySelector(`audio[data-key="wrong"]`); |
|
audio.play(); |
|
guessIdx = 0; |
|
guessSequence = []; |
|
if (isStrict) { |
|
resetGame(); |
|
} else { |
|
idx = 0; |
|
playSequence(sequence[0]); |
|
} |
|
} |
|
|
|
function continueGame() { |
|
//reset guess position |
|
setTimeout(function() { |
|
guessIdx = 0; |
|
guessSequence = []; |
|
addtoSequence(false);} , 1200 ); |
|
|
|
} |
|
|
|
$("#f").mousedown(function() { |
|
// can't use (event.target.id) to pass in the parameter because if you click |
|
// the letter inside the <p> it doesn't have the correct id |
|
// So parameters need to be specified. |
|
if (isOn) { |
|
playingNoteInGame('f'); |
|
} else { |
|
playNote('f'); |
|
} |
|
}); |
|
|
|
$("#g").mousedown(function() { |
|
if (isOn) { |
|
playingNoteInGame('g'); |
|
} else { |
|
playNote('g'); |
|
} |
|
}); |
|
|
|
$("#a").mousedown(function() { |
|
if (isOn) { |
|
playingNoteInGame('a'); |
|
} else { |
|
playNote('a'); |
|
} |
|
}); |
|
|
|
$("#b").mousedown(function() { |
|
|
|
if (isOn) { |
|
playingNoteInGame('b'); |
|
} else { |
|
playNote('b'); |
|
} |
|
|
|
}); |
|
|
|
function playingNoteInGame(note) { |
|
// compare with sequence |
|
let playingNote = false; |
|
const ok = compareGuess(note); |
|
if (!ok) { |
|
wrongAnswer(); |
|
return; |
|
} |
|
const audio = document.querySelector(`audio[data-key=${note}]`); |
|
console.log(audio.src); |
|
console.log(audio.currentTime); |
|
// check if Audio is still playing |
|
if (audio.currentTime > 0) { |
|
setTimeout(function() { |
|
audio.currentTime = 0; |
|
audio.play(); |
|
$("#" + note).css("background", keyColors[notes.indexOf(note)]); |
|
// if (guessIdx === 5) { |
|
// console.log("!! winner !!"); |
|
// // Todo: Winning state |
|
// $(".scoreboard").text('Win'); |
|
// $("body").css('background-image', "url('https://dl.dropboxusercontent.com/s/g6e7xlqxsvv6thu/200w_s.gif')"); |
|
// gameWon = true; |
|
// } |
|
audio.onended = function() { |
|
audio.currentTime = 0; |
|
$("#" + note).css("background", "none"); |
|
notePlaying = false; |
|
|
|
if (guessIdx >= sequence.length && !gameWon) { |
|
continueGame(); |
|
} else if (gameWon) { |
|
console.log('You won! I think...'); |
|
setWinState(); |
|
} |
|
}; |
|
}, 100); |
|
} else { |
|
|
|
audio.play(); |
|
$("#" + note).css("background", keyColors[notes.indexOf(note)]); |
|
|
|
audio.onended = function() { |
|
audio.currentTime = 0; |
|
$("#" + note).css("background", "none"); |
|
notePlaying = false; |
|
|
|
if (guessIdx >= sequence.length && !gameWon) { |
|
continueGame(); |
|
} else if (gameWon) { |
|
console.log('You won!'); |
|
setWinState(); |
|
} |
|
}; |
|
} |
|
} |
|
|
|
function setWinState() { |
|
$(".scoreboard").text('Win'); |
|
$(".on-off button").text('Reset'); |
|
$(".on-off button").css('background-color', 'gray'); |
|
$("body").css('background-image', "url('https://dl.dropboxusercontent.com/s/g6e7xlqxsvv6thu/200w_s.gif')"); |
|
|
|
$(".key").each(function() { |
|
$(this).addClass('whiteBg'); |
|
$(this).removeAttr("style"); |
|
}); |
|
|
|
$(".key > p").each(function() { |
|
$(this).css('color', '#282830'); |
|
}); |
|
|
|
$('h1').css('color', 'white'); |
|
gameWon = true; |
|
isOn = false; |
|
} |
|
|
|
function playNote(nt) { |
|
const audio = document.querySelector(`audio[data-key="${nt}"]`); |
|
audio.play(); |
|
$(`#${nt}`).css("background", keyColors[notes.indexOf(nt)]); |
|
audio.onended = function() { |
|
audio.currentTime = 0; |
|
$(`#${nt}`).css("background", "none"); |
|
notePlaying = false; |
|
}; |
|
} |