3

I'm trying to make a hangman type game but I'm having difficulty with writing the method that returns and updates the word which the user is trying to guess. So for example if the word to guess was 'hello' I would want it to be printed as * but if someone guesses 'e', it should update to e**. I have kind of got this to work but it loops through two ArrayLists which is not exactly what I want:

public String getVisible() {
    String rtn = null;
    for(Character b: brokenphrases){

        for(Character c:characters){
            if(b == c){
                rtn = rtn + b;

            }
        }

        if(b.toString().equals(" ")){
            rtn = rtn +" ";

        }
        else{

            rtn = rtn + "*";
        }

    }

    return rtn;
}

The idea is that the ArrayList of characters contains the input that the user puts in, and the ArrayList of brokenphrases has the word that needs to be guessed, broken down into single characters so they can compare the two. What happens now is when I run the program I get my word to guess, for example hello printed as * but as I guess letters the the text becomes doubled up instead of being replaced. So if I was to type in h ,e ,l ,o , the system prints out h*e*l*l*o* instead out getting rid of the asterix. I figure that it's to do with using two for loops but I'm not sure on how to mend it. Any advice would be appreciated. Thanks.

0

2 Answers 2

1

You can simplify your code to get something like this:

public String getVisible() {
    String rtn = null;

    for(Character b: brokenphrases){
        if(characters.contains(b)){
            rtn = rtn + b;
        } else {
            rtn = rtn + "*";
        }
    }

    return rtn;
}

You iterate over all characters of your target word (brokenphrases), for example hello. If the character is in characters, you add the character to the output. If not, you add *.

If you guessed h and l you would get h*ll*.

No need to add blankspace.

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

5 Comments

This doesn't seem to work too well. Every time I add a letter it doubles the whole string in size. So for hello it starts as ***** then becomes ********** then *************** and so on.
@LukaszMedza Sorry, i think i found the problem. No need for two loops now. Just check if the letter is in characters.
System.out.println("Word to guess:" + getVisible());
How would I check if the letter is in characters without using a for loop?
@LukaszMedza characters.contains(b) checks if b is in characters.
0

You don't really need the characters to be sorted, so you can use a Set (e.g. a TreeSet) for them instead of an ArrayList. Then you can change your code to something like this:

public String getVisible() {
    String rtn = null;
    for(Character b: brokenphrases){

        if(characters.contains(b)){
            rtn = rtn + b;
        }

        if(b.toString().equals(" ")){
            rtn = rtn +" ";

        }
        else {

            rtn = rtn + "*";
        }

    }

    return rtn;
}

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.