Reputation: 121
So I have been learning Java for a little over a month now, and I have a hang man game that I am making but I am having trouble with replacing characters in my string. I have it written so you have two strings, one is called "word" which contains the word to be guessed and the other is called "clone" which is a clone of the word that replaces all the characters with underscores. Then as you guess a letter it checks the string "word" to make sure it contains it, and if it does it replaces the underscore in "clone" with that letter.
while (this.guessesLeft >= 0) {
char letter;
int letterIndex;
getGuess();
if(this.word.contains(this.letterGuessed)) {
StringBuilder newString = new StringBuilder(this.clone);
letterIndex = this.word.indexOf(this.letterGuessed);
letter = this.word.charAt(letterIndex);
newString.setCharAt(letterIndex, letter);
this.clone = newString.toString();
} else {
this.guessesLeft--;
}
printGameBoard();
}
The problem that I'm having is that if you guess a letter and the string contains two of a character it only shows one. For example, here is my output if the word "burrito" is used.
Guess a letter: r
bur____
You have 5 guess left before you die!
Guess a letter: i
bur_i__
You have 5 guess left before you die!
Guess a letter: r
bur_i__
You have 5 guess left before you die!
How would I edit my game logic so that it if the letter "r" is guessed it puts both R's in the string and not just one? Thanks in advance for the help!
Upvotes: 0
Views: 306
Reputation: 9756
You need to look for all the indexes for your letter, then replace them all. At the moment you only look for the first one.
To find all indexes, look for a first occurrence of the letter, then if you find one (indexOf returns a positive value), keep looking from that last position using the indexOf(int ch, int fromIndex) method until you have found them all (indexOf returns -1).
Here is an example:
if(this.word.contains(this.letterGuessed)) {
// look for an occurrence,
// if you have one, keep looking for others until you have them all (ie: index = -1)
List<Integer> indexes = new ArrayList<>();
int index = this.word.indexOf(this.letterGuessed);
while (index >= 0) { // <- that will loop until the indexOf returns a -1
indexes.add(index);
index = this.word.indexOf(this.letterGuessed, index+1);
}
// replace at all the found indexes
StringBuilder newString = new StringBuilder(this.clone);
for(int letterIndex : indexes) {
char c = this.word.charAt(letterIndex);
newString.setCharAt(letterIndex, c);
}
this.clone = newString.toString();
} else {
this.guessesLeft--;
}
You could also do that in one go, without holding the indexes in a list:
if(this.word.contains(this.letterGuessed)) {
StringBuilder newString = new StringBuilder(this.clone);
int index = this.word.indexOf(this.letterGuessed);
while (index >= 0) {
char c = this.word.charAt(index);
newString.setCharAt(index, c);
index = this.word.indexOf(this.letterGuessed, index+1);
}
this.clone = newString.toString();
System.out.println("clone = " + clone);
} else {
this.guessesLeft--;
}
Upvotes: 1