Results 1 to 2 of 2
  1. #1
    barney is offline Member
    Join Date
    Jul 2007
    Posts
    40
    Rep Power
    0

    Default Create the game Hangman

    Hi, I have to create the game Hangman. It has to run in the command prompt. I have drawn scaffold and created an array with words.

    I can randomly generate a word from that list. My first problem comes in getting the length of that word. I can't seem to figure out how to get the length of the word.
    When I get the length of the word i can loop the length to draw __'s for the length.
    My next problem would be, how do you get user inputs as guesses from the command prompt as guesses, then determine if they are in the actual word generated, then return a wrong if wrong, display correct letters in place, and add the list of guessed letters to the settings.
    Thanks.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    how to get the length of the word
    Use the String method length
    Java Code:
    int len = word.length();
    Java Code:
    import java.util.*;
    
    public class UserInput {
        public static void main(String[] args) {
            String[] words = { "hello", "world" };
            Random rand = new Random();
            Scanner scanner = new Scanner(System.in);
            String choice;
            do {
                String word = words[rand.nextInt(words.length)];
                System.out.println("Guess word");
                String guess = scanner.nextLine();
                if(word.equals(guess)) {
                    System.out.println("correct");
                } else {
                    System.out.println("wrong");
                    // add the list of guessed letters to the settings
                    // Do you mean to the words array?
                    words = addGuess(words, guess);
                    System.out.printf("words = %s%n", Arrays.toString(words));
                }
                System.out.println("More? \"y\" or any key");
                choice = scanner.nextLine();
            } while(choice.equals("y"));
            scanner.close();
        }
    
        private static String[] addGuess(String[] array, String element) {
            int len = array.length;
            String[] temp = new String[len+1];
            System.arraycopy(array, 0, temp, 0, len);
            temp[len] = element;
            return temp;
        }
    }

Similar Threads

  1. Replies: 2
    Last Post: 08-05-2010, 05:49 AM
  2. TicTacToe Game
    By Ebtihal in forum New To Java
    Replies: 0
    Last Post: 01-09-2008, 12:01 PM
  3. Need help with random game!
    By silverq_82 in forum New To Java
    Replies: 4
    Last Post: 08-07-2007, 03:58 PM
  4. Help with my game in java
    By lenny in forum New To Java
    Replies: 1
    Last Post: 07-23-2007, 05:40 PM
  5. Help with pong game
    By Eric in forum New To Java
    Replies: 2
    Last Post: 07-03-2007, 08:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •