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

Thread: Hangman game for JAVA Beginning class

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Hangman game for JAVA Beginning class

    I need to write the code for Hangman. I have written what I could but can't figure out rest of the code. Hopefully someone can help me out. I have two different classes: Hangman & Dictionary.
    Attached Files Attached Files


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Hangman game for JAVA Beginning class

    I think your plead for help is somewhat vague because first of all, a lot of people will avoid opening attached files, we'd rather see you use the Java highlight tags to put your code inline in the post.

    Second, the issue at hand you seem to have is that SOMETHING is wrong or missing and I don't think people will like spending their time helping you by first having to try and figure out what your program does and what is actually missing.

    Could you please be more specific about what area of your Hangman program you are having a problem with, trying to isolate each question to a smaller bit of the code?

    Thanks!

  3. #3
    Junior Member
    Join Date
    May 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Hangman game for JAVA Beginning class

    Thanks for replying. Here is what I have so far.
    The problem I'm having is it doesn't loop correctly.

    PHP Code:
    // This is Hangman.java
    import java.util.Scanner;
    import java.text.DateFormat;
    import java.util.Date;
    import javax.swing.JOptionPane;
    public class 
    Hangman extends Dictionary 
    {
            
        public static 
    void main(String[] args
        {
            
    // Create a scanner object named sc
            
    Scanner sc = new Scanner (System.in);
            
            
    //Continue the game untill user chooses No
            
    String choice "y";
            while (
    choice.equalsIgnoreCase"y"))
            {
                
    //System.out.println ("Welcome to game Hangman ");
                // To display hangman message
                
    JOptionPane.showMessageDialog(null"Welcome to game Hangman");
                
    System.out.println ();
            
                
    System.out.print ("Please enter your name: ");
                
    sc = new Scanner (System.in);
                
    String name sc.nextLine();
                    
                
    // Getting today's date
                
    Date now = new Date ();
            
                
    System.out.println ("Date: " DateFormat.getDateInstance(DateFormat.SHORT).format (now));
                
    System.out.println (name ", purpose of this game is to guess the secret word.");
                
    System.out.println ("You have six guesses remaining.");
                
    System.out.println ("Good luck!");
                
    System.out.println (" ");

                
    Dictionary myDictionary = new Dictionary();
                
    String secretWord myDictionary.getRandomWord();
                
    int emptyField secretWord.length();
                
    String blanks "";
                for(
    int i 0emptyFieldi++)
                {
                    
    blanks blanks " _ ";
                }
            
                
    System.out.println (blanks);
                
    System.out.println (" ");
                
    drawFigure(0);
                
    int badGuessesRemaining 6;
                
    String word secretWord;
                
                
    String[] secretWordArray = new String [word.length()];
                
    String[] displayWord = new String [word.length()];
                
    String[] badGuesses = new String [badGuessesRemaining];
                
                while(
    badGuessesRemaining != 0)
                {
                    
    System.out.print ("Please enter your guess: ");
                    
    String guess sc.nextLine();            

                    for (
    int i=0i<displayWord.length;i++)
                    {
                        
    displayWord[i] = "_";
                    }
                    
                    
    //save word as array
                    
    for (int i=0i<word.length(); i++)
                    {
                        
    secretWordArray[i] = word.substring(i,i+1);
                    }
                    
                    
    //check for match
                    
    boolean match false;
                    for (
    int i=0secretWordArray.lengthi++)
                    {
                        if (
    secretWordArray[i].equals(guess))
                        {
                            
    displayWord[i] = secretWordArray[i];
                            
    match true;
                        }    
                    }
                    if (!
    match)
                    {
                        
    badGuesses[badGuessesRemaining 1] = guess;
                        
    badGuessesRemaining--;
                    }
                        
                    
    //show display word
                    
    for (int i=0i<displayWord.length;i++)
                    {
                        
    System.out.print(displayWord[i] + " ");
                    }
                    
                    
    System.out.print("Do you wish to play this game again? (y/n): ");
                    
    choice sc.next();
                    
    System.out.println();
                
                }
                
            }
                
                    
    //System.out.println("The secret word is " + secretWordArray + " and it has these amount of characters. " );
        
    }
            
        static 
    void drawFigureint badGuesses){
        if (
    badGuesses == 0)
        {
            
    System.out.println("  ___");
            
    System.out.println(" |   |");
            
    System.out.println(" |   ");
            
    System.out.println(" |");
            
    System.out.println(" |");
            
    System.out.println("_|_");
        }
        else if (
    badGuesses == 1)
        {
            
    System.out.println("  ___");
            
    System.out.println(" |   |");
            
    System.out.println(" |   0");
            
    System.out.println(" |");
            
    System.out.println(" |");
            
    System.out.println("_|_");
        }
        else if (
    badGuesses == 2)
        {
            
    System.out.println("  ___");
            
    System.out.println(" |   |");
            
    System.out.println(" |   0");
            
    System.out.println(" |  -");
            
    System.out.println(" |");
            
    System.out.println("_|_");    
        }
        else if (
    badGuesses == 3)
        {
            
    System.out.println("  ___");
            
    System.out.println(" |   |");
            
    System.out.println(" |   0");
            
    System.out.println(" |  - +");
            
    System.out.println(" |");
            
    System.out.println("_|_");    
        }
        else if (
    badGuesses == 4)
        {
            
    System.out.println("  ___");
            
    System.out.println(" |   |");
            
    System.out.println(" |   0");
            
    System.out.println(" |  - + -");
            
    System.out.println(" |");
            
    System.out.println("_|_");    
        }
        else if (
    badGuesses == 5)
        {
            
    System.out.println("  ___");
            
    System.out.println(" |   |");
            
    System.out.println(" |   0");
            
    System.out.println(" |");
            
    System.out.println(" |  /");
            
    System.out.println("_|_");    
        }
        else if (
    badGuesses == 6)
        {
            
    System.out.println("  ___");
            
    System.out.println(" |   |");
            
    System.out.println(" |   0");
            
    System.out.println(" |");
            
    System.out.println(" |  / '\'");
            
    System.out.println("_|_");    
        }}}
    /*
     for (int = 0; i < incorrect.length; i++)
     {
         System.out.print (incorrect [i] + " ");
     }
    */ 
    PHP Code:
    // This is Dictionary.java
    import java.util.Random;
    public class 
    Dictionary
    {
        private static 
    String[] words = new String [22];
        
        
    Dictionary()
        {
            
    words [0] = "dictionary";
            
    words [1] = "restaurant";
            
    words [2] = "television";
            
    words [3] = "responsible";
            
    words [4] = "technology";
            
    words [5] = "computer";
            
    words [6] = "communicate";
            
    words [7] = "automibile";
            
    words [8] = "coffee";
            
    words [9] = "federation";
            
    words [10] = "exaggerate";
            
    words [11] = "cappuccino";
            
    words [12] = "forensics";
            
    words [13] = "laboratory";
            
    words [14] = "lamborghini";
            
    words [15] = "ferrari";
            
    words [16] = "abandoned";
            
    words [17] = "residents";
            
    words [18] = "california";
            
    words [19] = "undergraguate";
            
    words [20] = "hangman";
            
    words [21] = "reference";    
        }
        
    String getRandomWord()
        {
            
    Random rnd = new Random ();
            
    int n rnd.nextInt (words.length);
            return 
    words [n];        
        }} 

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    20,952
    Thanks
    51
    Thanked 2,267 Times in 2,239 Posts

    Default Re: Hangman game for JAVA Beginning class

    it doesn't loop correctly
    Please explain.
    For example execute the code, copy the output and paste here and explain what is wrong with the output, show what the correct output would be and explain how it needs to work.

Similar Threads

  1. Hangman Game
    By Snow_Fox in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 29th, 2010, 03:07 PM
  2. Restarting at the beginning of an array
    By bonbon242 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 31st, 2010, 10:58 AM
  3. Hangman game HELP!
    By KingFisher in forum What's Wrong With My Code?
    Replies: 11
    Last Post: September 9th, 2010, 04:23 AM
  4. Java hangman game help...
    By AnotherNoob in forum AWT / Java Swing
    Replies: 16
    Last Post: December 4th, 2009, 10:17 PM
  5. Beginning java programmer!!!!!!!!!!!!!!!!!! need help
    By raidcomputer in forum Java Theory & Questions
    Replies: 3
    Last Post: September 15th, 2009, 08:52 PM