首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Hangman游戏-C

Hangman游戏-C
EN

Stack Overflow用户
提问于 2019-12-08 19:03:47
回答 1查看 96关注 0票数 1

我正在做一个学校项目的绞刑者游戏,但我面临着一个问题:

代码语言:javascript
复制
do{
    system("cls");
    // Header of the game
    printf("\n HANGMAN GAME\n\n\n");

    // Present letters found
    for (i=0; word[i]!='\0'; i++)
        printf (" %c  ", word_2[i]);
        printf("\n");

    // Present positions to the letters
    for (i=0; word[i]!='\0'; i++)
        printf("___ ");
        printf("\n");

    // ****PLAYER'S ANSWERS*****

    // Read player's answers
    printf("\n\n Whats your guess + <enter>: ");
    scanf("%c", &letter);
    scanf("%c", &c);

    // Verify if the letter is in the word
    found=0;
    for(i=0; word[i]!='\0'; i++)
    if (word[i] == letter){
        word_2[i] = letter;
        corrects++;
        max_attemps--;
        found = 1;
        printf("\nWell done, %s. You have now %d attempts\n\n", name, max_attemps);
        system("pause");

    }
    if(found == 0){
        max_attemps--;
        printf("\nOh no, %s. You have now %d attempts\n\n", name, max_attemps);
        system("pause");
    }

    if (max_attemps <= 0 || corrects == lenght) {
        end = 1;
    }
} while (end == 0);

当我得到一个在单词中有两个或更多位置的正确字母时,它需要我进行两次或更多次尝试,因为system("pause"),而实际上我只能尝试一次。但是如果我不把system("pause")放进去,在我看到消息之前,板子就会被清除。有人知道我能做些什么来解决这个问题吗?我会非常感激的。

EN

回答 1

Stack Overflow用户

发布于 2019-12-08 19:07:32

只有在扫描完整个单词之后,才能检查found标志:

代码语言:javascript
复制
    // Verify if the letter is in the word
found=0;
for(i=0; word[i]!='\0'; i++)
if (word[i] == letter){
    word_2[i] = letter;
    corrects++;
    max_attemps--;
    found = 1;

// comment out
//        printf("\nWell done, %s. You have now %d attempts\n\n", name, max_attemps);
//        system("pause");

}
    if(found == 1)
{
            printf("\nWell done, %s. You have now %d attempts\n\n", name, max_attemps);
            system("pause");
}



else{
    max_attemps--;
    printf("\nOh no, %s. You have now %d attempts\n\n", name, max_attemps);
    system("pause");
}

if (max_attemps <= 0 || corrects == lenght) {
    end = 1;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59234735

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档