Hello and thank you for reading. I will try my best to explain what I have done so far and my current thought process about solving this problem. I have to make a hangman-based, text game where a user can make guesses about a string I have disguised with question marks. Every time the user makes a guess the program should check the character input against the secret word and update the disguised word with the correct character displaying (granted the guess was correct) or simply ask again if the guess was incorrect. So the method I'm having trouble with is "makeGuess". Right now the program keeps skipping the "for-each" statement and returning "???????" disguised word no matter what the guess is (unless it's a digit to quit). I was hoping it would first check the "if" condition, checking if user's guess is equal to each character at every index, and then replace the index of the correct guess with the correct guess: basically updating the disguisedWord. Then it should also check in the 2nd (if) statement if the secretWord and disguisedWord are the same to break out of the isFound boolean and end the program. The (else) seems to be getting called no matter what character is entered, so all that happens is wrongGuesses continues incrementing.
I appreciate any advice or pointers in the right direction and I'm not looking for someone to just solve it for me since I feel like I almost have this done. I just feel like now I've hit a wall. Thank you for your time.
hangman class:
package hangmandemo; public class Hangman { private String secretWord; private String disguisedWord; private int guesses; private int wrongGuesses; boolean found = false; public String getDisguisedWord() { return disguisedWord; } public String getSecretWord() { return secretWord; } /** * returns number of guesses */ public int getGuesses() { return guesses; } /** * continues game until secretWord is found */ public boolean isFound() { return found; } /** * brings in a char to test against String */ public void makeGuess(char c) { for (int i = 0; i < secretWord.length(); i++) { if (c == secretWord.charAt(i)) { int position = secretWord.indexOf(c); char letter = secretWord.charAt(position); disguisedWord = disguisedWord.replace(letter, secretWord.charAt(i)); } } if (secretWord.equals(disguisedWord)) { found = true; } else { System.out.println("keep trying"); wrongGuesses++; } } /** * assigns secret String to secret word */ public void createSecretWord(String secret) { secretWord = secret; } /** * disguises secret word with "???" */ public void createDisguisedWord() { disguisedWord = ""; for (int i = 0; i < secretWord.length(); i++) { disguisedWord += "?"; } } /** * returns number of wrong guesses */ public int getWrongGuesses() { return wrongGuesses; } }
Main class:
package hangmandemo; import java.util.Scanner; public class HangmanDemo { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); Hangman game = new Hangman(); game.createSecretWord("assessments"); game.createDisguisedWord(); System.out.println("This is the hangmandemo."); System.out.println("Enter any digit to quit."); System.out.println("The disguised word is..."+ game.getDisguisedWord()); while (!game.isFound()) { System.out.println("Enter a one character guess"); char c = keyboard.next().charAt(0); if (Character.isDigit(c)){ System.out.println("game over, the word was... " + game.getSecretWord()); System.exit(0); } game.makeGuess(c); System.out.println(game.getDisguisedWord()); } System.out.println("you guessed it, the word was... " + game.getSecretWord()); System.out.println("you had " + game.getGuesses() + " guesses."); System.out.println("you had " + game.getWrongGuesses() + " wrong guesses."); } }