C语言 如何处理Word克隆中的重复黄色字母

np8igboo  于 2023-03-01  发布在  其他
关注(0)|答案(2)|浏览(137)

我试着用C做一个单词克隆,但是我不能处理重复的黄色字母。例如,如果主单词是"apple",用户猜测是"aplle"(不是一个实际的单词,只是一个示例),我的代码返回"APlLE"而不是"AP*LE"。请注意,大写字符表示绿色字符,小写字符表示黄色字符,而*表示灰色的。
这是处理用户猜测的逻辑。我真的不确定我如何才能得到一个解决方案,如果它甚至可能与这个逻辑。任何帮助是非常感谢的。我已经尝试看其他类似的问题,但代码看起来相当不同,这是我第一次用C编码。提前感谢!

do {
    scanf("%5s", userGuess);
    correctGuess = strcmp(userGuess,chosenWord);
    if (strlen(userGuess) != 5) { // checks if the user input is not a 5 letter word
        printf("Please enter a five letter word.\n");
    }
    else if (correctGuess == 0) { // 0 means that the two strings are the same
        printf("You are correct!\n");
        guessNumber++;
        printf("%i", guessNumber);
    }
    else {
        guessNumber++;
        for (int i = 0; i <= 4; i++) { // green letter implementation
            if (userGuess[i] == chosenWord[i]) {
                userGuess2[i] = toupper(userGuess[i]);
            }
            else {
                int yellowChar = 0; // handles yellow letters - doesn't know how to deal with duplicates
                for (int j = 0; j < 5; j++) {
                    if (i != j && userGuess[i] == chosenWord[j]) {
                        userGuess2[i] = tolower(userGuess[i]);
                        yellowChar = 1;
                        break;
                    }
                }
                if (!yellowChar) { // handles gray letters
                    userGuess2[i] = '*';
                }
            }
        }
        printf("%s\n", userGuess2);
    }
}
while(guessNumber <= 5 && correctGuess != 0);
xmakbtuz

xmakbtuz1#

您要寻找的答案是在黄色if语句中添加另一个&&条件:

if (i != j && userGuess[i] == chosenWord[j] && userGuess[i] != userGuess[j]) {
  logic
}

这就解决了这个问题,如果单词是apple,那么猜测aaaaa会得到A****
还有另一个问题是,如果猜测是xaaaa,那么它仍然会黄色所有其他4个A的,而不仅仅是第一个。

wyyhbhjk

wyyhbhjk2#

您可以使用一两个标志:

const int STR_LEN = 5;
const char *answer = "apple";
void adjustCase(char *s) {
    for (int i = 0; i < STR_LEN; i++) {
        s[i] = tolower(s[i]);
    }
}
int main(int argc, char *argv[])
{
    char guess[STR_LEN + 1]; // for null terminator
    char evalResult[STR_LEN + 1]; // for null terminator
    int guessNumber = 0;
    bool correct = false;
    bool charFound;

    do {
        charFound = false;
        strcpy(evalResult, "*****");
        scanf("%5s", guess);
        adjustCase(guess);
        if (strcmp(guess, answer) == 0) {
            correct = true;
            printf("You are correct!\n");
        }
        else {
            guessNumber++;
            for (int i = 0; i < STR_LEN; i++) {
                if (guess[i] == answer[i]) {
                    evalResult[i] = toupper(guess[i]);
                } else if (!charFound){
                    for (int j = 0; j < STR_LEN; j++) {
                        if (guess[i] == answer[j]) {
                            evalResult[i] = guess[i];
                            charFound = true;
                            break;
                        }
                    }
                }
            }
            printf("%s\n", evalResult);
        }
    }
    while(guessNumber <= 5 && !correct);
}

不是唯一的答案,甚至可能不是最好的,但似乎直截了当,合理有效。

相关问题