Hi all, I have a problem with my Simon Game which I can’t seem to debug. Relevant code snippet below and the whole of the code can be found here: https://github.com/JamesRiall/Simon-Game
Essentially the program is adding “red”, “green”, “blue” or “yellow” to the array clicksInRound whenever the relevant panel on the game is clicked. I can then use this clicksInRound array to check against the sequence the player should be following to either do nothing (if the selection is right but they haven’t completed the round yet), move them onto the next level (if selection is correct and they’ve completed that round’s sequence) or reset the game (if wrong).
My problem is that when I play through the game, the clicksInRound array is having too many values pushed to it. Below is the result of console.log of clicksInRound when I play a game and click on yellow twice (with yellow the first color in the generated sequence):
The output I want here is just “yellow” for the first two lines (the first click my selection is yellow in round one, and then second click my selection is yellow only so far in round two). However, I’m getting the third line [“yellow”, “yellow”] which is throwing out all of my checks. If I click yellow then blue in the second round, my clicksInRound array will be [“yellow”, “yellow”, “blue”] when I want my output to be [“yellow”, “blue”]
Can anyone diagnose the problem from the code snippet below or from my full code on GitHub?
Thanks!
//function to check if the click is incorrect or correct.
function checkClickIsCorrect() {
console.log(clicksInRound);
if (currentRoundSequence[whereInRound-1] != clicksInRound[whereInRound-1]) {
if (strict === true) {
//GAME OVER NOISE AND MESSAGE
running = false;
$("#start-reset").html("Start");
round = 0;
$("#round").html(round);
clearSequence();
} else if (strict === false) {
//ERROR MESSAGE AND ROUND RESTART MESSAGE
round--;
newRound();
}
} else if (arraysEqual(currentRoundSequence, clicksInRound) === true) {
newRound();
}
}
//check array iteratively to see if they are equal
function arraysEqual(arr1, arr2) {
if (arr1.length !== arr2.length) {
return false;
}
for (var i = arr1.length; i--;) {
if (arr1[i] !== arr2[i])
return false;
}
return true;
}
//Function to run at the beginning of every new round.
function newRound() {
//function to generate new round sequence
function generateNewRoundSequence() {
for (i = 0; i < round; i++) {
currentRoundSequence.push(sequence[i]);
}
}
//if player gets to round 21 (i.e. completes 20 separate rounds) then they have
//achieved victory.
if (round === 21) {
//SOME COOL VICTORY STUFF
} else {
//add one to the round counter
round++;
//reset clicks in round var
clicksInRound = [];
//reset whereInRound var
whereInRound = 0;
//update round display
$("#round").html(round);
//display the sequence for the current round.
displayCurrentSequence(round);
//wipe the currentRoundSequence array;
currentRoundSequence = [];
//update the currentRoundSequence array to reflect the addition of the next step.
generateNewRoundSequence();
//what to do when user clicks color panel while playing
$(".game-button").click(function() {
if (sequencePlaying === false && running === true) {
if ($(this).is("#red")) {
playRedInSequence();
clicksInRound.push("red");
whereInRound++;
checkClickIsCorrect();
} else if ($(this).is("#blue")) {
playBlueInSequence();
clicksInRound.push("blue");
whereInRound++;
checkClickIsCorrect();
} else if ($(this).is("#yellow")) {
playYellowInSequence();
clicksInRound.push("yellow");
whereInRound++;
checkClickIsCorrect();
} else if ($(this).is("#green")) {
playGreenInSequence();
clicksInRound.push("green");
whereInRound++;
checkClickIsCorrect();
}
}
});
}
}
```