Hangman Question

I’m having trouble finishing up my Hangman project. I can’t seem to get the computer side to be a functional guesser. I believe there is a logic issue in that when a letter does not exist in the Human’s secret word, I can’t access the Computer’s guess function correctly and recursively. This is also an issue after the Computer’s correct guess has been added to the board – the same letter is continually guessed. I’d be grateful for any insight! Thank you!

Below is my code in pictures (I can’t seem to format it with the @'s so that the board will allow me to post it, but I can try other methods if this isn’t readable):

Hi ellenupton,

I think your code has the same problem as another student. Check out my answer to her hangman question. Hangman specs not passing
If you do not mind, you can also post a screenshot of the specs you are not passing and I can look over that.

Michael Ruan
Alpha Course TA

Hi Michael,

I’ve modified my code to pass the specs that are mentioned in your other answer, you were right, those were issues in my code. However I’m still having one more issue that isn’t caught in the specs. I believe it is in line #83. The only case that doesn’t work when I run the actual game in my terminal is when the computer is guessing the word and the letter it guesses is not in the word that the human has thought of. When the human answers “no” an error is thrown. I think my logic is confused I know I shouldn’t be setting the pos array to nil but I’m not sure how to get the computer to simply guess again with the next most frequent letter.

Thank you,
Ellen Upton

06%20PM

Hi Ellen,

Yes, you should definitely not set the pos array to nil. There are a few ways to catch a “no” response. One way would be to use a begin, rescue, retry block to keep asking the guesser to keep guessing. Check out this link if you want to approach the problem this way.
https://www.tutorialspoint.com/ruby/ruby_exceptions.htm
Another way would be to call check_guess recursively if the response is “no” which essentially keeps looping until the response is "yes.
A third way would be to have a while loop in your take_turn method that encloses the check_guess method and only break if the response is “yes” otherwise keep calling check_guess.
Though ideally, you would want a counter to check how many incorrect guesses are made and then stop the whole game if the guesser makes too many incorrect guesses. You can also incorporate this counter to break your loop too!
Hope this helps!

Best,
Michael