Results 1 to 6 of 6
  1. #1
    durdanto is offline Member
    Join Date
    Feb 2011
    Posts
    20
    Rep Power
    0

    Default trying to write a program for this

    Hello,
    I am trying to do this assignment but can not figure it:

    Programming Question :Hangman Game
    Word Hangman Game is simplified:
    1) The word to guess cannot have repeated letters
    2) The word to guess is not case sensitive
    3) The guessing player has 10 tries to find the letters.
    4) The player cannot guess the entire word in one shot. S/he must give one letter at a time.
    Recommended skeleton of the structure of your program:
    1) A player enters the word to guess. Remember a word can only have letters and no repeated letters
    2) 20 or so blank lines are printed to clear the word to guess from the screen, before guessing player
    starts.
    3) Guessing player (the other player) is shown with dashes (or stars) the number of letters in the
    word to guess. Your program also shows the number of guesses left as well as a list of the letters
    still not picked (See sample run at the end of this explanation)
    4) Guessing player is prompted for a letter.
    5) As long as the input is more than one character long, is not a letter or is a letter that was already
    tried, your program keeps prompting the user for input. Incorrect entries should not be counted as
    guesses.
    6) Once a valid letter is entered (based on the restrictions described in step 4), your program updates
    the list of letters still left to try by removing this letter from the list(again refer to the sample
    outputs), reduces the number of guesses left by one, and checks whether the letter is part of the
    word to guess. If it is, then replace the dash (or star) with the letter in the appropriate position.
    Steps 3 to 6 are repeated until the guessing player finds all of the letters in the word or reaches 10 guesses.
    7) Your program ends with a summary of the game (Again see sample outputs).

    Output should be the following:
    Output Sample #1
    OK Guessing Player ... turn around, while your friend enters the word to guess!
    Other Player - Enter your word (letters only, no repeated letters and not case sensitive):
    Snow
    ( 20 blank lines here to clear the screen )
    Word to date: **** (10 guess(es) left)
    Letters not tried yet: ABCDEFGHIJKLMNOPQRSTUVWXYZ
    Which letter should I check for? 1
    --> Not a valid request - either not a letter or already guesses.
    Which letter should I check for? s
    Word to date: S*** (9 guess(es) left)
    Letters not tried yet: ABCDEFGHIJKLMNOPQR-TUVWXYZ
    Which letter should I check for? a
    Word to date: S*** (8 guess(es) left)
    Letters not tried yet: -BCDEFGHIJKLMNOPQR-TUVWXYZ
    Which letter should I check for? S
    --> Not a valid request - either not a letter or already guesses.
    Which letter should I check for? b
    Word to date: S*** (7 guess(es) left)
    Letters not tried yet: --CDEFGHIJKLMNOPQR-TUVWXYZ
    Which letter should I check for? c
    Word to date: S*** (6 guess(es) left)
    Letters not tried yet: ---DEFGHIJKLMNOPQR-TUVWXYZ
    Which letter should I check for? h
    Word to date: S*** (5 guess(es) left)
    Letters not tried yet: ---DEFG-IJKLMNOPQR-TUVWXYZ
    Which letter should I check for? j
    Word to date: S*** (4 guess(es) left)
    Letters not tried yet: ---DEFG-I-KLMNOPQR-TUVWXYZ
    Which letter should I check for?L
    Word to date: S*** (3 guess(es) left)
    Letters not tried yet: ---DEFG-I-K-MNOPQR-TUVWXYZ
    Which letter should I check for? q
    Word to date: S*** (2 guess(es) left)
    Letters not tried yet: ---DEFG-I-K-MNOP-R-TUVWXYZ
    Which letter should I check for? e
    Word to date: S*** (1 guess(es) left)
    Letters not tried yet: ---D-FG-I-K-MNOP-R-TUVWXYZ
    Which letter should I check for? r
    ----------------------------------------------------
    Sorry you didn't find the mystery word!
    It was "SNOW"
    Goodbye ....

    Output Sample #2

    OK Guessing Player ... turn around, while your friend enters the word to guess!
    Other Player - Enter your word (letters only, no repeated letters and not case sensitive):
    abcde
    ( 20 blank lines here to clear the screen )
    Word to date: ***** (10 guess(es) left)
    Letters not tried yet: ABCDEFGHIJKLMNOPQRSTUVWXYZ
    Which letter should I check for? a
    Word to date: A**** (9 guess(es) left)
    Letters not tried yet: -BCDEFGHIJKLMNOPQRSTUVWXYZ
    Which letter should I check for? ab
    You entered more than one letter - one letter at a time please!
    Which letter should I check for? b
    Word to date: AB*** (8 guess(es) left)
    Letters not tried yet: --CDEFGHIJKLMNOPQRSTUVWXYZ
    Which letter should I check for? c
    Word to date: ABC** (7 guess(es) left)
    Letters not tried yet: ---DEFGHIJKLMNOPQRSTUVWXYZ
    Which letter should I check for? d
    Word to date: ABCD* (6 guess(es) left)
    Letters not tried yet: ----EFGHIJKLMNOPQRSTUVWXYZ
    Which letter should I check for? e
    ----------------------------------------------------
    Congratulations!!!
    You guessed the mystery word "ABCDE" in 5 guesses!
    Goodbye ....
    Last edited by durdanto; 02-12-2011 at 12:28 AM.

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default

    What can't you figure out? What have you done so far? Simply posting an assignment such as this won't get you much advice other than the following: break the problem down into its individual parts, lay out some code, and if you have specific questions or problems post those along with the code, compile errors or exceptions, and a defined question.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Moderation actions: Moved thread from Eclipse and into the new to java forum. Duplicate post has been locked and will be deleted.

  4. #4
    durdanto is offline Member
    Join Date
    Feb 2011
    Posts
    20
    Rep Power
    0

    Default

    The thing that i can't figure out is how to take that the letter that has been tried out and display it to user as following (Note:As a condition of the assignment, I Cannot use arrays, must use strings for word that is to be guessed as well as the list of letters still
    left to try):

    Which letter should I check for? s
    Word to date: S*** (9 guess(es) left)
    Letters not tried yet: ABCDEFGHIJKLMNOPQR-TUVWXYZ

    So far what i have done is following:


    import java.util.Scanner;
    public class HANGMAN
    {
    public static void main(String arg[])
    {
    String Right = "snow" ;
    for (int player = 0; player <2; player ++)
    {
    System.out.println(" ------------------------------------- \n" +
    " Welcome to HANGMAN\n " +
    "(Nancy's version)\n " +
    "-------------------------------------");

    System.out.println("OK Guessing Player ... " +
    "turn around, while your friend enters the word to guess!");
    for(int i=0; i<10; i ++)
    {
    String acceptable = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    System.out.println(acceptable);
    Scanner input = new Scanner(System.in);


    String alphabet = input.next();


    if(acceptable.contains(alphabet)||acceptable.toLow erCase().contains(alphabet))
    {
    System.out.println(alphabet + " is vaild request");
    if(Right.contains(alphabet))
    {
    System.out.println("Congratulations!!!You guessed the mystery word");
    // char[] remove = Right.toCharArray();
    // remove.
    }

    }else {
    System.out.println(alphabet + "--> Not a valid request " +
    "- either not a letter or already guesses.");
    }
    if (i==9)
    {
    System.out.println(" ---------------------------------------------------- " +
    "\nSorry you didn't find the mystery word! \n" +
    "It was \"SNOW\" "+ "\" "+ Right + "\"" +
    "\nGoodbye .... ");
    }

    }
    Right = "ABCDE" ;
    }


    }


    }
    Last edited by durdanto; 02-15-2011 at 02:47 AM.

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    Forcing you to use Strings is not very nice as Strings are immutable and it is a bit of a pain having to create a new String each time a correct guess is made.

    What you can do is have 2 Strings. One holds the word to be guessed and the second holds the displayable word (originally all *'s). Then when user make a correct guess, scan the secret word for that letter and note the index. Then in the displayable word change the * to the guessed word before displaying it. Same deal with the list of the available letters to guess. Simply replace the guessed letter with a hypen to indicate it is no longer available.

    Now to comment on your code:
    Java Code:
    if(Right.contains(alphabet)) {
        System.out.println("Congratulations!!!You guessed the mystery word");
    As soon as the user makes a single correct guess you tell them that they guessed correctly. Hmmmm, I would think that you would wait until they have guessed all the letters before telling them that they have won.
    [/B]

  6. #6
    Debugger is offline Member
    Join Date
    Jan 2011
    Posts
    27
    Rep Power
    0

    Default

    Would it make more sense to use StringBuilder? you can add and remove a StringBuilder can't you?

Similar Threads

  1. how can write program for gui
    By pothireddy in forum Advanced Java
    Replies: 9
    Last Post: 11-04-2010, 06:17 AM
  2. Trying to write a program that uses the Collection API
    By javanewbie410 in forum New To Java
    Replies: 0
    Last Post: 10-12-2010, 07:24 PM
  3. Trying to write my own program : /
    By jdicerch in forum New To Java
    Replies: 50
    Last Post: 09-26-2010, 01:44 AM
  4. which classes to write..how to program this?
    By sony1 in forum New To Java
    Replies: 7
    Last Post: 09-04-2010, 06:12 PM
  5. need help to write Program in JAVA
    By maneibr in forum New To Java
    Replies: 1
    Last Post: 03-13-2008, 01:28 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
  •