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.
#I need help with a last string for Java in a game of Hangman
43 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @wispy oxide! Please use
/closeor theClose Postbutton 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.
what issue are you having
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 *
then, uh, fix that?
I don't know how that's the thing
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
Can I copy paste my code here ?
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!
please attempt to solve it first, if you get stuck on anything you can ask about that
once you do ask;
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.
tried since yesterday and I'm still stuck for my issue
// 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);
}
}
please format it...
// 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);
}
}
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
oh sorry about that
I can't use arrays for this assignment 
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!
char and boolean doesn't work
Why not?
not normal hangman :/
Oh.
Actually, you can't get a key/value by index in an ordinary HashMap.
missed this ping, sorry
wtf?
you can though? kinda. just barely
Yeah, but I prefer the modified one.
what's wrong with just iterating over the set though
You could, but I prefer a more usable version. Just my thoughts 😊
don't see any use for index here but eh ¯_(ツ)_/¯
the solution as a whole doesn't work
Hmm... Sorry!
(any other ideas though? it's 3 am and nothing's coming lmao)