I am building a Simon game, modeled after the old handheld game from the 90s(?). I cannot figure out how to prevent the user from clicking on the 4 colored buttons when not their turn, and essentially, go out of turn and break my program. How can I prevent them from activating the mousedown jquery below while 'Simon' takes his turn? I really appreciate any help, thanks.
Codepen link: http://codepen.io/MindfulBell/pen/ZbgQym
Relevant code (I think, only been programming for 6 months)...
Simons action:
function simonSaysLv1(){
simonsMoves.push(Math.floor(Math.random() * (4-1+1)) + 1);
doSimonMove(0);
playerTurn = true;
simonTurn = false;
}
function doSimonMove(index){
if(index < simonsMoves.length){
simonlightUp(simonsMoves[index]);
setTimeout(function(){
doSimonMove(index + 1);
}, simonSpeed);
}
}
Mousedown player action:
$('.sim-button').mousedown(function(){
if (playerTurn && started && gameOn) {
var playerPress = '#' + this.id
switch(playerPress){
case grn:
grn1Audio.play();
$(grn).css('background-color', '#00ff00')
playerMoves.push(1)
break;
case red:
red2Audio.play();
$(red).css('background-color', '#ff1a1a')
playerMoves.push(2)
break;
case yel:
yel3Audio.play();
$(yel).css('background-color', '#ffffb3')
playerMoves.push(3)
break;
default:
blue4Audio.play();
$(blue).css('background-color', '#b3d1ff')
playerMoves.push(4)
break;
}
}
})
//letting go of click
$('.sim-button').mouseup(function(){
if (playerTurn && started && gameOn) {
var playerPress = '#' + this.id
switch(playerPress){
case grn:
$(grn).css('background-color', 'green')
break;
case red:
$(red).css('background-color', '#cc0000')
break;
case yel:
$(yel).css('background-color', '#e4e600')
break;
default:
$(blue).css('background-color', '#0052cc')
break;
}
if (playerMoves.length === 21) {
alert('You Win!')
}
//fail on strict
else if (checkLoss() && strict) {
reset();
setTimeout(simonSaysLv1, 1200);
$('#strict').css('border-color', '#99ff66')
$('#count-num').text(simonsMoves.length+1)
}
//fail on non strict
else if (checkLoss()) {
playerMoves = [];
setTimeout(function() {
doSimonMove(0)}, 1200)
}
//back to simon for his turn
else if (playerMoves.length === simonsMoves.length) {
speedCheck(simonsMoves.length)
setTimeout(simonSaysLv1, 1200);
$('#count-num').text(simonsMoves.length+1); //score
playerMoves = [];
simonTurn = true;
playerTurn = false;
}
}
})