In my algorithm to compare the secret word with the guessed word, I need to manage the duplicated letters. For example,
if the secret word is newly
and the guessed word is newer
, the output of this should be "GGGWW" (G = green(Correct Place), W = White(The Letter isn't in the secret word)),
but instead my algorithm down below outputs "GGGYW" (Y = Yellow(Letter is in the word but not the right place)).
for (int i = 0; i < 5; i++) {
if (secret.charAt(i) == prop.charAt(i)) {
status[i] = LetterStatus.IN;
} else if (secret.contains(Character.toString(prop.charAt(i)))) {
status[i] = LetterStatus.OK;
} else {
status[i] = LetterStatus.NOTIN;
}
}
return status;
secret=secretword,数组状态是(IN, OK, NOTIN)
的枚举,因此字母和props 状态是猜测的单词.
关于如何修改这个代码来解决我的问题有什么 idea 吗