public class HiddenWord {
private String word;
public HiddenWord(String word) {
this.word = word;
}
public String getHint(String guess) {
String hint = "";
for (int i = 0; i < guess.length(); i++) {
if (guess.charAt(i) == word.charAt(i)) {
hint += guess.charAt(i);
}
else if (word.indexOf(guess.charAt(i)) != -1){
hint += "+";
}
else{
hint += "*";
}
// Add your other conditions if needed
}
return hint;
}
// Testing method
public static void main(String[] args) {
HiddenWord puzzle = new HiddenWord("HARPS");
System.out.println(puzzle.getHint("HELLO"));
System.out.println(puzzle.getHint("HEART"));
System.out.println(puzzle.getHint("HARMS"));
System.out.println(puzzle.getHint("HARPS"));
}
}
HiddenWord.main(null);
H****
H*++*
HAR*S
HARPS
How indexOf to find character