【问题标题】:A Hangman game is not working when entering a guess, but a random word form list work输入猜测时刽子手游戏不起作用,但随机单词形式列表起作用
【发布时间】:2021-07-07 15:43:10
【问题描述】:

我正在尝试为Hangman 游戏编写逻辑。 我希望能够输入一个单词并从列表中获取一个随机单词。

我选择随机单词的部分效果很好,没有问题。当我输入一个单词时,问题就开始了。无论我是否猜对了正确的字符,它仍然算作一次罢工。 为什么会发生这种情况的解释是什么?

int main()
{
    int option=5, randomword;
    bool loop = true;
    // here you choose if you want a random word or to enter one by yourself.
    while (loop)
    {
        printf("choose an option:\n0 to enter a word.\n1 for the computer to choose a word for you.\n");
        scanf("%d", &option);
        // start the loop over again if the value that got scan is not 0 or 1;
        if (option == 0 || option == 1)loop = false;
    }
    char EnterdWord[128];
    // if you choose to enter a word it will scan it and enter it to a string. I am using fgets so you can enter space
    if (option == 0)
    {
        printf("enter a word:\n");
        getchar();
        fgets(EnterdWord , 128, stdin);
    }else // if you want a random word, this will make a random number, so later it will choose a random word from a list.
    {
        srand(time(NULL));
        randomword = rand() % 20 ;
    }

    // random words list
    char RanWord[][20] = {"dog", "cat", "sky", "orange", "apple", "movie", "code", "number" ,"ship" ,"google" ,"sushi" , "shower",
                          "super hero", "job interview" ,"computer" ,"video games" ,"pizza" ,"cell phone", "charger", "bottle"};

    char *TempWord = malloc(1024 * sizeof(char));

    // remove the \n from the scan in fgets.
    if (*EnterdWord && EnterdWord[strlen(EnterdWord)-1] == '\n')EnterdWord[strlen(EnterdWord)-1] = 0;
    // give to the string TempWord the word based on the option u choose
    if (option == 0) strcpy(TempWord, EnterdWord);
    if (option == 1) strcpy(TempWord, RanWord[randomword]);

    //this is the string that you are going to scan your right guess.
    char *NiceWord = malloc(1024 * sizeof(char));
    if (NiceWord == NULL) return 0;
    // copy temp word to nice word
    strcpy(NiceWord, TempWord);

    // finding the length of the word for the loop below (I know I can use while != \0)
    int lengh = strlen(TempWord);
    int i=0;
    // this part is for aesthetics. I could have just changed all the characters to space.
    while(i < lengh)
    {
        if (TempWord[i] != ' ')NiceWord[i]='_';
        if (TempWord[i] == ' ')NiceWord[i]=' ';
        i++;
    }

    loop = true;

    char guess;
    // bool verb to check for strikes.
    bool CheckStrike = true;
    int y, strikes=0;
    // print so you can see how many characters the word is.
    printf("\n%s\n", NiceWord);

    while (loop == true)
    {
        y=0;
        //scan your guess.
        printf("enter your guess:\n");
        getchar();
        scanf("%c", &guess);
        // reset the strike checker in case you where right for the last guess
        CheckStrike = true;
        // check if your guess is correct or not.
        while(TempWord[y] != '\0')
        {
            if ( TempWord[y] == guess)
            {
                NiceWord[y] = guess;
                CheckStrike = false;
            }
            y++;
        }
        // if your guess was false, add one strike
        if (CheckStrike == true)strikes++;

        printf("you have %d strikes left\n", 10-strikes);
        printf("%s\n", NiceWord);

        // check if you lost
        if (strikes == 10)
        {
            printf("the word was: %s\nyou lost :( ", TempWord);
            loop = false;
        }
        // check if you won
        if (strcmp (NiceWord, TempWord) == 0)
        {
            printf("\n\n\n\nu won :) with %d strikes left!\n\n\n\n\n", 10-strikes);
            loop = false;
        }
    }

    return 0;
}

【问题讨论】:

  • 将此视为学习如何使用调试器在监控变量及其值的同时逐语句执行程序语句的好时机。
  • 顺便说一下,在你的程序中只调用srand一次。否则,您可能会将种子重置为与上次相同的值,并且rand() 返回与上次调用相同的值。并谈论调试。尝试找到程序失败的固定情况,并跳过随机数位,而是使用硬编码值。用户输入也是如此,将其硬编码为已知会导致问题的内容。
  • 您在scanf() 之前调用getchar() 以读取用户的猜测。如果用户输入了一个单词,getchar() 将读取猜测。
  • 不要混合使用scanfgetcharfgets。坚持使用一种输入法。
  • 处理fgets 保留的换行符的一个很好的干净方法是Removing trailing newline character from fgets() input

标签: c string


【解决方案1】:

用途:

printf("enter your guess:\n");
if (option == 1)
    getchar();
scanf("%c", &guess);
if (option == 0)
    getchar();

这样就解决了问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-14
    • 2017-03-26
    • 2018-11-13
    相关资源
    最近更新 更多