1

I am trying to make a Hangman game in Java, but most I found on the internet are with a while loop, but since this is part of a chatbot, I need it to be with if-statements, however it is not working. It grabs the first word correctly, sends 6 dashes, but every guess is "wrong" and a life goes off. Does anyone know what I'm doing wrong? Thanks in advance!

private static String hangmanStarted = "0";
private static String hangmanInitialized = "0";
private static String hangmanSecret = "";
private static int hangmanLives = 10;

private void cmdHangman() {
    if (hangmanStarted.equals("0")) {
        try {
            secretWord();
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        }
        hangmanStarted = "1";
        sendMessage("Hangman has started!\nType: ''.guess letter''\nto guess a letter!");
    }
}

private void cmdGuess() {
    if (hangmanStarted.equals("1")) {
        String input = lastMsg.replace("[^a-zA-Z]", "");
        input = input.replace("guess", "");
        input = input.toLowerCase();
        try {
            Hangman(input);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

public static void Hangman(String input) throws IOException {
        StringBuilder guessedLetters = new StringBuilder();
        String dashes = secretDashes();
        if (guessedLetters.toString().contains(input)) {
            sendMessage("This word is already guessed");
        } else {
            if (hangmanSecret.contains(input)) {
                guessedLetters.append(input);
                if (hangmanSecret.equals(letterConversion(hangmanSecret, secretDashes(), input))) {
                    sendMessage("You win!");
                    hangmanInitialized = "0";
                    hangmanStarted = "0";
                    guessedLetters.delete(0, guessedLetters.length());
                } else {
                    guessedLetters.append(input);
                    sendMessage("Letter was found!\nWord: " + letterConversion(hangmanSecret, secretDashes(), input));
                }
            } else {
                guessedLetters.append(input);
                hangmanLives = hangmanLives - 1;
                sendMessage("Word: " + dashes + "\nLives: " + hangmanLives);
                if (hangmanLives == 0) {
                    sendMessage("GAME OVER: You are dead.");
                    hangmanInitialized = "0";
                    hangmanStarted = "0";
                    hangmanLives = 10;
                    guessedLetters.delete(0, guessedLetters.length());
                }
            }
        }
    }

    public static void secretWord() throws FileNotFoundException {
        if (hangmanInitialized.equals("0")) {
            hangmanInitialized = "1";
            Scanner infile = new Scanner(new FileReader("hangWords.txt"));
            hangmanSecret = infile.next();
            infile.close();
        }
    }

    public static String secretDashes() throws FileNotFoundException {
        StringBuilder dashes = new StringBuilder();
        for (int count = 0; count < hangmanSecret.length(); count++) {
            dashes.append('-');
        }
        return dashes.toString();
    }

    public static String letterConversion(String secret, String dashes, String letter) {
        StringBuilder conversion = new StringBuilder();
        for (int index = 0; index < secret.length(); index++) {
            char secretChar = secret.charAt(index);
            secret = String.valueOf(secretChar);
            char letterChar = letter.charAt(index);
            if (secret.equals(letter))
                conversion.setCharAt(index, letterChar);
            dashes = conversion.toString();
        }
        return dashes;
    }

What it should do:

I start Hangman with the command. I guess letters through the guess command. The input of the guess command gets forwarded to Hangman void. Hangman checks if the letter guessed already. If not, check if the the letter is in the secret word and display the guessed letters to the user. If it's not in the secret word, remove a life. If life is 0, end game.

PS. Yes, I know, I'm using strings instead of booleans, but it's the same thing and I did it temporarily to test if that was the problem, heh.

EDIT: This is not the same question. After changing that line of code, the problem is still there.

Dysanix Official
  • 842
  • 1
  • 10
  • 18
  • So why did I get downvoted? I showed only necessary code, explained the problem and explained what it should do. I did not add thanks or hello. I followed all rules. I don't think this deserves a down-vote. – Dysanix Official Apr 26 '16 at 10:30
  • in your letterConversion() mmethod change the comparison of secret and letter – Kulbhushan Singh Apr 26 '16 at 10:32
  • @KevinEsche This did not solve it, still the same problem! :( Thanks for the input, though. – Dysanix Official Apr 26 '16 at 10:35
  • 1
    You post 91 lines of code and say "it's not working, why?". Try to extract your problem and ask a more specific question. We can help if you have a particular question, but you will be down voted if you ask us to debug your code. – Thomas Uhrig Apr 26 '16 at 10:40
  • 1
    @DysanixOfficial there are couple of codes in your program that causes the problem. I don't know how you are going to achieve but first of all the Hangman game probably needs an outer loop to get input from user – Pooya Apr 26 '16 at 10:42
  • 1
    @DysanixOfficial one of the bugs: StringBuilder guessedLetters = new StringBuilder(); and immediately after that: guessedLetters.toString().contains(input). guessedLetters is always empty and you compare it with a none empty String – Pooya Apr 26 '16 at 10:43
  • 1
    You can use the built-in debugger of your IDE (Eclipse? Netbeans?) to go through your code one line at a time and see what it does and what the value of each variable is. – Nzall Apr 26 '16 at 10:48

1 Answers1

2

This line in your code is incorrect:

 if (secret == letter)

it should be:

if(secret.equals(letter))

since you are using String.

Pooya
  • 6,083
  • 3
  • 23
  • 43