Simon game help please

Have been working on Simon Game for 2 weeks, can’t figure the userInput() part out. Any help or suggestions are appreciated!

https://codepen.io/judysome/pen/wppVeN?editors=0011

Line 66. In each level, after user presses the first button, any input after that is considered as ‘undefined’, thus a ‘wrong’ message will show in the console. How can I evaluate each enter, and then be able to stop anytime if an error is made.

Thanks,
Judy

First off your not alone … a lot of people struggle with this …
Second Im spending my valuable time giving suggestion … and if i sound harsh dont take offence as its not intended … plus if you dont like my suggestions you can ignore them.

Ok there is a lot going on here most of the problems stem from your start function
when i play your simon game it works for first two rounds but then falls apart eg on round three i click a panel and it plays sound but then the computer takes over and replays all the sounds one by one from the array

My main suggestion is delete all the javascript and start again … I know this is a hard suggestion but lets look at the start function … it plays the computers notes but also has the function call for you clicking your notes … so when you play it also starts the computer playing

You need to totally segregate everything which will make things easier for you and easier to debug
You do not use any onclick handlers for any of your buttons/panels which would make things a lot easier for you eg for your start button you could use … $(’#start’).click(function(){ }) for onOff button $(’#onOff’).click(function(){})
then just write code in these for those specific button clicks or write function and call them from these.

What should you put in your start function … first off i would change it to the click handler … then have a if statement to check if onOff button is in on position and start button hasnt previously being pressed … (if start button has previously being pressed it should do nothing) … then if onOff is in on position and start hasnt previously being pressed i would push notes to the array and then play whatever is in the array.
or better call a function that dose this.

$('#start').click(function(){ 
     if(checking onOff &&  start) {
      compMakesMusic()
     }
})

using a function call is best because when you do your moves and its computer turn you can just call the compMakesMusic function

what would be in compMakesMusic function … depending on how you go it could push to an array random note or color eg i pushed .green or .yellow and then it would play note and flash panel and when finished it would set a Boolean eg playerTurn to true;
might look like this

function compMakeMusic() {
      array.push(randomcolor); // i used a function for randomcolor to choose
     then  flash color
      play sound
      check if it has done it for all items in array
     set playerTurn to true
}

now this is just sudo code and you would have to think how you would do it … but you can see how it keeps the computer playing notes totally seperate from you playing your notes

i would then have a click handler for when you click on panels to play the notes back eg

$('.panels').click(function(){ 
     if(playerTurn &&  gameOn) {
     //assign what panel was clicked to a variable
    // check if it matches whats in array
   // if so play note
  // if all notes played 
  // playerTurn = false 
// call compMakeMusic();
     }
})

again this is sudo code you would have to decide how to write it … what goes into the function and whether to write helper functions that you could call rather than piling lots of code in this click function

But the code for you clicking the panels and playing the notes is totally seperate from the code comp uses to play the notes … this makes debugging easier and writing your code easier

As i said you can just ignore my suggestions or try to separate out your code so that comp is not playing notes when you are clicking on your panels … you will also need to reset your array when game is turned off as it is not doing this eg play game turn off game start game and it still has the previous games moves in array … had problems with strict mode too but i didnt look into that as i figured it also connected to the way the start function works.
Any further help required pleas post and im sure me or others will help

1 Like