#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    78
    Rep Power
    2

    Hangman game help, how to check if an array's index values matches with a String?


    In my hangman game, I want to check if the "index values" of an array matches with a random word. I'm trying to figure out when the player correctly guessed the word, or when the player correctly entered all the letters.

    Also, how can I add a counter to show each steps of the hangman? I have only those two issues, then I'm done.

    I'm talking about the highlighted part.

    Thank you,

    Tony

    Code:
    package hangmangame;
    
    /*
     * STEPS TO WRIE HANGMAN PROGRAM (Hangman game pseudocode)
     * -------------------------------------------------------
     * 1.  Create an array with some words
     * 2.  Create an empty dashArray
     * 3.  Create an alphabet array to delete used letters
     * 4.  Get a random word from the word array using random number generator
     * 5.  Set the word to the random number's index from the word array
     * 6.  Create a new dash array with the number of letters in the word
     * 7.  Get a guess from user
     * 8.  Check if the guess is in the word
     * 9.  If yes, replace the dash with the correct letter
     * 10. Delete the letter from alphabet array (good or bad)
     * 11. If the correct word is guessed, show the word and says you win
     * 12. If the incorrect word is guessed, only take the first character of the word, and put it in the correct place
     * 13. Create a counter to count each time the wrong guess is entered
     * 14. Display the appropriate "hangman" image
     * 15. If the counter is more than six, the player fails (hangman hanged)
     * 
     *     Need to figure out the counter to display each hangman sequence
     */
    
    import java.util.Random;
    import java.util.Scanner;
    
    public class HangmanGame {
    
        public static void main(String[] args) {
            
            Random rand = new Random();
            Scanner scan = new Scanner(System.in);
        
            String word, guess;
            char check;
            int random = 0, counter = 0;
            boolean hanged = false;
    
            String[] alphabetArray = new String[] {" A ", " B ", " C ", " D ", " E ", " F ", " G ", " H ", " I ", " J ", " K ", " L ", " M ",
                                                   " N ", " 0 ", " P ", " Q ", " R ", " S ", " T ", " U ", " V ", " W ", " X ", " Y ", " Z "};
            
            String[] wordsArray = new String[] {"elephant"};
            
            /*
             *                                  , "star", " candy", "wings", "shopping", "steak", "couch", "toy", "cat", "mouse",
                                                "elevator", "taxi", "airplane", "bus", "car", "flower", "cheese", "apple", "orange", "coat",
                                                "running", "sweater", "blanket", "chocolate", "chicken", "computer", "rice", "dance", "doll",
                                                "duck", "alligator", "snake", "calculator", "castle", "balloon", "laptop", "turtle", "pencil",
                                                "fridge", "police", "antartica", "dinner", "fire", "cookie", "pizza", "church", "village"
             
             
             */
    
            String[] dashArray;
    
            random = rand.nextInt(wordsArray.length);
            word = wordsArray[random];
            dashArray = new String[word.length()];   
            
            System.out.println("------------------: HANGMAN GAME INSTRUCTIONS :------------------");
            System.out.println("1. You can enter either a letter or try to guess the word any time.");
            System.out.println("2. You have six chances before the hangman is hanged (you fail).");
            System.out.println("3. If you try to guess the word, but the guess is wrong, program will read the first letter only");
            System.out.println("4. If you guess the word correctly, you win");
            
            System.out.println("\t\t\t\t\t _____");
            System.out.println("\t\t\t\t\t |   |");
            System.out.println("\t\t\t\t\t     |");
            System.out.println("\t\t\t\t\t     |");
            System.out.println("\t\t\t\t\t     |");
            System.out.println("\t\t\t\t\t_____|");
            
          
            System.out.print("Letters available: ");
            
            for(int c=0; c<alphabetArray.length; c++)
            {
                System.out.print(alphabetArray[c]);
            }
            
            System.out.println();
    
            for(int a=0; a<dashArray.length; a++)
            {
                dashArray[a] = " - ";
                System.out.print(dashArray[a]);           
            }
            
            while(hanged == false)
            {        
                System.out.println();
                
                System.out.print("Enter a guess (letter or word): ");
                guess = scan.nextLine();
                check = guess.charAt(0);
    
                for(int b=0; b<dashArray.length; b++)
                {
                    if(check == word.charAt(b))                    
                    {                    
    
                        alphabetArray[(int) Character.toUpperCase(check) - 65] = " ";
                        
                        dashArray[b] = "" + guess.charAt(0);
                        
                        dashArray[word.indexOf(guess.charAt(0))] = "" + check;
                        
                        hanged = false;
                    }
                    
                    else
                    {
                        alphabetArray[(int) Character.toUpperCase(check) - 65] = " ";
                        
                        hanged = false;
                    }
    
      
    Code:
                  [B]if(word.equals(guess))  // currently working on this, make an array to store guesses?
                    {
                        dashArray<b> = "" + guess.charAt(b);  
     
                        hanged = true;
                    }  </b>
    System.out.print(dashArray[b]); } System.out.println(); if(counter == 1) { System.out.println("\t\t\t\t\t _____"); System.out.println("\t\t\t\t\t | |"); System.out.println("\t\t\t\t\t 0 |"); System.out.println("\t\t\t\t\t |"); System.out.println("\t\t\t\t\t |"); System.out.println("\t\t\t\t\t_____|"); hanged = false; } else if(counter == 2) { System.out.println("\t\t\t\t\t _____"); System.out.println("\t\t\t\t\t | |"); System.out.println("\t\t\t\t\t 0 |"); System.out.println("\t\t\t\t\t | |"); System.out.println("\t\t\t\t\t |"); System.out.println("\t\t\t\t\t_____|"); hanged = false; } else if(counter == 3) { System.out.println("\t\t\t\t\t _____"); System.out.println("\t\t\t\t\t | |"); System.out.println("\t\t\t\t\t 0 |"); System.out.println("\t\t\t\t\t/| |"); System.out.println("\t\t\t\t\t |"); System.out.println("\t\t\t\t\t_____|"); hanged = false; } else if(counter == 4) { System.out.println("\t\t\t\t\t _____"); System.out.println("\t\t\t\t\t | |"); System.out.println("\t\t\t\t\t 0 |"); System.out.println("\t\t\t\t\t/|\\ |"); System.out.println("\t\t\t\t\t |"); System.out.println("\t\t\t\t\t_____|"); hanged = false; } else if(counter == 5) { System.out.println("\t\t\t\t\t _____"); System.out.println("\t\t\t\t\t | |"); System.out.println("\t\t\t\t\t 0 |"); System.out.println("\t\t\t\t\t/|\\ |"); System.out.println("\t\t\t\t\t/ |"); System.out.println("\t\t\t\t\t_____|"); hanged = false; } else if(counter == 6) { System.out.println("\t\t\t\t\t _____"); System.out.println("\t\t\t\t\t | |"); System.out.println("\t\t\t\t\t 0 |"); System.out.println("\t\t\t\t\t/|\\ |"); System.out.println("\t\t\t\t\t/ \\ |"); System.out.println("\t\t\t\t\t_____|"); hanged = false; } if(counter > 6) { System.out.println("The word was: " + word); hanged = true; } if(hanged == true) { System.out.println("Good guess, you win!"); } System.out.println(); System.out.print("Letters available: "); for(int d=0; d<alphabetArray.length; d++) { switch (guess) { case "a": alphabetArray[0] = " "; break; case "b": alphabetArray[1] = " "; break; case "c": alphabetArray[2] = " "; break; case "d": alphabetArray[3] = " "; break; case "e": alphabetArray[4] = " "; break; case "f": alphabetArray[5] = " "; break; case "g": alphabetArray[6] = " "; break; case "h": alphabetArray[7] = " "; break; case "i": alphabetArray[8] = " "; break; case "j": alphabetArray[9] = " "; break; case "k": alphabetArray[10] = " "; break; case "l": alphabetArray[11] = " "; break; case "m": alphabetArray[12] = " "; break; case "n": alphabetArray[13] = " "; break; case "o": alphabetArray[14] = " "; break; case "p": alphabetArray[15] = " "; break; case "q": alphabetArray[16] = " "; break; case "r": alphabetArray[17] = " "; break; case "s": alphabetArray[18] = " "; break; case "t": alphabetArray[19] = " "; break; case "u": alphabetArray[20] = " "; break; case "v": alphabetArray[21] = " "; break; case "w": alphabetArray[22] = " "; break; case "x": alphabetArray[23] = " "; break; case "y": alphabetArray[24] = " "; break; case "z": alphabetArray[25] = " "; break; } System.out.print(alphabetArray[d]); } } } }
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    348
    check if the "index values" of an array matches with a random word
    I/m not sure what you are asking here. index values are int numbers. A word is usually a String. Do you mean to ask: how to get the index into a String[] array where the contents at a specific index matches a given String?
    To do that use a for loop and compare the array's element at each index against the String.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    78
    Rep Power
    2
    Originally Posted by NormR
    I/m not sure what you are asking here. index values are int numbers. A word is usually a String. Do you mean to ask: how to get the index into a String[] array where the contents at a specific index matches a given String?
    To do that use a for loop and compare the array's element at each index against the String.
    By Index values, I meant, each character. I've tried dashArray.toString() to compare it with the String Word, but it doesn't work. I'll try the for loop, I was trying to see if there was another way besides it. I've tried with the for loop to compare each character in the dashArray with each characters in word.charAt(b). It knows I've correctly guessed if I type "elephant", but if I type, e, l, p, h, a, n, t. It doesn't know the word is entered. I know the word.equals(guess) works, but I'm trying to figure out dashArray[b].equals(word.charAt(b)), and why it doesn't know the word is entered correctly.

    Code:
    for(int b=0; b<dashArray.length; b++)
                {
                    if(word.equals(guess) || dashArray[b].equals(word.charAt(b)))  // currently working on this, make an array to store guesses?
                    {
                        dashArray[b] = "" + guess.charAt(b);  
                       
                        hanged = true;
                    }  
    
                        System.out.print(dashArray[b]);   
                }
    Last edited by tonynsx; September 30th, 2013 at 11:44 AM.
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    348
    dashArray.toString() to compare it with the String Word, but it doesn't work.
    dashArray is an Array of Strings. To get one String from an array of Strings use a loop to concatenate all the elements of the array into a single String. A StringBuffer would be more efficient.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    78
    Rep Power
    2
    Originally Posted by tonynsx
    By Index values, I meant, each character. I've tried dashArray.toString() to compare it with the String Word, but it doesn't work. I'll try the for loop, I was trying to see if there was another way besides it. I've tried with the for loop to compare each character in the dashArray with each characters in word.charAt(b). It knows I've correctly guessed if I type "elephant", but if I type, e, l, p, h, a, n, t. It doesn't know the word is entered. I know the word.equals(guess) works, but I'm trying to figure out dashArray[b].equals(word.charAt(b)), and why it doesn't know the word is entered correctly.
    As far as I know, I'm comparing the array's element at each index against the String using the for loop.
  10. #6
  11. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    348
    if I type, e, l, p, h, a, n, t. It doesn't know the word is entered.
    Try debugging the code to see why it is missing that. Add some println statements to show what the code is doing so that you can see where it is going wrong.

    Here's a hint to make the testing easier. Set the Scanner object to get the input from a String vs requiring you to type it in:
    Code:
            Scanner scan = new Scanner("a\nb\np\ne\nn\nt\nh\ne\nx\nelephant\n"); //System.in);

    The if condition test in post #3
    Code:
     if(word.equals(guess)
    should be outside the loop since neither word nor guess changes in the loop.
    Last edited by NormR; September 30th, 2013 at 01:04 PM.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    78
    Rep Power
    2
    Originally Posted by NormR
    Try debugging the code to see why it is missing that. Add some println statements to show what the code is doing so that you can see where it is going wrong.

    Here's a hint to make the testing easier. Set the Scanner object to get the input from a String vs requiring you to type it in:
    Code:
            Scanner scan = new Scanner("a\nb\np\ne\nn\nt\nh\ne\nx\nelephant\n"); //System.in);

    The if condition test in post #3
    Code:
     if(word.equals(guess)
    should be outside the loop since neither word nor guess changes in the loop.
    Thank you sir, I'll try those
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    78
    Rep Power
    2
    Just a general question, Is there a way to get the value from a for loop outside of the for loop?

    ex:

    for(int a=0; a<10; a++)
    {
    System.out.println(a);
    }

    System.out.println(b);

    the first system.out prints numbers from 0 - 9
    the second system.out doesn't know the value of 'a', so it's syntax error. Does it mean that, the value of 'a' lives only inside the for loop?
  16. #9
  17. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    348
    the value of 'a' lives only inside the for loop?
    Yes.
    Define the variable that you want to access outside of the for loop.
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    78
    Rep Power
    2
    Originally Posted by NormR
    Yes.
    Define the variable that you want to access outside of the for loop.
    I'm sorry, but I don't understand by define the variable that you want to access outside of the for loop.

    Could you give me an example?
  20. #11
  21. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    348
    Code:
    int x = 0; // define a variable: x with an initial value of 0
    for(...) {
    //  use x here
    } // end for
    // here x is still defined
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    78
    Rep Power
    2
    Originally Posted by NormR
    Code:
    int x = 0; // define a variable: x with an initial value of 0
    Understood, thank you.

IMN logo majestic logo threadwatch logo seochat tools logo