user14397445
user14397445

Reputation:

Can't figure out why my counter is broken?

I am a somewhat intermediate-level Java programmer but I have had trouble with one of my recent programs. Basically the application is a Hangman game that allows the user to input letters in order to guess a word. Everything works okay except for the counter for how many lives the player has, in this case it is 5. The counter for some reason subtracts by 4 instead of 1, as well as this it takes away from the number of lives even if the letter is guessed correctly.

Any help would be widely appreciated, thank you in advance. The two classes are provided below. Also,

Instantiable Class

public class Hangman {

    private char letterGuess;
    private int numberLives;
    private String outputWord;
    private final String hiddenWord;
    private final StringBuffer swapBuffer = new StringBuffer();

    public Hangman() {
        letterGuess = ' ';
        numberLives = 5;
        hiddenWord = "java";
        outputWord = "";
        for (int i = 0; i < hiddenWord.length(); i++) {
            swapBuffer.append("*");
        }
    }

    public void setLetterGuess(char letterGuess) {
        this.letterGuess = letterGuess;
    }

    public void compute() {
        for (int i = 0; i < hiddenWord.length(); i++) {
            if (letterGuess == hiddenWord.charAt(i)) {
                swapBuffer.setCharAt(i, letterGuess);
            }
            
            else {
                numberLives--;
            }
        }

        outputWord = swapBuffer.toString();
    }

    public int getNumberLives() {
        return numberLives;
    }

    public String getHiddenWord() {
        return hiddenWord;
    }

    public String getOutputWord() {
        return outputWord;
    }
}

Main Class

import javax.swing.*;
public class HangmanApp {

    public static void main(String[] args) {

        char letterGuess;
        int numberLives;
        String hiddenWord, outputWord, restartGame;

        do {
            Hangman myHangman = new Hangman();

            JOptionPane.showMessageDialog(null, "Welcome to Java Hangman!");
            JOptionPane.showMessageDialog(null, "In this game, a word will be printed to you in asterisks - each letter will be revealed upon a correct guess!");
            JOptionPane.showMessageDialog(null, "You have 5 lives for the game, the game will end if you make too many incorrect guesses!");

            for (int i = 0; i < 10; i++) {
                hiddenWord = myHangman.getHiddenWord();
                numberLives = myHangman.getNumberLives();
                JOptionPane.showMessageDialog(null, "You currently have " +numberLives+ " lives!");
                letterGuess = JOptionPane.showInputDialog(null, "Now, please enter a letter : ").charAt(0);
                myHangman.setLetterGuess(letterGuess);

                myHangman.compute();

                outputWord = myHangman.getOutputWord();
                JOptionPane.showMessageDialog(null, "The word so far is  :  " +outputWord);
            }

            numberLives = myHangman.getNumberLives();
            JOptionPane.showMessageDialog(null, "You have finished the game with :  " +numberLives+ " lives!");
            restartGame = JOptionPane.showInputDialog(null, "Would you like to play again?");

        }

        while (restartGame.equalsIgnoreCase("Yes"));

    }
}

Upvotes: 0

Views: 88

Answers (2)

If the guessed letter is wrong, in the compute function 1 life will be taken for each letter of the hidden word. You should try and use a switch(boolean) that will show you if the letter was found or not after parsing the whole word.

     public void compute() {
//        for (int i = 0; i < hiddenWord.length(); i++) {
//            if (letterGuess == hiddenWord.charAt(i)) {
//                swapBuffer.setCharAt(i, letterGuess);
//            }
//
//            else {
//                numberLives--;
//            }
//        }
        int letterNo = hiddenWord.length();
        boolean found = false;
        while (letterNo>0){
            letterNo--;
            if (letterGuess == hiddenWord.charAt(letterNo)){
                swapBuffer.setCharAt(letterNo, letterGuess);
                found = true;
            }
        }

        if (!found){
            numberLives--;
        }

        outputWord = swapBuffer.toString();
    }

Upvotes: 0

ControlAltDel
ControlAltDel

Reputation: 35106

Use a found boolean to check if the letter was found. If it wasn't, subtract a life.

var found = false;
for (int i = 0; i < hiddenWord.length(); i++) {
    if (letterGuess == hiddenWord.charAt(i)) {
        swapBuffer.setCharAt(i, letterGuess);
        found = true;
    }
}
if (!found) numberLives--;

Upvotes: 3

Related Questions