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
    Jan 2013
    Posts
    29
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Hangman Game

    Hi, i've been working on a hangman java game. I have a lot of it coded, but when I go to run it, Im getting stuck somewhere I believe on line 64. I put a print statement before line 64 and it is printing it, then the program doesnt do anything but it is still running. Can anyone tell me what I have wrong in my program? Thanks.

    /*
     * Mat2670 Homework 4 EX 1
     * 
     * Connor Moore
     * 03/07/2013
     * 
     * File: Hangman.java
     * This program gets a random word from a file that is
     * at least 5 characters long and lets a user play a 
     * game of hangman. 
     */
    package mat2670;
     
    import java.util.*;
    import java.io.*;
     
    public class Hangman {
     
        private static final int LENGTH = 5;
        private static final int MAXGUESS = 6;
        private static final int NUMCHARS = 26;
     
        public static void main(String[] args) {
     
            Random rand = new Random();
            ArrayList<String> words = new ArrayList<String>();
     
     
     
            Scanner input = null;
            try {
                input = new Scanner(new File("/usr/share/dict/words"));
            } catch (FileNotFoundException ex) {
                System.err.print("File Not Found!");
            }
     
     
            while (input.hasNextLine()) {
                String curWord = input.nextLine();
     
                if (curWord.length() >= LENGTH) {
                    for (int i = 0; i < curWord.length(); i++) {
                        words.add(i, curWord);
                    }
                    break;
                }
     
            }
     
     
            String gameWord = words.get(rand.nextInt(words.size()));
            char[] displayWord = gameWord.toCharArray();
            for (int k = 0; k < gameWord.length(); k++) {
                displayWord[k] = '_';
            }
     
            Scanner console = new Scanner(System.in);
            char[] correct = new char[NUMCHARS];
            char[] incorrect = new char[MAXGUESS];
            int y = 0;
     
            System.out.print("here"); //print for debugging purpose
     
            while (!Arrays.equals(gameWord.toCharArray(), displayWord)
                    && y <= MAXGUESS) {
     
                String guess = console.nextLine();
                if (gameWord.contains(guess)) {
                    int index = gameWord.indexOf(guess);
                    for (int t = 0; t < gameWord.length(); t++) {
                        displayWord[index] = guess.charAt(0);
                    }
                } else {
                    incorrect[y] = guess.charAt(0);
                    y++;
                }
                System.out.println(displayWord);
                System.out.println(correct);
                System.out.println(incorrect);
            }
     
     
        }
     
        public static void drawBase() {
            echoString(" ", 4);
            echoString("-", 4);
            System.out.println();
            System.out.println("    |   |");
            echoString("        |\n", 1);
            System.out.println("     ------");
     
        }
     
        public static void drawHead() {
            echoString(" ", 4);
            echoString("-", 4);
            System.out.println();
            System.out.println("    |   |");
            System.out.println("    O   |");
            echoString("        |\n", 1);
            System.out.println("     ------");
        }
     
        public static void drawTorso() {
            echoString(" ", 4);
            echoString("-", 4);
            System.out.println();
            System.out.println("    |   |");
            System.out.println("    O   |");
            System.out.println("    !   |");
            echoString("        |\n", 1);
            System.out.println("     ------");
        }
     
        public static void drawLeftA() {
            echoString(" ", 4);
            echoString("-", 4);
            System.out.println();
            System.out.println("    |   |");
            System.out.println("    O   |");
            System.out.println("   /|   |");
            echoString("        |\n", 1);
            System.out.println("     ------");
        }
     
        public static void drawRightA() {
            echoString(" ", 4);
            echoString("-", 4);
            System.out.println();
            System.out.println("    |   |");
            System.out.println("    O   |");
            System.out.println("   /|\\   |");
            System.out.println("        |\n");
            System.out.println("     ------");
        }
     
        public static void drawLeftL() {
            echoString(" ", 4);
            echoString("-", 4);
            System.out.println();
            System.out.println("    |   |");
            System.out.println("    O   |");
            System.out.println("   /|\\   |");
            System.out.println("   /   |");
            echoString("        |\n", 1);
            System.out.println("     ------");
        }
     
        public static void drawRightL() {
            echoString(" ", 4);
            echoString("-", 4);
            System.out.println();
            System.out.println("    |   |");
            System.out.println("    O   |");
            System.out.println("   /|\\   |");
            System.out.println("   / \\   |");
            echoString("        |\n", 1);
            System.out.println("     ------");
        }
     
        private static void echoString(String s, int times) {
            for (int i = 1; i <= times; i++) {
     
                System.out.print(s);
     
            }
        }
    }

    Ignore the ugly drawPieces methods I have, just typed them out in a hurry, but before changing and implementing them, i need my code working.


  2. #2
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Hangman Game

    Quote Originally Posted by connorlm3 View Post
    ... I have a lot of it coded, but ... the program doesnt do anything but it is still running....
    Well, isn't it waiting for user input? (Why the heck didn't your program tell the user to enter something?)

    Anyhow., in addition to letting the user know what is expected...

    I think it's a Good Idea to make debug statements verbose enough to let you see what the program is seeing (and what it is doing) at places where there is an "issue."

    .
    .
    .        
            // Statements for help in debugging
            //
            //System.out.print("here"); //print for debugging purpose<--- not very helpful is it?
            System.out.println("y = " + y + ", MAXGUESS = " + MAXGUESS);
            System.out.println("gameWord: " + gameWord + ", displayWord: " + Arrays.toString(displayWord));
     
     
            // Normal user-informative program output.
            //
            //
            //  Why not put something here that shows the number of characters in the word?
            //
            System.out.println("\nYou have " + MAXGUESS + " guesses.");
            System.out.print("\nWhat is your first guess? ");
     
            while (!Arrays.equals(gameWord.toCharArray(), displayWord)
                    && y <= MAXGUESS) {
    .
    .
    .

    The beginning of a run could look like:

    y = 0, MAXGUESS = 6
    gameWord: 10-point, displayWord: [_, _, _, _, _, _, _, _]
     
    The word: _ _ _ _ _ _ _ _ 
     
    You have 6 guesses.
     
    What is your first guess? p
    .
    .
    .




    Cheers!

    Z

Similar Threads

  1. hangman game
    By candoa in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 10th, 2012, 10:10 PM
  2. Hangman game
    By candoa in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 9th, 2012, 10:07 PM
  3. Hangman game loop
    By Grocerybag in forum Loops & Control Statements
    Replies: 2
    Last Post: November 6th, 2012, 08:18 PM
  4. Hangman Game
    By Snow_Fox in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 29th, 2010, 04:07 PM
  5. Hangman game HELP!
    By KingFisher in forum What's Wrong With My Code?
    Replies: 11
    Last Post: September 9th, 2010, 04:23 AM