Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: Method error for checking and updating guess in a basic hangman game

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Method error for checking and updating guess in a basic hangman game

    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.");
        }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Method error for checking and updating guess in a basic hangman game

    he program keeps skipping the "for-each" statement
    I don't see that type of for statement.

    Try debugging the code by adding lots of println statements in the makeGuess() method to show every variables value every time it is tested and changed.
    The printout will help you understand what the code is doing.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Method error for checking and updating guess in a basic hangman game

    Thank you for the advice, I will try this debugging approach and hopefully I'll figure out the logic errors!

    --- Update ---

    The debugging helped me identify what's working and what's not working inside the makeGuess method. But now I'm confused to as to the why functions aren't working - specifically these functions:
    disguisedWord = disguisedWord.replace(letter, secretWord.charAt(i));
    //and
     int position = secretWord.indexOf(c);
    Here is the debugging output I learned from trying "e":
    run:
    This is the hangmandemo.
    Enter any digit to quit.
    The disguised word is...???????????
    Enter a one character guess
    e
    c is = e
    letter is = e
    position is = 3
    secret word is = assessments
    disguised word is  = assessments
    c is = e
    letter is = e
    position is = 3
    secret word is = assessments
    disguised word is  = assessments
    keep trying
    ???????????
    Enter a one character guess

    The whole method logic seems to be off as well. It runs through multiple times depending on how many times this boolean is true:
     if (c == secretWord.charAt(i))
    So I know it is correctly checking the index, but incorrectly updating the position and incorrectly updating disguisedWord.
    Do you have any tips or links to documentation that might help me correctly amend the logic and functions in this method?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Method error for checking and updating guess in a basic hangman game

    disguisedWord.replace(letter, secretWord.charAt(i));
    What was the value of disguisedWord when the above was executed? Did it contain a char that is in the variable: letter?

    Did your debug printouts printout the value of disguisedWord before and after the above statement with the replace() method? Were any changes made?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Apr 2013
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Method error for checking and updating guess in a basic hangman game

    (I accidentally made the println debug test for disguisedWord return the secretWord, I went back and changed it so it returns 11 questions marks)The value of disguisedWord is 11 questions marks (disguising the string 'assessments'). I feel like this function should replace a question mark with index of a correct guess. It does not do this

    --- Update ---

    c = e, letter = e, and the positon(index) is 3 which is all correct. it does this twice, instead of finding the 2nd instance of e which should return postion 7....I guess the params of the replace method must be incorrect, I will try working on this.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Method error for checking and updating guess in a basic hangman game

    I feel like this function should replace a question mark with index of a correct guess
    Read the API doc for the replace() method.
    When you call it, you did not pass it an index.
    If all the char in a String are the same(?) how would the method know which one to replace?

    After you are done reading the API doc for the replace() method, read about the StringBuilder class.
    If you don't understand my answer, don't ignore it, ask a question.

  7. The Following User Says Thank You to Norm For This Useful Post:

    lotusmind (April 13th, 2013)

  8. #7
    Junior Member
    Join Date
    Apr 2013
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Method error for checking and updating guess in a basic hangman game

    Ok thank you I will check out the doc - unfortunately for the purposes of this program I cannot use the StringBuilder class.

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Method error for checking and updating guess in a basic hangman game

    If you can't use StringBuilder class, you'll have to build Strings using substrings and concatenation.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #9
    Junior Member
    Join Date
    Apr 2013
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Method error for checking and updating guess in a basic hangman game

    Hmmm, I see. I will spend time trying this on my own to hopefully better grasp the logic and application. Thank you for your help thus far.

  11. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Method error for checking and updating guess in a basic hangman game

    Given the String: "1234" and you want to replace the 3 with an "X"
    newString = "12" + "X" + "4";

    Using substring to get the beginning and ending Strings.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #11
    Junior Member
    Join Date
    Apr 2013
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Method error for checking and updating guess in a basic hangman game

    something in the realm of.... newString = firstString.substring(0,1) + 'x' + firstString.substring(3)
    So in my program, disguisedWord is all question marks. The index of a correct guess is int position = secretWord.indexOf(c);
    so the substring of disguisedWord should go up to the position, then concat "c", and then concat the rest of the string....ah, it's so hard to keep track of where everything is >.<

  13. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Method error for checking and updating guess in a basic hangman game

    Use a pencil and paper. draw a String and mark the spots and lengths needed for the substring method.
    Write a small testing program and experiment.


    I'm done for tonight.
    If you don't understand my answer, don't ignore it, ask a question.

  14. #13
    Junior Member
    Join Date
    Apr 2013
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Method error for checking and updating guess in a basic hangman game

    Okay, thank you for all your help - I appreciate it. Have a good night.

    --- Update ---

    Updating with new progress - thanks for the tips Norm! I'm nearly done now. Here is what I came up with and it's working:
    int position = i;
    disguisedWord = disguisedWord.substring(0, position) + c + disguisedWord.substring(position + 1, Math.max(0, disguisedWord.length()));
    this is working great, now i just have some smaller problems in the same method (makeGuess) that always hits the Else statement but I can figure this out. Thanks again!

Similar Threads

  1. hangman game
    By candoa in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 10th, 2012, 10:10 PM
  2. Hangman game
    By candoa in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 9th, 2012, 10:07 PM
  3. Help with 3 guess Dice game
    By tabmanmatt in forum Loops & Control Statements
    Replies: 1
    Last Post: November 28th, 2012, 07:42 PM
  4. Basic Java Calculating Cumulative Score - Hangman Help
    By ashboi in forum Java Theory & Questions
    Replies: 3
    Last Post: November 8th, 2012, 08:49 AM
  5. Hangman game HELP!
    By KingFisher in forum What's Wrong With My Code?
    Replies: 11
    Last Post: September 9th, 2010, 04:23 AM