Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    78
    Rep Power
    1

    Help with if statement or counter (not sure which)


    In the program below, I'm trying to count the number of times the wrong letter is guessed, and then count it by one. My problem is that, even if the correct letter is guessed, it counts. How can I fix this? I've tried everything, but no clue, so I thought I would ask here.

    Thanks,

    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. If the correct word is guessed by letter by letter before counter is more than six, player wins
     * 14. Create a counter to count each time the wrong guess is entered
     * 15. Display the appropriate "hangman" image
     * 16. 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, convertDashArray;
            char check;
            int random = 0, counter = 0;
            boolean hanged = false;
     
            String[] alphabetArray = {" 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 = {"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"
             */
     
            char[] dashArray;
     
            random = rand.nextInt(wordsArray.length);
            word = wordsArray[random];
     
            dashArray = new char[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);
                    }
     
                    else
                    {
                        alphabetArray[(int) Character.toUpperCase(check) - 65] = " "; 
     
                        hanged = false;
                    }
     
                        System.out.print(dashArray[b]);   
                }  
     
                if(hanged == false)
                    {
                        counter++;
                    }
     
                System.out.println("Counter is " + counter);
     
                convertDashArray = new String(dashArray);
     
                if(word.equals(guess) || convertDashArray.equals(word))  
                    {
    //                    dashArray[b] = guess.charAt(b);  
     
                        hanged = true;
                    }
     
                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. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    Notice this.

    Code:
            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);
                    }
     
                    else
                    {
                        alphabetArray[(int) Character.toUpperCase(check) - 65] = " "; 
     
                        hanged = false;
                    }
     
                        System.out.print(dashArray[b]);   
                }  
     
                if(hanged == false)
                    {
                        counter++;
                    }
    You only enter the while loop if hanged is false. There is nothing between the beginning of the while loop and the if statement that changes the value of hanged to true.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    78
    Rep Power
    1
    I tried putting hanged = true if the letter is in the word, but it's same result.

    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. If the correct word is guessed by letter by letter before counter is more than six, player wins
     * 14. Create a counter to count each time the wrong guess is entered
     * 15. Display the appropriate "hangman" image
     * 16. 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, convertDashArray;
            char check;
            int random = 0, counter = 0;
            boolean hanged = false;
    
            String[] alphabetArray = {" 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 = {"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"
             */
    
            char[] dashArray;
    
            random = rand.nextInt(wordsArray.length);
            word = wordsArray[random];
            
            dashArray = new char[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);
                        
                        hanged = true;
                    }
                    
                    else
                    {
                        alphabetArray[(int) Character.toUpperCase(check) - 65] = " "; 
                        
                        hanged = false;
                    }
    
                        System.out.print(dashArray[b]);   
                }  
                
                if(hanged == false)
                    {
                        counter++;
                    }
                
                System.out.println("Counter is " + counter);
                
                convertDashArray = new String(dashArray);
                
                if(word.equals(guess) || convertDashArray.equals(word))  
                    {
    //                    dashArray[b] = guess.charAt(b);  
                       
                        hanged = true;
                    }
    
                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]);
                }
            }  
        }
    }
    My word is "elephant", so it's counting the 'e' and also the 'w'. I just want it to count the 'w' since it's not in the word "elephant"

    Code:
    run:
    ------------------: HANGMAN GAME INSTRUCTIONS :------------------
    1. You can enter either a letter or try to guess the word any time.
    2. You have six chances before the hangman is hanged (you fail).
    3. If you try to guess the word, but the guess is wrong, program will read the first letter only.
    4. If you guess the word correctly, you win.
    					 _____
    					 |   |
    					     |
    					     |
    					     |
    					_____|
    Letters available:  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 
    --------
    Enter a guess (letter or word): e
    e-e-----Counter is 1
    
    					 _____
    					 |   |
    					 0   |
    					     |
    					     |
    					_____|
    
    Letters available:  A  B  C  D   F  G  H  I  J  K  L  M  N  0  P  Q  R  S  T  U  V  W  X  Y  Z 
    Enter a guess (letter or word): w
    e-e-----Counter is 2
    
    					 _____
    					 |   |
    					 0   |
    					 |   |
    					     |
    					_____|
    
    Letters available:  A  B  C  D   F  G  H  I  J  K  L  M  N  0  P  Q  R  S  T  U  V   X  Y  Z 
    Enter a guess (letter or word):
  6. #4
  7. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    Note that the if statement is after the for loop. So the value of hanged will be set for every character in the word.

    Since the character w was not the last character in the word, hanged is false.

    I would suggest you use a different boolean, found. found should be true if any of the characters in the word are equal to the guess, and false if none of the characters in the word are.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    78
    Rep Power
    1
    Originally Posted by bullet
    Note that the if statement is after the for loop. So the value of hanged will be set for every character in the word.

    Since the character w was not the last character in the word, hanged is false.

    I would suggest you use a different boolean, found. found should be true if any of the characters in the word are equal to the guess, and false if none of the characters in the word are.
    Let me try that, I'll let you know how it goes.

    Thank you
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    78
    Rep Power
    1
    I created a boolean found like you said, and set found = true if the letters are in the word, and found = false if not, but the results are still the same as before. It's counting both true and false.

    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. If the correct word is guessed by letter by letter before counter is more than six, player wins
     * 14. Create a counter to count each time the wrong guess is entered
     * 15. Display the appropriate "hangman" image
     * 16. 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, convertDashArray;
            char check;
            int random = 0, counter = 0;
            boolean hanged = false, found = false;
    
            String[] alphabetArray = {" 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 = {"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"
             */
    
            char[] dashArray;
    
            random = rand.nextInt(wordsArray.length);
            word = wordsArray[random];
            
            dashArray = new char[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);
                        
                        found = true;
                    }
                    
                    else
                    {
                        alphabetArray[(int) Character.toUpperCase(check) - 65] = " "; 
                        
                        found = false;
                    }
    
                        System.out.print(dashArray[b]);   
                }  
                
                if(found == false)
                    {
                        counter++;
                    }
                
                System.out.println("found is " + counter);
                
                convertDashArray = new String(dashArray);
                
                if(word.equals(guess) || convertDashArray.equals(word))  
                    {
    //                    dashArray[b] = guess.charAt(b);  
                       
                        hanged = true;
                    }
    
                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]);
                }
            }  
        }
    }
  12. #7
  13. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    You still have the same issue. You can checking each letter and setting the boolean after every check. You only want to set the bool false if the character is not found in the word.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    78
    Rep Power
    1
    Originally Posted by bullet
    You still have the same issue. You can checking each letter and setting the boolean after every check. You only want to set the bool false if the character is not found in the word.
    That's what I'm trying to do, but for some reason, it's not working. I'm pretty much all out of ideas.
  16. #9
  17. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    Originally Posted by tonynsx
    That's what I'm trying to do, but for some reason, it's not working. I'm pretty much all out of ideas.
    No, what you're doing is checking the first character. If it is the same as the guessed character, you set found to true, if not, you set it to false.

    Then you do the same thing for every other character.

    So if, for example, a character matched at a certain position, found would be set to true. But if the next character didn't match, found would be set to false.
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    78
    Rep Power
    1
    Originally Posted by bullet
    No, what you're doing is checking the first character. If it is the same as the guessed character, you set found to true, if not, you set it to false.

    Then you do the same thing for every other character.

    So if, for example, a character matched at a certain position, found would be set to true. But if the next character didn't match, found would be set to false.
    OK, I see, let me try that, thanks for your help!
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    78
    Rep Power
    1
    As far as I know, that's what I'm doing here: Could you please give me an example?

    Code:
    if(check == word.charAt(b))                    
                    {                    
                        alphabetArray[(int) Character.toUpperCase(check) - 65] = " ";
                        
                        dashArray[b] = guess.charAt(0);
                        
                        found = true;
                    }
                    
                    else
                    {
                        alphabetArray[(int) Character.toUpperCase(check) - 65] = " "; 
                        
                        found = false;
                    }
  22. #12
  23. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    Consider an example.

    Suppose the word is trigonometry.

    Suppose the player chooses t.

    In your code, you step through the for loop. At position 0, found becomes true. At position1, found becomes false. At position 9, found becomes true. But at positions 10 and 11, found is false.

    So even if the character is found, the boolean is false.

    One thing you might do is modify the for loop so that the condition includes a test if found is still false.
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    78
    Rep Power
    1
    Originally Posted by bullet
    Consider an example.

    Suppose the word is trigonometry.

    Suppose the player chooses t.

    In your code, you step through the for loop. At position 0, found becomes true. At position1, found becomes false. At position 9, found becomes true. But at positions 10 and 11, found is false.

    So even if the character is found, the boolean is false.

    One thing you might do is modify the for loop so that the condition includes a test if found is still false.
    I did a system.out to see the boolean values,

    I entered 'e'

    and it showed

    e = true
    l = false
    e = true
    p = false
    h = false
    a = false
    n = false
    t = false

    It's doing exactly what you said it was doing, but I still have no clue how to fix this.
  26. #14
  27. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    Originally Posted by tonynsx
    I did a system.out to see the boolean values,

    I entered 'e'

    and it showed

    e = true
    l = false
    e = true
    p = false
    h = false
    a = false
    n = false
    t = false

    It's doing exactly what you said it was doing, but I still have no clue how to fix this.
    Note the condition on your for loop. Instead of just looping through every character in the array, stop if you find the character.
  28. #15
  29. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    78
    Rep Power
    1
    Originally Posted by bullet
    Note the condition on your for loop. Instead of just looping through every character in the array, stop if you find the character.
    how do I stop?

    use break; command, I'm sorry for asking too many questions!

    Is this the condition you're talking about?

    Code:
    if(check == word.charAt(b))                    
                    {                    
                        alphabetArray[(int) Character.toUpperCase(check) - 65] = " ";
                        
                        dashArray[b] = guess.charAt(0);
                        
                        found = true;
                        
                        System.out.println(found);
                    }
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo