Results 1 to 16 of 16

Thread: Hnagman program

  1. #1
    fluffy is offline Member
    Join Date
    Apr 2008
    Posts
    15
    Rep Power
    0

    Default Hnagman program

    Please could any one help to write the code for hangman using while loops, boolean method and text processing..

    If the user guesses a letter that is not in the word, the program adds that
    letter (in uppercase) to a list of wrong guesses. If the user guesses 6
    wrong letters before guessing all the letters of the word, the user loses.
    If the user guesses a letter that is in the word, the program replaces the
    corresponding underscore(s) with that letter (in uppercase). If the user
    guesses all the letters of the word before making 6 wrong guesses, the
    user wins.
    If the user guesses a letter that they have already guessed before, the
    program notifies them and asks them to enter another letter (see
    example logs of execution for exact wording).

    the program output ....

    Word: _ _ G
    Misses: T
    Guess a letter: g
    Already guessed! Guess a letter: t
    Already guessed! Guess a letter: f
    Word: _ _ G
    Misses: T,F
    Guess a letter: d
    Word: D _ G
    Misses: T,F
    Guess a letter: m
    Word: D _ G
    Misses: T,F,M
    Guess a letter: O
    The answer was: DOG
    You WIN!
    Do you want to play again? Yess!!

  2. #2
    fluffy is offline Member
    Join Date
    Apr 2008
    Posts
    15
    Rep Power
    0

    Default

    could any one help me to write a method for one game

  3. #3
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    lol write some yourself first then ask again. Then show us the code and where your stuck
    My IP address is 127.0.0.1

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  5. #5
    fluffy is offline Member
    Join Date
    Apr 2008
    Posts
    15
    Rep Power
    0

    Default

    import java.util.*;
    public class Hangman {
    public static void main(String[] args) {
    Scanner console = new Scanner(System.in);
    System.out.println("Let's play Hangman!");
    System.out.println();
    //String answer = HangmanUtils.getWord();
    String answer = "cat";
    answer = answer.toUpperCase();
    System.out.println("Random word = " + answer);
    System.out.println("Random word Length = " + answer.length());
    System.out.print("Word: ");
    for (int i = 0; i < answer.length(); i++) {
    System.out.print("_ ");
    }

    System.out.println();
    System.out.print("Guess a letter: ");
    }
    }

  6. #6
    fluffy is offline Member
    Join Date
    Apr 2008
    Posts
    15
    Rep Power
    0

    Default

    i have done till here.....how to write the code to put letter in that underscores and all that ..after this i am not able to write the code ....could you please help me....

  7. #7
    theonly is offline Member
    Join Date
    Apr 2008
    Posts
    23
    Rep Power
    0

    Default

    Do you want a user to pick the desire word or is the desire word already programmed into the code?

  8. #8
    fluffy is offline Member
    Join Date
    Apr 2008
    Posts
    15
    Rep Power
    0

    Default

    yes i want user to pick the desire word
    i just took that word 'Cat' as an example to see how it works...

  9. #9
    theonly is offline Member
    Join Date
    Apr 2008
    Posts
    23
    Rep Power
    0

    Default

    Where are you having a problem?

    This "program" requires a few if-statements and a loop.

    Really try to understand this, and tell us where are you stuck.

  10. #10
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    heres the steps I would take.

    step 1: make a file with words in it. Then put them in a Vector. Then randomly choose one of those words.

    step 2: I personally would have a GUI with a letter bank or something along those lines so I wouldn't have to worry about users inputing numbers or symbols.

    step 3: Just check if the letter that the user inputs is in the word.

    step 4: add a new game option.
    My IP address is 127.0.0.1

  11. #11
    fluffy is offline Member
    Join Date
    Apr 2008
    Posts
    15
    Rep Power
    0

    Default

    i have to guess a letter if its correct it should go into that position in the word otherwise it goes to misses...how to put that letter in that position only for eample the word CAT i guessed a letter 'a' how to write the code that letter goes to that 2nd position...

  12. #12
    fluffy is offline Member
    Join Date
    Apr 2008
    Posts
    15
    Rep Power
    0

    Default

    i have a file with words

  13. #13
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    The easiest way to do this is with a simple GUI that has a vector of JLabels or what ever and each label represents the same spot in your word. then just have it update the labels to whatever the user inputs. By the way if you want to be a serious programmer is to down a uml designer I recommend the piece of shit that is called visual paradigm. It is very good, if you don't mind it being slow as balls.
    My IP address is 127.0.0.1

  14. #14
    fluffy is offline Member
    Join Date
    Apr 2008
    Posts
    15
    Rep Power
    0

    Default

    import java.util.*;
    public class Hangman {
    public static void main(String[] args) {
    Scanner console = new Scanner(System.in);
    System.out.println("Let's play Hangman!");
    int gameCount;
    char response = 'y';
    while (response=='y') {
    System.out.println();
    gameCount = playGame(console);
    System.out.print("Do you want to play again? ");
    String playAgain = console.next();
    playAgain=playAgain.toLowerCase();
    response = playAgain.charAt(0);
    if (response=='n') {
    System.out.println("You played " + gameCount + " game(s). Goodbye!");
    break;
    }
    }
    }

    public static int playGame(Scanner console) {
    String answer = HangmanUtils.getWord();
    int gameCount = 0;
    answer = answer.toUpperCase();
    String word = "";
    for (int i = 0; i < answer.length(); i++) {
    word += "_ ";
    }
    String answerword = getAnswerWithSpaces(answer);
    String misses="";
    System.out.println("Word: " + word);
    System.out.println("Misses: " + misses);
    while (misses.length() < 6) {
    System.out.print("Guess a letter: ");
    String letter = console.next();
    boolean missesAlreadyContains = contains(misses, letter.toUpperCase());
    boolean letterAlreadyContains = contains(word, letter.toUpperCase());

    while (missesAlreadyContains == true || letterAlreadyContains == true) {
    System.out.print("Already guessed! Guess a letter: ");
    letter = console.next();
    missesAlreadyContains = contains(misses, letter.toUpperCase());
    letterAlreadyContains = contains(word, letter.toUpperCase());
    }
    boolean letterContains = contains(answer, letter.toUpperCase());

    if (letterContains == false) {
    misses +=letter.toUpperCase();
    }

    word = getWord(letter.toUpperCase(), answerword, word);

    if (answerword.equals(word.substring(0, word.length()-1))) {
    System.out.println("The answer was: " + answer);
    System.out.println("You WIN!");
    break;
    } else {
    System.out.println();
    System.out.println("Word: " + word);
    if (misses.length() > 1) {
    printMisses(misses);
    if (misses.length()==6) {
    System.out.println();
    System.out.println("The answer was: " + answer);
    System.out.println("You LOSE!");
    break;
    }
    } else {
    System.out.println("Misses = " + misses);
    }

    }
    }
    gameCount++;
    return gameCount;
    }

    public static boolean contains(String answer, String letter) {
    for(int i=0; i< answer.length(); i++) {
    if (answer.charAt(i)==letter.charAt(0)) {
    return true;
    }
    }
    return false;
    }


    public static String getWord(String letter, String answer, String word) {
    for(int i = 0; i < answer.length(); i++) {
    if(answer.charAt(i) == letter.charAt(0)) {
    word = word.substring(0, i) + letter + word.substring(i + 1, word.length());
    }
    }
    return word;
    }

    public static void printMisses(String misses) {
    System.out.print("Misses = " + misses.charAt(0));
    for(int i =1; i < misses.length(); i++) {
    System.out.print(", " + misses.charAt(i));
    }
    System.out.println();
    }

    public static String getAnswerWithSpaces(String answer) {
    String answerword = "";
    answerword += answer.charAt(0);
    for (int i = 1; i < answer.length(); i++) {
    answerword += " " + answer.charAt(i);
    }
    return answerword;
    }
    }

  15. #15
    fluffy is offline Member
    Join Date
    Apr 2008
    Posts
    15
    Rep Power
    0

    Default

    irrespective of the games i played its showing the game count only 1(you played 1 game(s). Goodbye!).how to change that game count

  16. #16
    Jesdisciple is offline Member
    Join Date
    May 2008
    Posts
    22
    Rep Power
    0

    Default

    Move this to the top of the class, out of all methods, and add an access modifier (private/protected/public) if you like:
    Java Code:
    int gameCount = 0;
    (The absence of all access modifiers means 'package' access.)

    EDIT: Also, change these:
    Java Code:
    gameCount = playGame(console);
    Java Code:
    public static int playGame(Scanner console) {
    to these:
    Java Code:
    playGame(console);
    Java Code:
    public static void playGame(Scanner console) {
    and remove this:
    Java Code:
    int gameCount;
    Last edited by Jesdisciple; 05-07-2008 at 05:53 PM.

Similar Threads

  1. Executing a program within a program
    By gibsonrocker800 in forum New To Java
    Replies: 5
    Last Post: 05-12-2008, 08:24 AM
  2. Replies: 0
    Last Post: 04-04-2008, 02:40 PM
  3. cannot run the program
    By amiey in forum New To Java
    Replies: 1
    Last Post: 11-20-2007, 04:13 AM
  4. Replies: 0
    Last Post: 10-04-2007, 09:33 PM
  5. Why does this program not end?
    By trill in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 07:22 AM

Posting Permissions

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