Consider a guessing game in which a player tries to guess a hidden word. The hidden word contains only capital letters and has a length known to the player. A guess contains only capital letters and has the same length as the hidden word.
After a guess is made, the player is given a hint that is based on a comparison between the hidden word and the guess. Each position in the hint contains a character that corresponds to the letter in the same position in the guess. The following rules determine the characters that appear in the hint.
Write the complete HiddenWord class, including any necessary instance variables, its constructor, and the method, getHint, described above. You may assume that the length of the guess is the same as the length of the hidden word.
public class HiddenWord {
// initializing hiddenWord String
private String hiddenWord;
public HiddenWord(String hiddenWord) {
this.hiddenWord = hiddenWord;
}
// method getHint
public String getHint(String guess) {
// Initialize String for hint
String hint = "";
for (int i = 0; i < guess.length(); i++) {
if (guess.substring(i,i+1).equals(word.substring(i, i+1))) {
hint += guess.substring(i, i+1);
} else if (hiddenWord.indexOf(guess.substring(i, i+1)) != -1) {
hint += "+";
} else {
hint += "*";
}
}
return hint.toString();
}
}
HiddenWord puzzle = new HiddenWord("HARPS");
puzzle.getHint("AAAA");
+A++
HiddenWord puzzle = new HiddenWord("HARPS");
puzzle.getHint("HELLO");
H****
HiddenWord puzzle = new HiddenWord("HARPS");
puzzle.getHint("HEART");
H*++*
HiddenWord puzzle = new HiddenWord("HARPS");
puzzle.getHint("HARMS");
HAR*S
HiddenWord puzzle = new HiddenWord("HARPS");
puzzle.getHint("HARPS");
HARPS