My code currently determines if a guess is wrong or correct. If wrong, guess count goes up. When the guess count hits 6 it should exit out of the loop and say "You Lost". The issue is, with <5, when the counter is at 5 and the next guess is correct, it still says "You Lost." I tried setting the loop to <6, however it then does not say "You Lost" until the 7th guess. I am not sure what I am doing wrong, any help is appreciated.
Where guess is incremented:
public String updatedHiddenWord(){
StringBuilder updatedDashes = new StringBuilder(dashes);
boolean wrongGuess = true;
for (int i=0; i<word.length(); i++) {
if (word.charAt(i) == hv.keyChar) {
updatedDashes.setCharAt(i*2+1, hv.keyChar);
wrongGuess = false;
correct++; //ignore this counter
System.out.println(correct);
}
}
dashes = updatedDashes.toString();
if (wrongGuess){
guesses++;
}
return dashes;
}
Where the if statements and while loop are:
public String guessNotification(){
while(guesses < 5 && correct < word.length()){
if(word.indexOf(hv.keyChar)!=-1 && (hv.keyChar >= 'a' && hv.keyChar <= 'z')) {
letterGuessed = hv.keyChar + " is in the word";
hv.guessResult(this, size);
if(correct == word.length()){
letterGuessed = "You win!!";
hv.winResult(this, size);
}
}
else if(word.indexOf(hv.keyChar)==-1 && (hv.keyChar >= 'a' && hv.keyChar <= 'z')) {
letterGuessed = hv.keyChar + " is not in the word";
hv.guessResult(this, size);
hv.partAdder();
}
else{
letterGuessed = "Not a valid letter";
}
return letterGuessed;
}
if(correct == word.length()){
letterGuessed = "You win!!";
hv.winResult(this, size);
guesses = 5;
}
else{
letterGuessed = "You Lost!!";
hv.loseResult(this, size);
hv.partAdder();
}
return letterGuessed;
}
Aucun commentaire:
Enregistrer un commentaire