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

Thread: Help with if statement or counter (not sure which)

  1. #1
    Member
    Join Date
    Jul 2013
    Location
    Baltimore, MD
    Posts
    59
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default 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

    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
    Member GoodbyeWorld's Avatar
    Join Date
    Jul 2012
    Location
    Hidden command post deep within the bowels of a hidden bunker somewhere under a nondescrip building
    Posts
    161
    My Mood
    Stressed
    Thanks
    14
    Thanked 25 Times in 25 Posts

    Default Re: Help with if statement or counter (not sure which)

    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;
    }


    As there is nothing to set hanged to be true before you have that if check, it'll go to

    if (hanged == false) first before it reaches if (word.equals(guess) || convertDashArray.equals(word)), even if the latter returns true.

    Try checking to see if it meets the criteria first so hanged can be true before you check to see if hanged is false.

Similar Threads

  1. [SOLVED] A Loop statement and a switch statement issue
    By sternfox in forum Loops & Control Statements
    Replies: 13
    Last Post: March 7th, 2013, 04:19 PM
  2. Replacing an If statement with a Switch statement
    By logi in forum Loops & Control Statements
    Replies: 9
    Last Post: February 4th, 2013, 12:21 AM
  3. [SOLVED] Counter
    By mssim in forum Java Theory & Questions
    Replies: 2
    Last Post: November 7th, 2012, 05:32 AM
  4. Help With Counter
    By Catgroove in forum Java Theory & Questions
    Replies: 7
    Last Post: February 10th, 2011, 08:50 AM
  5. Odd Even Zero Counter?
    By velop in forum Java Theory & Questions
    Replies: 1
    Last Post: February 19th, 2010, 02:13 PM