0

So my issue is that I have most of my code set up, except the loops I have tried always seem to iterate extra times, throwing off the correct/incorrect count. I have been stuck trying different combinations of while and for loops and this was the last straw. Any help or ideas on how I can get it in the right direction would be really appreciated. I am a newbie so my code may be sloppy or whatever but as long as it works.Subclass for getting input

package hangman;
import java.util.Scanner;
public class Hangman {

    private String word;

    public void setWord() {
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter the secret word: (Under 7 letters)");
        this.word = scan.nextLine();
        this.word = word; 
    }

    public String getWord() {
        return this.word;
    }





}

Superclass containing the logic of the game

 /*
 * Hangman 
     * 5/23/18
     * JDK Version 1.8.0
     */
    package hangman;

    import java.awt.*;
    import java.util.Scanner;
    import javax.swing.*;

    public class PlayHangman {

        public static void main(String[] args) {
            Hangman hangman = new Hangman();
            Scanner scn = new Scanner(System.in);
            int triesCount = 0;
            int correctCount = 0;
            int triesLimit = 7;
            hangman.setWord();
            String secretWord = hangman.getWord();
            int winCount = secretWord.length();
            StringBuilder b = new StringBuilder(secretWord.length());
            for (int i = 0; i < secretWord.length(); i++) {
                b.append("*");
            }
            char[] secrectStrCharArr = secretWord.toCharArray();
            int charCnt = secretWord.length();
            for (int x = 0; triesCount < triesLimit; x++) {

                    System.out.println("Secrect Word :" + b.toString());
                    System.out.println("Guess a letter :");

                    char guessChar = scn.next().toCharArray()[0];
                    for (int i = 0; i < secrectStrCharArr.length; i++) {
                        if(correctCount == winCount)
                        {System.out.println("Congrats! You have won!");}    
                        else if (guessChar == secrectStrCharArr[i]) {
                            b.setCharAt(i, guessChar);
                            correctCount++;hangmanImage(triesCount,correctCount);
                        } else if (guessChar != secrectStrCharArr[i]) {
                            triesCount++;
                            System.out.println("Incorrect: " + triesCount);
                            hangmanImage(triesCount, correctCount);
                        }

                    }
            }
        }

        public static void hangmanImage(int triesCount, int correctCount) {

          if (triesCount == 1) {
                    System.out.println("Wrong guess, try again - Incorrect: " + triesCount+" |Correct: "+correctCount);
                    System.out.println("   ____________");
                    System.out.println("   |          _|_");
                    System.out.println("   |         /   \\");
                    System.out.println("   |        |     |");
                    System.out.println("   |         \\_ _/");
                    System.out.println("   |                      ");
                    System.out.println("   |                        ");
                    System.out.println("   |                       ");
                    System.out.println("   |                           ");
                    System.out.println("   |                           ");
                    System.out.println("   |                           ");


                }
            if (triesCount == 2) {
                    System.out.println("Wrong guess, try again - Incorrect: " + triesCount+" | Correct: "+correctCount);
                    System.out.println("   ____________");
                    System.out.println("   |          _|_");
                    System.out.println("   |         /   \\");
                    System.out.println("   |        |     |");
                    System.out.println("   |         \\_ _/");
                    System.out.println("   |            |               ");
                    System.out.println("   |            |                ");
                    System.out.println("   |            |               ");
                    System.out.println("   |                           ");
                    System.out.println("   |                           ");
                    System.out.println("   |                           ");


                }
            if (triesCount == 3) {
                    System.out.println("Wrong guess, try again - Incorrect: " + triesCount+" | Correct: "+correctCount);
                    System.out.println("   ____________");
                    System.out.println("   |          _|_");
                    System.out.println("   |         /   \\");
                    System.out.println("   |        |     |");
                    System.out.println("   |         \\_ _/");
                    System.out.println("   |            |   /            ");
                    System.out.println("   |            | -/               ");
                    System.out.println("   |            |               ");
                    System.out.println("   |                           ");
                    System.out.println("   |                           ");
                    System.out.println("   |                           ");


                }
                if (triesCount == 4) {
                    System.out.println("Wrong guess, try again - Incorrect: " + triesCount+" | Correct: "+correctCount);
                    System.out.println("   ____________");
                    System.out.println("   |          _|_");
                    System.out.println("   |         /   \\");
                    System.out.println("   |        |     |");
                    System.out.println("   |         \\_ _/");
                    System.out.println("   |            |   /             ");
                    System.out.println("   |            | -/                ");
                    System.out.println("   |            |               ");
                    System.out.println("   |                           ");
                    System.out.println("   |                           ");
                    System.out.println("   |                           ");


                }
                if (triesCount == 5) {
                    System.out.println("Wrong guess, try again - Incorrect: " + triesCount+" |Correct: "+correctCount);
                    System.out.println("   ____________");
                    System.out.println("   |          _|_");
                    System.out.println("   |         /   \\");
                    System.out.println("   |        |     |");
                    System.out.println("   |         \\_ _/");
                    System.out.println("   |      \\    |   /            ");
                    System.out.println("   |        \\- | -/               ");
                    System.out.println("   |            |               ");
                    System.out.println("   |                           ");
                    System.out.println("   |                           ");
                    System.out.println("   |                           ");


                }
                if (triesCount == 6) {
                    System.out.println("Wrong guess, try again - Incorrect: " + triesCount+" |Correct: "+correctCount);
                    System.out.println("   ____________");
                    System.out.println("   |          _|_");
                    System.out.println("   |         /   \\");
                    System.out.println("   |        |     |");
                    System.out.println("   |         \\_ _/");
                    System.out.println("   |      \\    |   /            ");
                    System.out.println("   |        \\- | -/               ");
                    System.out.println("   |            |               ");
                    System.out.println("   |             \\               ");
                    System.out.println("   |               \\             ");
                    System.out.println("   |                 --          ");


                }

                if (triesCount == 7) {
                    System.out.println("Wrong guess, try again - Incorrect: " + triesCount+" |Correct: "+correctCount);
                    System.out.println("   ____________");
                    System.out.println("   |          _|_");
                    System.out.println("   |         /   \\");
                    System.out.println("   |        |     |");
                    System.out.println("   |         \\_ _/");
                    System.out.println("   |      \\    |   /            ");
                    System.out.println("   |        \\- | -/               ");
                    System.out.println("   |            |               ");
                    System.out.println("   |           /  \\               ");
                    System.out.println("   |          /     \\             ");
                    System.out.println("   |       ---        ---          ");


                }

                if(triesCount>7)
                {System.out.println("You lost");}


        }

    }
