Christopher Miller Christopher Miller - 1 month ago 5
Java Question

Loop not ending when it's supposed to?

I've been tasked to make a secret word guessing game and it is supposed to be game over and user is asked if they want to play again if the number of guesses for the character reaches 5.
I thought my incrementor is correct but perhaps not...

Here's the class:

public class SecretWord {
private String secretWord;
private String hintWord ;
private int numberOfTurns;

//Default Constructors
public SecretWord()
{
hintWord = "";
secretWord = "juice";
for (int i = 0; i < secretWord.length(); i++)
{
hintWord+="*";
}
this.numberOfTurns = 0;
}
//Accessors
public String getSecretWord()
{
return this.secretWord;
}
public String getHintWord()
{
return this.hintWord;
}
public int getNumberOfTurns()
{
return this.numberOfTurns;
}
//Mutators
public void setSecretWord ()
{
this.secretWord = "juice";
}
public void setHintWord ()
{
//Setting the hint word which sets the asterisks when you guess something right
char[] correctLetters = new char[secretWord.length()];
for (int i = 0; i<secretWord.length();i++)
{
hintWord+="*";
correctLetters[i] += '*';
}
}
public void setNumberOfTurns (int i)
{
this.numberOfTurns = 5;
}
//Methods
public void guessLetter(char guess)
{
String tempHintWord="";
for (int i = 0; i < secretWord.length(); i++)
{
if (secretWord.charAt(i) == guess)
{
tempHintWord += guess;
}
else
{
tempHintWord += hintWord.charAt(i);
}
}
hintWord = tempHintWord;
}


Here's the driver with my loops:

public class SecretWordGame {

//Constant for number of tries
public static final int NUM_TRIES = 5;

public static void main (String[] args)
{
Scanner keyboard = new Scanner(System.in);

//Starts game
boolean quit = false;
while (quit == false)

{
System.out.println("Welcome to the word guessing game! You have " +
+NUM_TRIES+" tries to guess the secret word!");

SecretWord myWord = new SecretWord();
System.out.println("The current hint is \n"+myWord.getHintWord());

while (myWord.getNumberOfTurns() <=NUM_TRIES)
{
System.out.println("Guess a lowercase letter");

//Gets the first letter of what is entered

char tempGuess = keyboard.nextLine().charAt(0);

//Updates the hint by calling guess letter method

myWord.guessLetter(tempGuess);

System.out.println(myWord.getHintWord());

System.out.println("Guess the secret word");

String myGuess = keyboard.nextLine();
//Checks correct guess
if (myGuess.equals(myWord.getSecretWord()))
{
System.out.println("You win!");
break;
}
else
{
System.out.println("Keep trying!");
}
myWord.setNumberOfTurns(myWord.getNumberOfTurns()+1);
}
//Prompts user to play again

System.out.println("Game over! Try again?");

String userInput = keyboard.nextLine();
if(userInput.equalsIgnoreCase("no"))
{
quit = true;
}
else
{
System.out.println("Let's go again!");
}
}
System.out.println("Goodbye!");
}


Maybe the while loop (myWord.getNumberOfTurns() <=NUM_TRIES) comparison is wrong? Or perhaps the getNumberOfTurns incrementor is in the wrong place? I'm unsure.

Answer

change

public void setNumberOfTurns (int i)
{
    this.numberOfTurns = 5;
}

to

public void setNumberOfTurns (int i)
{
    this.numberOfTurns = i;
}

otherwise it would be set to 5 when this code is called myWord.setNumberOfTurns(myWord.getNumberOfTurns()+1);

Comments