Code keeps setting accumulator to 0?

174 views Asked by At

So I am making a hangman game. I am trying, at the moment, to make an accumulator for how many wrong guesses the user gives (after 6 wrong guesses, they lose); However, my strikeCounter keeps getting set to 0 and I don't know why (thus printing out that there is always 1 strike no matter how many wrong guesses the user gives). Can someone help me see where my error is? Thank you!

//imports:
import java.util.Scanner; //import scanner class
import java.lang.StringBuilder; //Import StringBuider class


public class P3A1_Doron_3918410 { //create class
    public static void main (String[] args){ //create main method
        System.out.println("Hello and welcome to Hangman!"); //Welcome the user
        System.out.println(); //Leave space to organize code
        System.out.println("These are the instructions for the game:"); //Introduce instructions
        System.out.println(); //Leave space to organize code
        System.out.println("You will be tasked with guessing a secret word, which is" +
            " represented by blank underscores. For each round of the game, you will" + 
            " guess a letter. If that letter is in the word, it will appear among the underscores and" + 
            " you continue until you guess the entire word, thus winning the game. If a" +
            " letter is not in the secret word, then you lose that round and gain a" +
            " strike. Once you accumulate more than six strikes, the game is over and you lose.");
        System.out.println(); //Leave space to organize 

        System.out.println("Now let's begin. Here is your secret word:"); //Notify user game will now start
        System.out.println(); //Leave space to organize code

        //create secret word that user sees
        StringBuilder blanks = new StringBuilder("___"); //create initial, blank guessed word
        System.out.println(blanks);//print out hiddenWord's initial, blank state
        System.out.println(); //Leave space to organize code

        //Begin guessing portion of game
        Scanner keyboard = new Scanner(System.in); // Create Scanner object called keyboard
        int strikes = 0;

        for (int i = 0; i < 3; i++){
            while(blanks.charAt(i) == '_'){
                System.out.println(); //Leave space to organize code
                System.out.println("Please guess a letter."); //Ask user to guess a letter
                System.out.println(); //Leave space to organize code
                String letterGuess = keyboard.nextLine(); //Assign letter user guesses to variable letterGuess
                letterCheck(letterGuess, blanks, strikes);//send guess to letterCheck method
                System.out.println(); //Leave space to organize code
                System.out.println(blanks);
            }
        }
        System.out.println(); //Leave space to organize code
        System.out.println("Congratulations! You win!");

    } //End of main method

    public static String letterCheck(String existance, StringBuilder updatedWord, int strikeCounter){//create method that checks whether or not letter exists in secret word
        String secretWord = "cat";
        int index;

        if(secretWord.contains(existance)){
            char letter = existance.charAt(0); //convert string to char
            index = secretWord.indexOf(letter); // return index within this string of the first occurrence of specified char
            updatedWord.setCharAt(index, letter);
            System.out.println(); //Leave space to organize code
            System.out.println("Correct!");
            System.out.println(); //Leave space to organize code
        }   
        else{
            System.out.println(); //Leave space to organize code
            System.out.println("Incorrect.");
            System.out.println(); //Leave room to organize code
            strikeCounter += 1; //add a strike
            System.out.println("You have " + strikeCounter + " strike(s).");
            if(strikeCounter > 5){ //if they have more than 6 strikes
                updatedWord.append("///");
                System.out.println("You have six strikes. Game over.");
            }
            String holdStrikes = Integer.toString(strikeCounter);
            System.out.println(holdStrikes);
            return holdStrikes;
        }
        return updatedWord.toString();
    } //End of letterCheck method

} //End of class
2

There are 2 answers

2
MykelXIII On

Read on how values are passed to functions. It can either be passed by value or passed by reference. Your code passes its arguments by value. That is the problem.

0
Jackson Collins On

I changed your code to work.

Instead of passing the integer, create a private static int strikes inside the class and just up it's value.

private static int strikes = 0;

Then instead of using strikeCounter, change it to strikes.

If this helps don't forget to accept answer and upvote.