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

Thread: Refining some simple "Hangman" code

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Refining some simple "Hangman" code

    Hello, I am very new here (and to Java as a whole), so I apologize if I've made a mistake.

    Basically, I'm asking for some advice on how I can make this code so that it is all within the same class, rather than working as 2 separate classes. This is a simple text-based hangman game. As of now, I have 2 separate files, one containing my main class, and one with the game class (containing all of the rules). Sorry if that doesn't make much sense, I'm not sure how to explain it better.


    Here is the game class:

    import java.util.Scanner;
     
    public class game 
    {
        Scanner keyboard = new Scanner (System.in);
        public String secretWord;
        public String disguisedWord = "";
        public String guessedLetters = "";
        private int numberOfGuesses = 0;
        private int numberOfWrongGuesses = 0;
     
        public void setSecretWord(String secretWord)
        {
            this.secretWord = secretWord;
            for(int i = 0; i < secretWord.length(); i++)
            {
                this.disguisedWord += "?";
            }
     
        }
        public boolean makeGuess(char guess)
        {
            guessedLetters += guess;
            String tempString = "";
            numberOfGuesses ++;
            for(int i = 0; i < secretWord.length(); i++)
            {
                if(guess == secretWord.charAt(i))
                    tempString += guess;
                else
                    tempString += disguisedWord.charAt(i);
     
            }
            if(!tempString.equalsIgnoreCase(disguisedWord))
            {
                disguisedWord = tempString;
                return true;
            }
            else
            {
                numberOfWrongGuesses++;
                return false;
            }        
        }
     
        public String getDisguisedWord()
        {
            System.out.print("The disguisedWord is ");
            return disguisedWord;
        }
     
        public String getSecretWord()
        {
            return secretWord;
        }
     
     
        public void guessResponse()
        {
            if(numberOfGuesses > 0)
            {
     
                System.out.print("Guesses made " + numberOfGuesses
                        + " times.");
                System.out.print(" with " + numberOfWrongGuesses
                        + " wrong");
                System.out.println("(" + guessedLetters
                        + ")");
            }
            System.out.println("Guess a letter: ");
        }
     
        public boolean isFound()
        {
            if (secretWord.equalsIgnoreCase(disguisedWord))
            {
                System.out.println("Congratulations, you found the secret word: " + disguisedWord);
                return true;
            }
            else
            {
                return false;
            }
        }
    }

    ...and the main class:

    import java.util.Scanner;
     
    public class Hangman 
    { 
        public static void main(String[] args) 
        {
            Scanner keyboard = new Scanner (System.in);
            game hangman = new game();
            System.out.println("Lets play Hang Man");
            System.out.println("Enter secret word");
            String secretWord = keyboard.next();
            hangman.setSecretWord(secretWord);
            System.out.println();
            while(hangman.isFound() == false)
            {
                System.out.println(hangman.getDisguisedWord());
                hangman.guessResponse();
                String guess = keyboard.next();
                hangman.makeGuess(guess.charAt(0));
                System.out.println();
     
            }
            System.out.println();
            System.out.println("Thanks for playing Hangman.");
     
        }
    }

    My instructor suggested using a main method that looks more like the following, but I just don't have the capacity to understand how to make it work with what I've got.

    public static void main(String[] args) {
    Hangman game = new Hangman();
    game.initialize("Happiness");
    System.out.println("Lets play a round of hangman.");
    game.playGame();
    }

    Thanks!


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Refining some simple "Hangman" code

    One of the better first posts I've seen. Thanks for taking the time to figure out how it should be done.

    Can you explain WHY you want to combine the 2 classes into one? Is the instructor requiring it? Is the desired result a single class in a single file, or could the result be a single file containing more than 1 class?

  3. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Refining some simple "Hangman" code

    Hi Greg,
    In my mind this was easier done with 2 separate files and classes as I did. However the instructor clearly stated that I should submit only a single Java file named Hangman.java. I think this would suggest having only a single class in a single file.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Refining some simple "Hangman" code

    Okay. It's important to understand the requirement, but the instructor's requirement could be met by a single file with multiple classes which would present the simples conversion from what you have. It's simple and would look like this:
    import java.util.Scanner;
     
    public class Hangman 
    { 
        public static void main(String[] args) 
        {
            Scanner keyboard = new Scanner (System.in);
            Game hangman = new Game();
            System.out.println("Lets play Hang Man");
            System.out.println("Enter secret word");
            String secretWord = keyboard.next();
            hangman.setSecretWord(secretWord);
            System.out.println();
            while(hangman.isFound() == false)
            {
                System.out.println(hangman.getDisguisedWord());
                hangman.guessResponse();
                String guess = keyboard.next();
                hangman.makeGuess(guess.charAt(0));
                System.out.println();
     
            }
            System.out.println();
            System.out.println("Thanks for playing Hangman.");
     
        }
    }
     
    class Game 
    {
        Scanner keyboard = new Scanner (System.in);
        public String secretWord;
        public String disguisedWord = "";
        public String guessedLetters = "";
        private int numberOfGuesses = 0;
        private int numberOfWrongGuesses = 0;
     
        public void setSecretWord(String secretWord)
        {
            this.secretWord = secretWord;
            for(int i = 0; i < secretWord.length(); i++)
            {
                this.disguisedWord += "?";
            }
     
        }
        public boolean makeGuess(char guess)
        {
            guessedLetters += guess;
            String tempString = "";
            numberOfGuesses ++;
            for(int i = 0; i < secretWord.length(); i++)
            {
                if(guess == secretWord.charAt(i))
                    tempString += guess;
                else
                    tempString += disguisedWord.charAt(i);
     
            }
            if(!tempString.equalsIgnoreCase(disguisedWord))
            {
                disguisedWord = tempString;
                return true;
            }
            else
            {
                numberOfWrongGuesses++;
                return false;
            }        
        }
     
        public String getDisguisedWord()
        {
            System.out.print("The disguisedWord is ");
            return disguisedWord;
        }
     
        public String getSecretWord()
        {
            return secretWord;
        }
     
     
        public void guessResponse()
        {
            if(numberOfGuesses > 0)
            {
     
                System.out.print("Guesses made " + numberOfGuesses
                                     + " times.");
                System.out.print(" with " + numberOfWrongGuesses
                                     + " wrong");
                System.out.println("(" + guessedLetters
                                       + ")");
            }
            System.out.println("Guess a letter: ");
        }
     
        public boolean isFound()
        {
            if (secretWord.equalsIgnoreCase(disguisedWord))
            {
                System.out.println("Congratulations, you found the secret word: " + disguisedWord);
                return true;
            }
            else
            {
                return false;
            }
        }
    }
    If you MUST reduce it to a single class, I think you should be able to figure that out from the code I've posted. If you still can't, come back.

  5. The Following User Says Thank You to GregBrannon For This Useful Post:

    skousey (October 23rd, 2013)

  6. #5
    Junior Member
    Join Date
    Oct 2013
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Refining some simple "Hangman" code

    I didn't know putting 2 classes in a single file was that simple - I guess it just hasn't really been shown in the textbook yet. Since it is a fully online course, the instructions can be hard to interpret, and the only requirement (other than saying what the code should do) was that it should be submitted in one file - so I'd imagine this should be fine. Thanks a lot.

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Refining some simple "Hangman" code

    I've never understood why the main() method is placed in a class by itself. I often add a main() method to a class for testing the class and provide an example of how to use the class.
    Why not simply move the main() method into the Game class and get rid of the Hangman class? Then it could make sense to rename the Game class to Hangman.

    Physically having multiple classes in one file is not logically different from having the classes in separate files. The only requirement is that one of the classes be public.
    If you don't understand my answer, don't ignore it, ask a question.

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

    skousey (October 23rd, 2013)

  9. #7
    Junior Member
    Join Date
    Oct 2013
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Refining some simple "Hangman" code

    Quote Originally Posted by Norm View Post
    Why not simply move the main() method into the Game class and get rid of the Hangman class? Then it could make sense to rename the Game class to Hangman.
    I tried pasting the main() method to the beginning of the Game class file and swapped the names around, and it seems to be working as intended. Thanks for the suggestion.

Similar Threads

  1. "Ask a Question" menu item dumps into "What's Wrong With My Code?"
    By GregBrannon in forum Forum Updates & Feedback
    Replies: 1
    Last Post: August 6th, 2013, 03:37 AM
  2. Replies: 2
    Last Post: June 22nd, 2013, 10:30 AM
  3. Replies: 3
    Last Post: December 7th, 2011, 02:03 AM
  4. Replies: 7
    Last Post: August 13th, 2011, 01:22 AM
  5. [SOLVED] "possible loss of precision", except not, code doesn't work, simple question
    By Perd1t1on in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 24th, 2010, 07:11 PM