anon
  • 11
  • 2

1 Answers1

0

Your loop is such that it compares a character to every position, hence even if the character input is at some position, the way your loop is, it will go to else if (guessChar != secrectStrCharArr[i] for all other positions in the string. For instance of the word is hello, and input is h, the loop will run for ello as well, and return incorrect count as 4.

Instead, you should check for a match and break the inner loop when you find a match:

for (; triesCount < triesLimit; ) { //for loop can used like this

    System.out.println("Secret Word :" + b.toString());
    System.out.println("Guess a letter :");
    Boolean flag = false; //using a flag to check for match
    char guessChar = scn.next().toCharArray()[0];
    for (int i = 0; i < secretWord.length(); i++) {

        if (guessChar == secretWord.charAt(i)) {
            b.setCharAt(i, guessChar);
            correctCount++;
            hangmanImage(triesCount, correctCount);
            flag = true; //match found
            break; 
        }
     }
     if (!flag) { 
         //if no match is found after checking entire array,
         //increase triesCount
         triesCount++;
         hangmanImage(triesCount, correctCount);
     }
     if (!b.toString().contains("*")) { //game win check
          System.out.println("Congrats! You have won!");
          break;
     }
}

Note that I have removed the line char[] secrectStrCharArr = secretWord.toCharArray(); as it is redundant and not required. Also note that I have changed the logic for checking if the game is won.

Also, Java conventions are such that a setter function is written like this:

public void setWord(String word) {
    this.word = word;

}

And used as hangman.setWord(word)

Anuraag Baishya
  • 874
  • 2
  • 11
  • 26
  • Oh my gosh this was the most helpful thing ever. I tried asking for help on how to see if it wins and you included it even in this one. Thanks so much I didn't even know for loop could be like that – anon Jun 06 '18 at 19:54