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

Thread: Help with java hangman game(beginner)

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default need help with hangman game project(begginer)

    I have been assignedfrom my university teacher to write a java source code for a hangman game.I have been stuck on the guess method where the player guess a character(especially when he's right).Here is what I have done so far!
    <public class Hangman
    {
     
        private static int ALLOWDED_MISTAKES=7;
        private String secretWord;
        private int noOfMistakes;
        private int chances=7-noOfMistakes;
     
        public  Hangman()
        {
            newGame();
        }//Hangman constructor
     
        public void newGame()
        {
            noOfMistakes=0;
            secretWord=Words.randomWord();
                    System.out.println();
            System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * * * * * *");
            System.out.println("* * * * * * * * * *  N E W   G A M E  * * * * * * * * * *");
            System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * * * * * *");
            System.out.println();
            System.out.print("The word is:");
            for (int i=1;i<=secretWord.length();i++)
              System.out.print("_");
            System.out.println();
            System.out.println();
            System.out.println("You have 7 chances to find the word");
            System.out.println("use method \"guess(char c)\" to guess a character");
            System.out.println("from: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z");
            System.out.println("or use method \"giveUp()\" to stop playing");
     
        }//newGame
     
        public void giveUp()
        {
            System.out.println();
            System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * * * * * *");
            System.out.println("* * * * * * * * * * * *G I V E  U P * * * * * * * * * * *");
            System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * * * * * *");
            System.out.println();
            System.out.println("The hidden word is "+secretWord);
            System.out.println();
            System.out.println("Use method \"newGame()\"to start a new game.");
        }//giveUp
     
     
         public void guess(char c)
     
     
     
     
                    if (!Character.isLetter(c)){
                              System.out.println("Only enter letters!");
     
                    }            
     
     
     
     
     
                    if (secretWord.indexOf(c) == -1){
                            noOfMistakes++;
                            System.out.println("wrong guess,try again");
                            System.out.println("your remaining chances are "+chances);
                                                    if (noOfMistakes==ALLOWDED_MISTAKES){
                                    System.out.println();
                             System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * * * * * *");
                             System.out.println("* * * * * * * * * * * *YOU LOST * * * * * * * * * * *");
                             System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * * * * * *");
                             System.out.println();
                             System.out.println("The hidden word is "+secretWord);
                             System.out.println();
                             System.out.println("Use method \"newGame()\"to start a new game.");
                            }
     
     
                    }
     
     
     
                    }
     
        }//class
    }>
    Please help me.Feel free to change anything you want but remember this is for a beginner so try to kee it simple.The player has to use the methods by himself.I'm using bluej .Finally the class Words was given by the teacher and is that :
    < import java.util.ArrayList;
    import java.util.Random;
    /**
     * Provides static method for choosing randomly a word from a dictionary
     * 
     * @author (CR) 
     * @version (11/2012)
     */
    public class Words
    {
       //class variables
       private static ArrayList<String> dictionary=new ArrayList<String>();
       private static Random random=new Random();
     
       /*
        * Generates a list of words
        */
       private static void generateWords()
       {
           String[] words={"ALCOHOL","AMBULANCE","ANSWER", "AUDIENCE", "AUTOMN", 
               "BLANKET", "BRIDGE",
               "CARPENTER", "CHICKEN", "COMFORTABLE", "COMPUTER", "CONFIDENCE", "COUNTRY",
               "DANGEROUS", "DAUGHTER", "DESCRIBE", "DIRECTLY", "DISTANCE",
               "EQUIPMENT", "EXAMPLE", "EXPERIMENT",
               "FAIRYTALE", "FISHERMAN", "GOVERNMENT",
               "IMAGINATION", "IMPORTANCE", "INDUSTRY", "INFLUENCE", "INSTRUMENT",
               "KEYHOLE", "KNOWLEDGE",
               "LANGUAGE", "MACHINE", "NATURAL", "ORDINARY",
               "PICTURE", "PLEASURE", "POPULATION", "POWERFUL", "QUESTION",
               "STRANGE", "SUBSTANCE", "SURFACE", "SURPRISE", "SYSTEM",
               "TEMPERATURE", "TRIANGLE",
               "UNKNOWN", "WORKER", "YOUTHFUL"};
           for(int i=0;i<words.length;i++)
            dictionary.add(words[i]);
       }//generateWords
     
       /**
        * Creates a list of words and picks one in random
        * 
        * @return   the chosen word
        */
       public static String randomWord()
       {
           generateWords();
           return (String) dictionary.get(random.nextInt(dictionary.size()));
       }//randomWord
    }//class>



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

    Default Re: need help with hangman game project(begginer)

    I have been stuck on the guess method
    Can you explain what the problem is you are stuck on? What are you having problems with?

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: need help with hangman game project(begginer)

    I don't know how to write the code for when the player guess a letter and he is right.How to relpace the proper _ with the letter he guessed!

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

    Default Re: need help with hangman game project(begginer)

    How to relpace the proper _ with the letter he guessed!
    If the word was: BLANKET and the user guessed: N
    then what would the code do?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Dec 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with java hangman game(beginner)

    here's the code of a simple hangman game,I have to do for homework.I'm a beginner and I have stuck in the method guess.
    <import java.lang.Object;
    /**
     * Write a description of class Hangman here.
     * 
     * @author (your name) 
     * @version (a version number or a date)
     */
    public class Hangman
    {
     
        private static int ALLOWDED_MISTAKES=7;
        private String secretWord;
        private int noOfMistakes;
        private int chances=7-noOfMistakes;
     
        public  Hangman()
       {
            newGame();
        }//Hangman constructor
     
        public void newGame()
        {
            noOfMistakes=0;
            secretWord=Words.randomWord();
                    System.out.println();
            System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * * * * * *");
            System.out.println("* * * * * * * * * *  N E W   G A M E  * * * * * * * * * *");
            System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * * * * * *");
            System.out.println();
            System.out.print("The word is:");
            for (int i=1;i<=secretWord.length();i++)
              System.out.print("_ ");
           System.out.println();
            System.out.println();
            System.out.println("You have 7 chances to find the word");
           System.out.println("use method \"guess(char c)\" to guess a character");
            System.out.println("from: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z");
            System.out.println("or use method \"giveUp()\" to stop playing");
     
        }//newGame
     
        public void giveUp()
        {
            System.out.println();
            System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * * * * * *");
            System.out.println("* * * * * * * * * * * *G I V E  U P * * * * * * * * * * *");
           System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * * * * * *");
            System.out.println();
            System.out.println("The hidden word is "+secretWord);
            System.out.println();
            System.out.println("Use method \"newGame()\"to start a new game.");
        }//giveUp
     
     
         public void guess(char c)
     
     
     
     
     
                    if (!Character.isLetter(c)){
                             System.out.println("Only enter letters!");
     
                   }
     
     
     
     
     if (secretWord.indexOf(c) == -1){
                            noOfMistakes++;
                            System.out.println("wrong guess,try again");
                            System.out.println("your remaining chances are "+chances);
                                                    if (noOfMistakes==ALLOWDED_MISTAKES){
                                    System.out.println();
                             System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * * * * * *");
                             System.out.println("* * * * * * * * * * * *YOU LOST * * * * * * * * * * *");
                             System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * * * * * *");
                             System.out.println();
                             System.out.println("The hidden word is "+secretWord);
                             System.out.println();
                             System.out.println("Use method \"newGame()\"to start a new game.");
                            }
     
     
                    }
                    if (secretWord.indexOf(c) !== -1)
     
     
     
                    }
     
       }//class
    }>

    I don't know how to write the code for when the player guess a letter and he is right.How to relpace the proper _ with the letter he guessed!I have to mainly use the java.lang package. feel free to change whatever you want,but please keep it simple because I'm a beginner.The class Words were given by the professor
    <import java.util.ArrayList;
    import java.util.Random;
    /**
     * Provides static method for choosing randomly a word from a dictionary
     * 
     * @author (CR) 
     * @version (11/2012)
     */
    public class Words
    {
       //class variables
       private static ArrayList<String> dictionary=new ArrayList<String>();
       private static Random random=new Random();
     
       /*
        * Generates a list of words
        */
       private static void generateWords()
       {
           String[] words={"ALCOHOL","AMBULANCE","ANSWER", "AUDIENCE", "AUTOMN", 
               "BLANKET", "BRIDGE",
               "CARPENTER", "CHICKEN", "COMFORTABLE", "COMPUTER", "CONFIDENCE", "COUNTRY",
               "DANGEROUS", "DAUGHTER", "DESCRIBE", "DIRECTLY", "DISTANCE",
               "EQUIPMENT", "EXAMPLE", "EXPERIMENT",
               "FAIRYTALE", "FISHERMAN", "GOVERNMENT",
               "IMAGINATION", "IMPORTANCE", "INDUSTRY", "INFLUENCE", "INSTRUMENT",
               "KEYHOLE", "KNOWLEDGE",
               "LANGUAGE", "MACHINE", "NATURAL", "ORDINARY",
               "PICTURE", "PLEASURE", "POPULATION", "POWERFUL", "QUESTION",
               "STRANGE", "SUBSTANCE", "SURFACE", "SURPRISE", "SYSTEM",
               "TEMPERATURE", "TRIANGLE",
               "UNKNOWN", "WORKER", "YOUTHFUL"};
           for(int i=0;i<words.length;i++)
            dictionary.add(words[i]);
       }//generateWords
     
       /**
        * Creates a list of words and picks one in random
        * 
        * @return   the chosen word
        */
       public static String randomWord()
       {
           generateWords();
           return (String) dictionary.get(random.nextInt(dictionary.size()));
       }//randomWord
    }//class
    >

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

    Default Re: Help with java hangman game(beginner)

    Is this the same question?
    http://www.javaprogrammingforums.com...-begginer.html


    Threads Merged.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Hangman game loop
    By Grocerybag in forum Loops & Control Statements
    Replies: 2
    Last Post: November 6th, 2012, 08:18 PM
  2. Hangman game for JAVA Beginning class
    By shahravi88 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 12th, 2011, 03:15 PM
  3. Hangman Game
    By Snow_Fox in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 29th, 2010, 04:07 PM
  4. Hangman game HELP!
    By KingFisher in forum What's Wrong With My Code?
    Replies: 11
    Last Post: September 9th, 2010, 04:23 AM
  5. Java hangman game help...
    By AnotherNoob in forum AWT / Java Swing
    Replies: 16
    Last Post: December 4th, 2009, 11:17 PM

Tags for this Thread