简体   繁体   中英

Replacing question marks with guessed letters in Java using arrays

The premise of my project it to create a Hangman kind of game where the user inputs a phrase that contains just letters and spaces, it prints out the phrase but with question marks in place of the letters, and then a second user guesses letters that may be in the phrase. The only part I'm struggling with is getting the guessed letters to replace the question marks again. Right now if you guess any correct letter the entire phrase is revealed. Here is my update method. Any feedback is appreciated!

  public static int updateTemplateArray(char [] tmpArr, String sPhrase, char guess)
  {
    int vowel = 0;

    for (int i = 0; i < sPhrase.length(); i++)
    {
        if(guess == sPhrase.charAt(i))
        {
            guess = tmpArr[i];
            if (isVowel(guess))
            {
                vowel++;
            }
        }
    }
    return vowel;
} //end updateTemplateArray

EDIT: I think the main problem may reside in a different method, here is that

 public static void printTemplateArray(char [] tmpArr)
 {
    char [] qMark = new char[tmpArr.length];
    for (int i = 0; i < tmpArr.length; i++)
    {
        if(tmpArr[i] == ' ')
        {
            qMark[i] = ' ';
        }

        else
        {
            qMark[i] = '?';
        }
    }

    System.out.print(qMark);
}

It seems that your

guess = tmpArr[i];

should be

tmpArr[i] = guess;

You may also want to remove System.out.println(tmpArr); statement from your update method since it probably shouldn't be part of it (especially as part of loop). Invoke it after that method to see results on your array which was represented by tmpArr .

Simplified demo:

public static void updateTemplateArray(char[] tmpArr, String sPhrase, char guess) {

    for (int i = 0; i < sPhrase.length(); i++) {
        if (guess == sPhrase.charAt(i)) {
            tmpArr[i] = guess;
        }
    }
}

...

String phrase = "foo bar";
char[] arr = new char[phrase.length()];
Arrays.fill(arr, '?');

//guessing time
updateTemplateArray(arr, phrase, 'f');
System.out.println(arr);
updateTemplateArray(arr, phrase, 'b');
System.out.println(arr);
updateTemplateArray(arr, phrase, 'x');
System.out.println(arr);

Output:

f??????
f???b??
f???b??

RE Edit:

In your printTemplateArray(char [] tmpArr) method you are simply creating new array which is filled with either space or ? . So there is no way for it to print other characters that these two.
If you want this method to only hide with ? characters which are not spaces and ware not guessed by second player then you should store somewhere and pass to your method guesses of that player ( Set<Character> seems to be right collection for that). So your corrected code could look like:

public static void printTemplateArray(char[] tmpArr,
        Set<Character> guesses) {
    char[] qMark = new char[tmpArr.length];
    for (int i = 0; i < tmpArr.length; i++) {
        if (tmpArr[i] == ' ') {
            qMark[i] = ' ';
        } else if (guesses.contains(tmpArr[i])) {
            qMark[i] = tmpArr[i];// copy already guessed char
        } else {
            qMark[i] = '?';
        }
    }

    System.out.print(qMark);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM