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

Thread: hangman game

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

    Question hangman game

    /*the file that im reading from has 40 lines of words. I need to read from file whatever the size of the file and assign the amount of word from the file to the array size. thank you in advance
    */


    public static void main(String[] args) throws FileNotFoundException {
    // TODO Auto-generated method stub
    printIntro();

    String[] words = new String[40];// size of the array
    readWords(words);// this method store all words in an array
    String secretWord = getWord(words);// this method choose a random word
    char status[] = new char[secretWord.length()];// takes the length
    for (int i = 1; i < secretWord.length(); i++) {//set the limit of the for loop
    status[i] = '?';//puts a ? symbol in every token of the string
    }
    Scanner console = new Scanner(System.in);
    int tries = 6;//hold number of misses in the program
    while (tries > 0) {
    System.out.println("Enter a letter:");
    char guessLetter = (console.next()).charAt(0);//hold a char and store it in guessletter
    int hits = processGuess(guessLetter, secretWord, status);//this method test whether the guessletter was found in the string and returns the number of char found
    displayWord(secretWord, status);//this method display the word found and the misses

    if (hits > 0) {//test if the char was found
    System.out.println("found!" + guessLetter);
    boolean finished = true;//holds value to exit for loop
    for (int i = 0; i < secretWord.length(); i++) {
    if (status[i] == '?') {//test whether the array as symbol ?. if it does then the word is not complete
    finished = false;//false indicates that the array still have unsolved letters
    }
    }
    if (finished) {//if boolean is true finish this program
    System.out.println("congratualition");
    break;//exit while loop
    }
    } else if (hits == 0) {//if hits don't execute the letter was not found.
    System.out.println("sorry no " + guessLetter);
    tries--;//decrease the number of misses
    }
    System.out.println(tries + " remaining misses");

    if (tries == 0) {//execute when all tries have being used.
    System.out.println("Sorry you lose, the word was \'"
    + secretWord + "\'");
    }
    }
    }

    public static void printIntro() {//prints the introduction of the program
    System.out.println("Welcome to hangman try to guess the secret word,"
    + " you have 6 chances.");
    }

    public static void readWords(String[] words) {//read all the words from the file
    File file = new File("words.txt");//set direction of the file
    try { //
    Scanner console = new Scanner(file);//scanner for the file
    int index = 0;//to fill in all the elements of the array
    while (console.hasNextLine()) {
    String word = console.nextLine();
    words[index] = word;//store word in the array
    index++;//move index of the array
    }

    } catch (FileNotFoundException e) {//throw exception not found
    e.printStackTrace();
    }
    }

    public static String getWord(String[] words) {//method would pick a random word from file
    Random rand = new Random();
    int index = rand.nextInt(40);//choose any element
    String secretWord = words[index];//choose a random word from a words array
    return secretWord;//return the random word
    }

    public static int processGuess(char guess, String secretWord, char[] status) {//this method test whether the letter was found
    int count = 0;
    for (int i = 0; i < secretWord.length(); i++) {
    if (guess == secretWord.charAt(i)) {//test if the the user input guess letter was found
    count++;
    status[i] = '+';//store + in every element found
    }
    }
    return count;
    }

    public static void displayWord(String secret, char[] status) {
    for (int i = 0; i < secret.length(); i++) {
    if (status[i] == '+') {//if it has a + then the letter was found
    System.out.print(secret.charAt(i));//print the letter
    } else {
    System.out.print("_");//if not found
    }
    }
    System.out.println();//to get a new blank line
    }
    }


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: hangman game

    Your post doesn't ask a question, and you've still got an active question regarding this program, one which has a response that you never replied to. Locking this. Please reply to Norm, and if you post a question here, please actually ask a question that is separate from the code so that it can easily be seen to be a question. You'll also want to read the FAQ to learn about use of [code] [/code] tags so that your code will show its formatting.

Similar Threads

  1. Hangman game
    By candoa in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 9th, 2012, 10:07 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