I’m working on Simon Says and I’m stuck. I’m trying to write a function that simulates button clicks to use for the computer’s turn i.e. a series of increasingly complex turn moves.
I’m starting by just trying to figure out how to simulate a click event on the green button. I will later work out timing the event using setInterval and choosing random combinations of buttons for the turn via the Math.random function.
But for now I’m just trying to simulate the button click of the green button and save it to the computer’s array which is aiMoves[] in my program.
I’m simulating the click like this:
function simulateClick(){
$('#green').on('click', function(){
sound1();
alert("Green button clicked");
});
$('#green').trigger('click');
}
After simulating the click I want to pass it to the aiMoves array so I can compare it to the playerMoves array and determine if the player has the correct sequence of moves. I stored the players series of moves like this:
function storeClicks(){
$('.button').each(function() {
$(this).click(function() {
playerMoves.push($(this).attr('data-simonButton'));
console.log(playerMoves);
return playerMoves;
});
});
}
I’ve hit a mental block and I’m probably overthinking this, but how would I do something similiar for the aiMoves array and then pass both to the validate function?
Here’s the codepen: