Help with Simon!

Ok, so I’ve been trying to solve this problem for awhile. My Simon game is in the works and I’m just stuck with a very strange issue. Hopefully somebody can help me out while trying to understand my code. Right now, I can turn on the device and start a game. All I want it to do right now is play the computer sequence, wait for the player to copy (so far it doesnt check if the play pressed the right buttons, just the same amount) and then add one to the sequence and do it again. The computer sequence is working, but every time its my turn, it runs the player sequence (on click) too many times. If anybody has the patience, try playing the game and open the console. I have it log “+1” every time it runs the player function, and youll see that instead of it adding one, it exponentially adds one (+1, +1+1, +1+1+1, etc). I’ve already have the player array resetting to empty before their turn. I don’t know what to do!! Thanks in advance to any patient coders who are willing to help!

  • Jeff

Sounds like you are adding an event listener every turn? If so use .off() to remove all previous event handlers: $(...).off().on("click", ....

2 Likes

Thanks Ben, this did the trick. All I had to do was $(".color).off("click) after each turn. Otherwise it was remembering each previous click every turn. I owe you a beer my friend. Thanks a lot!

1 Like

Oh my… I had the same problem when working on my TicTacToe project. It was like witchcraft!! I was really one of those times you hate coding… :))) I was getting different right results when playing a human against the computer, but strange ones when letting the computer be the two players…

It is true also that Javascript gets silly when you want to pause and wait for events, or just wait.

Thank you for tip on the eventlisteners trap!!