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

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Hangman game

    import java.util.*;
    import java.io.*;
    public class lab10 {

    /**
    * @param args
    */
    public static void main(String[] args) throws FileNotFoundException{
    // TODO Auto-generated method stub
    printIntro();
    String [] words = new String [40];//size of the array
    Scanner console = new Scanner(System.in);
    System.out.println("Enter a letter");
    String guessLetter = console.next();//this store the letter the user most type
    readWords(words);//this method store all words in an array
    String secretWord = getWord(words);//this method choose a random word from 40 elements in the array and stores it.

    }
    public static void printIntro(){
    System.out.println("Welcome to hangman try to guess the secret word," +
    " you have 6 chances.");
    }
    public static void readWords (String[] words){
    File file = new File ("words.txt");
    try {
    Scanner console = new Scanner (file);
    int index = 0;
    while(console.hasNextLine()){
    String word = console.nextLine();
    words[index] = word;
    index++;
    }
    }
    catch (FileNotFoundException e) {
    e.printStackTrace();
    }
    }
    public static String getWord (String[] words){
    Random rand = new Random ();
    int index = rand.nextInt(40);
    String secretWord = words[index];
    return secretWord;
    }
    public static int processGuess (char guess, String secretWord, char[] status){

    }

    }
    Last edited by candoa; December 9th, 2012 at 08:46 PM. Reason: making more clear


  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: Hangman game

    Can you explain what you are having problems with? What is the method given as data and what is it supposed to do?

    Hiding your questions in with the code makes it hard to see them.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Hangman game

    I need to finish this two methods and I have no idea what to do. what I need to do basically is to pass 3 values a char, a string and a char[] status. I don't know how to convert the string word to a char so I can pass it as a char, because I trying to create the game hangman and I need to check if the letter than the user guess are in the word. this is the instructions.

    /* This method takes in a guess, the word and the array representing the hits and misses in the word and returns the number of hits
    (occurrences of the guess in the word). Note that the array representing the status of the word is also updated. Example: in the second
    turn in the example above, the user previously guessed a, so the word array that is passed to the method might have ? ? ? ? ? + ? ? in it.
    If the user now guesses ‘e’, the return value will be 2 (there are two e’s) and the updated array would be + ? + ? ? + ? ? to represent the
    letters that have been guessed. */
    int processGuess (char guess, String secretWord, char[] status)
    /* This method is responsible for displaying the status of the word. An underscore should be displayed for any un-guessed letters and
    the actual letter should be displayed for any letters that have been guessed */
    void displayWord(String secret, char[] status)

  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: Hangman game

    how to convert the string word to a char
    The String class has some methods that will help you get the char values out of a String.

    need to check if the letter than the user guess are in the word.
    If the word is a String, the String class has methods for testing if a String contains another String.

    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.

Similar Threads

  1. [SOLVED] need help with hangman game project(begginer)
    By alexander@semfe in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 9th, 2012, 01:35 PM
  2. Hangman game loop
    By Grocerybag in forum Loops & Control Statements
    Replies: 2
    Last Post: November 6th, 2012, 08:18 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