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 3 of 3

Thread: Method in hangman game not working

  1. #1
    Junior Member
    Join Date
    Feb 2019
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Method in hangman game not working

    Hi all,

    I'm currently developing a hangman game for my assignment however one of my method does not give me the result i wanted.

    Expected output with disguised word as HTML (case-insensitive)

    The disguised word is ****
    Guess a character:
    h
    Incorrect. No. of guesses made is 1 with 1 wrong
     
    The disguised word is h***
    Guess a character:
    m
    Incorrect. No. of guesses made is 2 with 2 wrong
     
    The disguised word is h*m*
    Guess a character:
    k
    Incorrect. No. of guesses made is 3 with 3 wrong
     
    The disguised word is h*m*
    Guess a character:
    t
    Correct. No. of guesses made is 4 with 4 wrong
    Good job, you found the secret word which is html

    As seen in the output above input "h" & "m" should return Correct instead of incorrect.

    Hangman.java

    public class Hangman {
     
        private String secretWord;
        private String disguisedWord = "";
        private String result;
        private int guesses = 0;
        private int wrongGuess = 0;
     
        public void setSecretWord(String word) {
            secretWord = word;
        }
     
        public void setDisguisedWord() {
            for (int i = 0; i < secretWord.length(); i++) {
                disguisedWord += "?";
            }
        }
     
        public void guesses() {
            guesses++;
        }
     
        public boolean guessLetter(char c) {
            for (int i = 0; i < disguisedWord.length(); i++) {
                if (c == secretWord.charAt(i)) {
                    disguisedWord = disguisedWord.substring(0, i) + c + disguisedWord.substring(i + 1);
                    result = "Correct.";
                } else {
                    result = "Incorrect.";
                }
            }
            wrongGuess++;
     
            if (secretWord.equals(disguisedWord)) {
                return true;
            } else {
                return false;
            }
        }
     
        public String getDisguisedWord() {
            return disguisedWord;
        }
     
        public String getResult() {
            return result;
        }
     
        public int getGuesses() {
            return guesses;
        }
     
        public int getWrongGuess() {
            return wrongGuess;
        }
     
        public String getSecretWord() {
            return secretWord;
        }
     
    }

    Game.java

     
    import java.util.Scanner;
     
    public class Game {
     
        public static void main(String[] args) {
     
            String word1 = "html";
            String word2 = "css";
            String word3 = "java";
     
            Scanner kb = new Scanner(System.in);
     
            Hangman game = new Hangman();
            game.setSecretWord(word1);
            game.setDisguisedWord();
     
            System.out.println("Let's play a round of hangman.");
            System.out.println("We are playing hangman");
     
            while (true) {
                System.out.println("");
                System.out.println("The disguised word is " + game.getDisguisedWord());
                System.out.println("Guess a letter:");
                char guess = kb.next().charAt(0);
                game.guessCount();
                boolean isFound = game.guessLetter(guess);
                if (isFound) {
                    System.out.println(game.getResult() + " No. of guesses made is " + game.getGuesses() + " with " + game.getWrongGuess() + " wrong");
                    System.out.println("Good job, you found the secret word which is " + game.getSecretWord());
                    break;
                }
                else {
                    System.out.println(game.getResult() + " No. of guesses made is " + game.getGuesses() + " with " + game.getWrongGuess() + " wrong");
                }
            }
     
        }
     
    }

  2. #2
    Junior Member
    Join Date
    Feb 2019
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Method in hangman game not working

    Hi all,

    I'm currently developing a hangman game for my assignment however one of my method does not give me the result i wanted.

    Expected output with disguised word as HTML (case-insensitive)

    The disguised word is ****
    Guess a character:
    h
    Incorrect. No. of guesses made is 1 with 1 wrong
     
    The disguised word is h***
    Guess a character:
    m
    Incorrect. No. of guesses made is 2 with 2 wrong
     
    The disguised word is h*m*
    Guess a character:
    k
    Incorrect. No. of guesses made is 3 with 3 wrong
     
    The disguised word is h*m*
    Guess a character:
    t
    Correct. No. of guesses made is 4 with 4 wrong
    Good job, you found the secret word which is html

    As seen in the output above input "h" & "m" should return Correct instead of incorrect.

    Hangman.java

    public class Hangman {
     
        private String secretWord;
        private String disguisedWord = "";
        private String result;
        private int guesses = 0;
        private int wrongGuess = 0;
     
        public void setSecretWord(String word) {
            secretWord = word;
        }
     
        public void setDisguisedWord() {
            for (int i = 0; i < secretWord.length(); i++) {
                disguisedWord += "?";
            }
        }
     
        public void guesses() {
            guesses++;
        }
     
        public boolean guessLetter(char c) {
            for (int i = 0; i < disguisedWord.length(); i++) {
                if (c == secretWord.charAt(i)) {
                    disguisedWord = disguisedWord.substring(0, i) + c + disguisedWord.substring(i + 1);
                    result = "Correct.";
                } else {
                    result = "Incorrect.";
                }
            }
            wrongGuess++;
     
            if (secretWord.equals(disguisedWord)) {
                return true;
            } else {
                return false;
            }
        }
     
        public String getDisguisedWord() {
            return disguisedWord;
        }
     
        public String getResult() {
            return result;
        }
     
        public int getGuesses() {
            return guesses;
        }
     
        public int getWrongGuess() {
            return wrongGuess;
        }
     
        public String getSecretWord() {
            return secretWord;
        }
     
    }

    Game.java

     
    import java.util.Scanner;
     
    public class Game {
     
        public static void main(String[] args) {
     
            String word1 = "html";
            String word2 = "css";
            String word3 = "java";
     
            Scanner kb = new Scanner(System.in);
     
            Hangman game = new Hangman();
            game.setSecretWord(word1);
            game.setDisguisedWord();
     
            System.out.println("Let's play a round of hangman.");
            System.out.println("We are playing hangman");
     
            while (true) {
                System.out.println("");
                System.out.println("The disguised word is " + game.getDisguisedWord());
                System.out.println("Guess a letter:");
                char guess = kb.next().charAt(0);
                game.guessCount();
                boolean isFound = game.guessLetter(guess);
                if (isFound) {
                    System.out.println(game.getResult() + " No. of guesses made is " + game.getGuesses() + " with " + game.getWrongGuess() + " wrong");
                    System.out.println("Good job, you found the secret word which is " + game.getSecretWord());
                    break;
                }
                else {
                    System.out.println(game.getResult() + " No. of guesses made is " + game.getGuesses() + " with " + game.getWrongGuess() + " wrong");
                }
            }
     
        }
     
    }

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Method in hangman game not working

    How are you trying to debug the code to see why it is not returning the desired results?

    Add some print statements that print out the values of variables as the code is executed so you can see what the code is doing.
    For example in for loop in the guessLetter method print the value of c and result and i after each time their values are changed.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Hangman Game
    By JohanMartin in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 18th, 2014, 06:25 AM
  2. Method error for checking and updating guess in a basic hangman game
    By lotusmind in forum What's Wrong With My Code?
    Replies: 12
    Last Post: April 13th, 2013, 10:03 PM
  3. Hangman Game
    By connorlm3 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 7th, 2013, 12:50 PM
  4. hangman game
    By candoa in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 10th, 2012, 10:10 PM
  5. Hangman game HELP!
    By KingFisher in forum What's Wrong With My Code?
    Replies: 11
    Last Post: September 9th, 2010, 04:23 AM