user1712544
user1712544

Reputation:

Hangman wrong guesses not printing correctly

I have this Hangman program that i am working on for school and i cant get the number of wrong guesses to print correctly when the user guesses a wrong letter. Here is my code that i got so far, i would appreciate any tips.

import java.util.Scanner;

public class HangmanTest {
    public static void main(String[] args) {

        String[] wordBank = { "madelynn", "crystal", "mcbride", "daughter",
                "adorable", "beautiful", "andrew", "programming", "alyssa",
                "computers", "mcbreezy", "maddy", "happy", "vacation", "beach",
                "java", "benefical", "military", "veteran", "standale",
                "lions", "tigers", "redwings", "pistons", "michigan",
                "football", "baseball", "hockey", "basketball", "golf" };
        int minimum = 0;
        int maximum = wordBank.length - 1;
        String again;

        do {
            int choice = minimum + (int) (Math.random() * maximum);

            String word = wordBank[choice];

            // Converts the random word to asterix
            String userWord = "";
            for (int i = 0; i < word.length(); i++) {
                userWord += "*";
            }

            // Breaks into a bunch of characters
            char[] userWordCh = userWord.toCharArray();

            // Show the random word
            System.out.println("The word for you to guess is " + userWord);

            // instantiate a scanner object
            Scanner input = new Scanner(System.in);

            int size = word.length();
            int rightGuesses = 0;
            int wrongGuesses = 0;

            while (size != rightGuesses) {
                System.out.println("Enter a character: ");
                String response = input.next();
                char ch = response.charAt(0);

                char[] wordChars = word.toCharArray();

                for (int i = 0; i < word.length(); i++) {
                    if (wordChars[i] == ch) {
                        userWordCh[i] = ch;
                        ++rightGuesses;
                    } else {
                        ++wrongGuesses;
                    }
                } // end of for loop

                System.out.print("The word is: ");
                for (int j = 0; j < userWordCh.length; j++)
                    System.out.print(userWordCh[j]);

                System.out.println();
            } // end of while loop

            System.out.println("You had " + wrongGuesses + " wrong guesses.");

            System.out.println("Would you like to play again y/n: ");
            again = input.next();

        } while (again.equals("y"));

    }
}

Upvotes: 0

Views: 947

Answers (4)

75inchpianist
75inchpianist

Reputation: 4102

problem is in your for loop. You are iterating over each letter, and for every letter that doesn't match yours, you mark it as an incorrect guess. It should only be marked incorrect if NONE of the letters are correct. Additionally it should only be marked right if you haven't marked it already.

import java.util.Scanner;
import java.util.ArrayList;

public class HangmanTest {
    public static void main(String[] args) {

        String[] wordBank = { "madelynn", "crystal", "mcbride", "daughter",
                "adorable", "beautiful", "andrew", "programming", "alyssa",
                "computers", "mcbreezy", "maddy", "happy", "vacation", "beach",
                "java", "benefical", "military", "veteran", "standale",
                "lions", "tigers", "redwings", "pistons", "michigan",
                "football", "baseball", "hockey", "basketball", "golf" };
        int minimum = 0;
        int maximum = wordBank.length - 1;
        String again;

        do {
            int choice = minimum + (int) (Math.random() * maximum);

            String word = wordBank[choice];

            // Converts the random word to asterix
            String userWord = "";
            for (int i = 0; i < word.length(); i++) {
                userWord += "*";

            }

            String guessedLetters="";
            // Breaks into a bunch of characters
            char[] userWordCh = userWord.toCharArray();

            // Show the random word
            System.out.println("The word for you to guess is " + userWord);

            // instantiate a scanner object
            Scanner input = new Scanner(System.in);

            int size = word.length();
            int rightGuesses = 0;
            int wrongGuesses = 0;
            boolean foundLetter;
            char[] wordChars = word.toCharArray();

            guessLoop:
            while (size != rightGuesses) {
                System.out.println("Enter a character: ");
                String response = input.next();
                char ch = response.charAt(0);


                foundLetter=false;
                for (int i=0;i<guessedLetters.size();i++){
                    if (ch == guessedLetters.charAt(i)){
                         System.out.println("Already guessed that letter!");
                         continue guessLoop;
                    }
                }

                guessedLetters+=response;
                for (int i = 0; i < word.length(); i++) {
                    if (wordChars[i] == ch) {
                           foundLetter=true;
                           userWordCh[i] = ch;
                           ++rightGuesses;                            
                    } 
                } // end of for loop
                if(!foundLetter)
                     ++wrongGuesses;
                System.out.print("The word is: ");
                for (int j = 0; j < userWordCh.length; j++)
                    System.out.print(userWordCh[j]);

                System.out.println();
            } // end of while loop

            System.out.println("You had " + wrongGuesses + " wrong guesses.");

            System.out.println("Would you like to play again y/n: ");
            again = input.next();

        } while (again.equals("y"));

    }
}

Upvotes: 0

CBredlow
CBredlow

Reputation: 2840

for (int i = 0; i < word.length(); i++) {
                if (wordChars[i] == ch) {
                    userWordCh[i] = ch;
                    ++rightGuesses;
                } else {
                    ++wrongGuesses;
                }
            } // end of for loop

This for loop is causing your problem with wrong guesses, since it will say that a character is a wrong guess even though it is in the word. An option you can do is have some boolean that is switched to true when it finds the letter. That way, when you leave the for loop, you check to see if that value is true. If it isn't, increment the wrong guesses.

Upvotes: 0

Patashu
Patashu

Reputation: 21793

for (int i = 0; i < word.length(); i++) {
    if (wordChars[i] == ch) {
        userWordCh[i] = ch;
        ++rightGuesses;
    } else {
        ++wrongGuesses;
    }
} // end of for loop

In this loop, you increment rightGuesses by 1 every time the guess matches a letter in the word, and wrongGueeses by 1 every time the guess does not match a letter in the word. As you can imagine, this will lead to the numbers, collectively, being incremented by the same number as the number of letters, when it should only be incremented once.

Try something like:

boolean foundMatch = false;
for (int i = 0; i < word.length(); i++) {
    if (wordChars[i] == ch) {
        userWordCh[i] = ch;
        if (!foundMatch)
        {
            ++rightGuesses;
            foundMatch = true;
        }
    }
}
if (!foundMatch)
{
    ++wrongGuesses;
}
// end of for loop

Now we only increment one of rightGuesses and wrongGuesses once - rightGuesses can only be incremented if we have not found a match (setting found match to true), and wrongGuesses can only be incremented once if we have not found a match.

Upvotes: 2

slugonamission
slugonamission

Reputation: 9642

I'm guessing here, but I think this might be wrong:

for (int i = 0; i < word.length(); i++) {
     if (wordChars[i] == ch) {
         userWordCh[i] = ch;
         ++rightGuesses;
     } else {
         ++wrongGuesses;
     }
} // end of for loop

This will increment the rightGuesses and wrongGuesses variables for each character in the word that matches/doesn't match the inputted character. Instead, you need to set a flag when a character "matches", then check that flag at the end to update rightGuesses and wrongGuesses.

Upvotes: 0

Related Questions