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

Thread: Hangman java question double letters

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Hangman java question double letters

    hey guys new on here but im in second year studying game development and we have a simple exercise to do involving a hangman game and for some reason i can't seem to get my head around the idea of double letter like "pool" has two "o" but i can only ever print out the first ? there's what i have done so far any insights would be much appreciated!!

    import java.util.Scanner;
    import java.util.Random;
    public class Excersize09_25 {

    public static void main(String[] args) {
    Random rand = new Random();
    Scanner input = new Scanner(System.in);
    String[] words= {"tiger","elephant","monster","school","cage","poo l","killer"};



    int n = rand.nextInt(6);

    String secretWord = words[n];
    String hiddenWord = secretWord.replaceAll(".","*" );
    System.out.print("Okay a word has been choosen " + "\n");
    boolean gameOver =false;
    while(gameOver!=true){
    System.out.print("(guess) Enter a letter in the word " );
    System.out.println(hiddenWord + " >");
    char guess=input.nextLine().charAt(0);

    for(int i=0 ; i<secretWord.length();i++){
    int position = secretWord.indexOf(guess);
    String newDisplaySecret = "";

    for (int k = 0; k < secretWord.length(); k++)
    if (k == position)
    newDisplaySecret += secretWord.charAt(k);
    else
    newDisplaySecret += hiddenWord.charAt(k);

    hiddenWord = new String(newDisplaySecret);


    }


    if(hiddenWord.compareTo(secretWord)==0){
    System.out.print("well done you guessed the word " + secretWord);
    gameOver=true;
    }


    }
    }
    }


  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 java question double letters

    Quote Originally Posted by sparky971 View Post
    ...hangman game...
    Instead of using the indexOf function (that would require repeats each time you find a guessed character in the secret word), why not just step through the secret word, testing each character to see if it is in a hidden word that you use to maintain a record of correct guesses? Each guess is processed by a single loop that is traversed the number of times equal to the length of the secret word.

    The concept is simple (seems simple to me). Everything else is a "simple" matter of bookkeeping: Keeping track of the characters that have not been guessed (to show the user) and keeping track of the characters that have been guessed (to compare with the secret word to determine when the game is over).

    I think it's pretty important (no, scratch that---speaking for myself, I think it is vital) to have an idea of the exact program sequence before starting to write code. Pseudo-code is pretty much de rigueur these days for showing general code structure and sequencing.

    Everyone has his/own style of writing pseudo-code. I'll show you mine. It doesn't have to be tied to any particular computer language, but since I know it's going to be a Java project, I'll sort of lean in that direction.


    Consider, perhaps, something like the following program flow description and pseudo-code. Here is how I think the game is played:
    //
    // secretWord is selected from the array of Strings.  It
    // is fixed for the duration of the game.
    //
    // There are two strings that are modified as the game progresses.
    // These could be StringBuilder objects, but I'll show a way with
    // Strings.  (StringBuilder objects are cleaner for this kind of
    // thing, I'm thinking, but maybe you haven't had them yet.)
    //
    //   hiddenWord starts out equal to the secretWord.  As a user
    //     guesses one of the characters in hiddenWord, the character
    //     at that position is replaced by an asterisk.
    //
    //   displayWord is what the user sees when prompted for a guess.
    //     displayWord starts with all asterisks. Each time the
    //     user guesses a character in hiddenWord, the asterisk in
    //     the corresponding position of displayWord is replaced
    //     by the guess (the correct character). 
    //  
    //
    //   
    // If hiddenWord and displayWord are Strings rather than StringBuilder
    // objects, then you will need a copy of each one that is built up a
    // a character at a time for each user guess, as you did in your non-working
    // example.  Then at the end of processing each guess, hiddenWord and
    // displayWord are set equal to their respective copies.
    //
    // Suppose we are going to do it with Strings:
    //
     
    BEGIN GAME
     
        Set secretWord to be one of the words in the array of String.
        Set hiddenWord equal to secretWord
        Set displayWord equal to all asterisks (Same length as secretWord)
     
        WHILE secretWord is not equal hiddenWord Loop
        BEGIN LOOP // The big loop for the entire game
            Prompt user and get guessed character
            Set newHiddenWord to ""
            Set newDisplayWord to ""
            loop i over the length of secretWord:
            BEGIN LOOP
                Set testCharacter to the character at hiddenWord(i)
                IF testCharacter is equal to guess
                THEN
                    // Good guess:
                    //    Will replace the original character in hiddenWord
                    //    with an asterisk
                    //    and will put the correct character in displayWord
                    newHiddenWord += '*'
                    newDisplayWord += guess
                ELSE
                    // Bad guess: hiddenWord and displayWord
                    //            will not change 
                    newHiddenWord += testCharacter
                    newDisplayWord += character at displayWord(i) 
                END IF;
            END LOOP // The loop of i over the length of secretWord
     
            // Replace hiddenWord and displayWord with the ones
            // we just created
            Set hiddenWord equal to newHiddenWord
            Set displayWord equal to newDisplayWord
     
        END LOOP // The big WHILE Loop
        Print final message or whatever...
    END GAME

    [/begin edit]
    Thanks to sparky971's keen analysis, I see an error in the loop condition. See if the following makes more sense:
    .
    .
    .
        WHILE secretWord is not equal displayWord Loop
        BEGIN LOOP // The big loop for the entire game
    .
    .
    .
    Sorry.
    [/end edit]


    Cheers!

    Z
    Last edited by Zaphod_b; October 8th, 2012 at 10:02 AM. Reason: Logic Correction due to resonse from sparky971

  3. #3
    Junior Member
    Join Date
    Sep 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Hangman java question double letters

    hmm i think i get the logic but if you set hiddenWord equal to SecretWord then
    while SecretWord!=hiddenWord wouldnt work no ?

  4. #4
    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 java question double letters

    Quote Originally Posted by sparky971 View Post
    hmm i think i get the logic but if you set hiddenWord equal to SecretWord then
    while SecretWord!=hiddenWord wouldnt work no ?
    You are, of course, correct.

    If you "get" the logic, maybe you can "guess" that what I really had in mind was to make the loop condition (displayWord not equal to secretWord) since displayWord is built up a character at a time from correct guesses. Sorry.

    Does that make more sense? Does the overall scheme make sense? Is it do-able? Can you think of a way to do it "better" or some way more suited to your logical thinking and program style?

    My post was only a suggestion, and I think it's practical.

    However...

    The bottom line behind my post is this:

    Once people see a way of doing things, sometimes it breaks the creative logjam, and they can come up with their own, improved, scheme. If nothing "better" occurs, then, at least it might serve as a starting point for their implementation.


    Cheers!

    Z
    Last edited by Zaphod_b; October 8th, 2012 at 10:03 AM.

  5. #5
    Junior Member
    Join Date
    Sep 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Hangman java question double letters

    Yup i guessed as much man but no seriously thanks for that it does help a lot now and i should be able to write it up a lot easier now

Similar Threads

  1. [SOLVED] Double key Caesar Cipher (USING A STACK AND A QUEUE TO SHIFT LETTERS OF A STRING)
    By Medo Almasry in forum What's Wrong With My Code?
    Replies: 16
    Last Post: November 19th, 2011, 03:18 PM
  2. Hangman game for JAVA Beginning class
    By shahravi88 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 12th, 2011, 03:15 PM
  3. Replies: 4
    Last Post: March 2nd, 2011, 10:30 AM
  4. Hangman
    By Tycho91 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 19th, 2010, 06:04 AM
  5. Java hangman game help...
    By AnotherNoob in forum AWT / Java Swing
    Replies: 16
    Last Post: December 4th, 2009, 10:17 PM

Tags for this Thread