#I need help with a last string for Java in a game of Hangman

43 messages · Page 1 of 1 (latest)

wispy oxide
#

I programmed a hangman game for my assignment but the teacher wants us to only guess one letter of the word if there's multiple of that letter.
For example the word hello:
-player guesses: L
-program updates hidden word to H * L * *
-player guesses: H
-program updates hidden word to H * L * *
-player guesses: L
-program updates hidden word to H * L L *
...
Thank you for taking your time to read this and please need help.

oak domeBOT
#

This post has been reserved for your question.

Hey @wispy oxide! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

clear spade
#

what issue are you having

wispy oxide
#

My program guesses all the letters in that word instead of guessing one letter

#

for example for hello, if I guess L it gives me * * L L *

clear spade
#

then, uh, fix that?

wispy oxide
#

I don't know how that's the thing

clear spade
#

uh, do you expect us to know

#

we know so much less than you about your own code...

#

figure out which part goes through the string to reveal characters, and amend that part to only reveal 1

wispy oxide
#

Can I copy paste my code here ?

oak domeBOT
#

We won't do your homework for you. Try the problem first, and if you have a question on a specific part of the problem, or your implementation of the problem, we can help!

clear spade
#

please attempt to solve it first, if you get stuck on anything you can ask about that

#

once you do ask;

oak domeBOT
#

Looks like you shared a lot of code, this may cause people to overlook problems in your code. To make it easier for them, make sure you only share lines that are relevant to your issue.

wispy oxide
clear spade
wispy oxide
#

// Play the game
while (numGuesses > 0 && !solved) {
// Print current status
String display = "";
for (int a = 0; a < word.length(); a++) {
if (guessedLetters.indexOf(word.charAt(a)) >= 0) {
display += word.charAt(a);
} else {
display += "*";
}
}
System.out.println("Word to date: " + display + " (" + numGuesses + " guess(es) left)");

        // Ask for a guess
        System.out.print("Letters not tried yet: ");
        for (char c = 'A'; c <= 'Z'; c++) {
            if (guessedLetters.indexOf(c) < 0) {
                System.out.print(c);
            }
        }
clear spade
#

please format it...

oak domeBOT
#
// Play the game
        while (numGuesses > 0 && !solved) {
            // Print current status
            String display = "";
            for (int a = 0; a < word.length(); a++) {
                if (guessedLetters.indexOf(word.charAt(a)) >= 0) {
                    display += word.charAt(a);
                } else {
                    display += "*";
                }
            }
            System.out.println("Word to date: " + display + " (" + numGuesses + " guess(es) left)");

            // Ask for a guess
            System.out.print("Letters not tried yet: ");
            for (char c = 'A'; c <= 'Z'; c++) {
                if (guessedLetters.indexOf(c) < 0) {
                    System.out.print(c);
                }
            }
clear spade
#

so with the new thing you don't need to keep track of guessed letters, so you can remove all that

#

instead of generating the string result each time, you would need to store the current state

#

you could do that with a boolean array, for example

wispy oxide
wispy oxide
wicked willow
#

You could do it with a modified HashMap that contains a char and a boolean. With this modified map, you could use a 'for' loop until you get to a char that isn't associated with true. Then, use your correct guess logic and toggle that value.
After every turn, you could just do one sweep over the map, and if every value is true, you win!

wicked willow
#

Why not?

clear spade
#

also why modified?

#

normal HashMap works for that

clear spade
wicked willow
wicked willow
clear spade
clear spade
wicked willow
clear spade
#

what's wrong with just iterating over the set though

wicked willow
#

You could, but I prefer a more usable version. Just my thoughts 😊

clear spade
#

don't see any use for index here but eh ¯_(ツ)_/¯

#

the solution as a whole doesn't work

wicked willow
#

Hmm... Sorry!

clear spade
#

(any other ideas though? it's 3 am and nothing's coming lmao)