0

Hey guys I am working on a simple GUI word guess game in which one user inputs a word and another tries to guess it letter by letter. A user has 4 tries and each "-" represents a wrong guess before the "bomb" (shown as ----O) explodes. I have coded the GUI and the logic to show the stars for the word but I am stuck on the method to check the guess of user (checkGuess()). I appreciate your time

here is my code

public void checkGuess()
{
    char[] wordToGuess = secretword.toCharArray();
    guessLetter = guessBox.getText();

    realGuessLetter = guessLetter.charAt(0);

    for(int i=0; i<wordToGuess.length; i++) 
    {
        if(realGuessLetter == wordToGuess[i]) 
        {
            wordSoFar[i] = realGuessLetter; 
            stardisplay  = new String(wordSoFar);
        }
    }
}
Mike 'Pomax' Kamermans
  • 49,297
  • 16
  • 112
  • 153
  • What is the exact problem? Is it like hangman? Because now you are constantly checking for the first letter to be correct, not just any letter in the word. Which makes no sense to use the loop for it. – Dylan Meeus Dec 03 '13 at 17:38
  • please remove all that superfluous whitespace and do some proper indenting of that code, to make it easier to read for us. Remember: we're humans too =) – Mike 'Pomax' Kamermans Dec 03 '13 at 17:38
  • 1
    Please explain just what checkGuess is supposed to do (this is the kind of thing comments are for); if you could explain how it is coming up short, that would be even better. – Scott Hunter Dec 03 '13 at 17:43
  • your edited code can't compile now. But, assuming you have some global variables (which you shouldn't have), what is the actual problem you are having with the code you're now showing? This code just shuffles some `char` around without really doing anything. – Mike 'Pomax' Kamermans Dec 03 '13 at 17:44
  • Hello, firstly I apologize for any mistakes in my original post and I appreciate everyone who has answered. checkGuess is supposed to convert secretword(string) to a char array then get the letter from guessBox(JTextField) and convert it to a char so it can be compared to the char array. Then the for loop compares guessLetter to the char array wordToGuess Lastly it sends the char array wordsofar as a string to stardisplay(JLabel) – user3062491 Dec 03 '13 at 17:50
  • What is your problem sir?! – theGreenCabbage Dec 03 '13 at 19:38
  • Also, for `string` or `char` comparisons, please refrain from using `==`, as that's actually comparing memory locations as compared to actually comparing the equality of the string. `.isEqual()` is the method you want to use. Check this thread for more information why: http://stackoverflow.com/questions/767372/java-string-equals-versus – theGreenCabbage Dec 03 '13 at 19:40

0 Answers0