0

I'm making a simple hangman game. The code below updates the letters so it's checking if the guessed letter is in the word properly. However, it won't store the letter if a second correct letter is guessed. For example, for "moose", if I guess 'o' then it will print out (#oo##), then if I guess 'e', it will print out ####e. Maybe it has something to do with the else statement? I'm fairly new to this so any tips or just documentation would help. Thanks!

 public static boolean updateWithGuess(char[] knownLetters,
                                      char guessedLetter,
                                      String word) {
    boolean found = false;

    for(int i = 0; i < word.length(); i++) {
        if(word.charAt(i) == guessedLetter) {
            knownLetters[i] = guessedLetter;
            found = true;
        }
        else {
            knownLetters[i] = BLANK;
            found = false;
        }
    } 
    return found;
}
2
  • why do you need knownLetters[i] = BLANK;? if all positions are initially blank, there is no need to assign them to blank again Commented Oct 26, 2020 at 3:58
  • You're storing the guessed letter in a single char variable, but you're storing the letters of the secret word in a char array. What if... you also stored guessed characters in a second char array and iterated through it when you wanted to know whether the user already guessed it? Commented Oct 26, 2020 at 4:06

1 Answer 1

1

My guess is that the issue isnt in this method, Instead try returning the char[] And then the caller of the method will update to that, In the end not caring if its correct or incorrect(Unless if you have some reason not to do this). Also just take out the whole else, That is whats causing your issue because its blanking out the other letters even if they are correct.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.