1

我对 C 语言仍然很陌生,我正在尝试制作一个刽子手游戏,但是当我获胜时,我总是无法结束游戏。

这是代码:

const int true = 1;
const int false = 0;

char words[][20] = {
    "hangman",
    "computer",
    "programming",
    "microsoft",
    "visual",
    "studio",
    "express",
    "learning"
};
    
int isletterinword(char word[], char letter)
{
    int i;    
    for (i = 0; i < strlen(word); i++) {
        if (word[i] == letter) {
            return true;
        }
    }
    return false;
}
    
int iswordcomplete(char secretword[], char rights[])
{
    int i;    
    for (i = 0; i < strlen(secretword); i++) {            
        if (rights[i] == secretword[i] ) {                
            return true;                
        }
    }    
    return false;
}
    
void printhangman(int numofwrongs)
{
    // Line 1
    printf("\t  ______\n");

    // Line 2
    printf("\t  |     |\n");

    // Line 3
    printf("\t  |     +\n");

    // Line 4 - left arm, head and right arm
    printf("\t  |");
    if (numofwrongs > 0) printf("    \\");
    if (numofwrongs > 1) printf("O");
    if (numofwrongs > 2) printf("/");
    printf("\n");

    // Line 5 - body
    printf("\t  |");
    if (numofwrongs > 3) printf("     |");
    printf("\n");

    // Line 6 - left leg and right leg
    printf("\t  |");
    if (numofwrongs > 4) printf("    /");
    if (numofwrongs > 5) printf(" \\");
    printf("\n");

    // Line 7
    printf("\t  |\n");

    // Line 8
    printf("\t__|__\n");
}

void printletters(char letters[])
{
    int i;    
    for (i = 0; i < strlen(letters); i++) {
        printf("%c ", letters[i]);
    }
}
    
void printscreen(char rights[], char wrongs[], char secretword[])
{
    int i;
    
    for (i = 0; i < 25; i++)
        printf("\n");
    
    printhangman(strlen(wrongs));
    printf("\n");

    printf("Correct guesses: ");
    printletters(rights);
    printf("\n");
    printf("Wrong guesses: ");
    printletters(wrongs);
    printf("\n\n\n");
      
    printf("\t");
    for (i = 0; i < strlen(secretword); i++) {
        if (isletterinword(rights, secretword[i])) {
            printf("%c ", secretword[i]);
        }
        else {
            printf("_ ");
        }
    }
    printf("\n\n");
}

int main()
{
    int i;        
    int secretwordindex;    
    char rights[20];    
    char wrongs[7];    
    char guess;        

    secretwordindex = 0;
   
    srand(time(0));
    secretwordindex = rand() % 8;

    for (i = 0; i < 20; i++) {
        rights[i] = '\0';
    }
 
    for (i = 0; i < 6; i++) {
        wrongs[i] = '\0';
    }

    while (strlen(wrongs) < 6) {
        
        printscreen(rights, wrongs, words[secretwordindex]);

        printf("\nPlease enter your guess: ");
        scanf(" %c", &guess);

        if (isletterinword(words[secretwordindex],guess)) {
            
            rights[strlen(rights)] = guess;
        }

        else {
            
            wrongs[strlen(wrongs)] = guess;
        }
       
    } 

    printscreen(rights, wrongs, words[secretwordindex]);

    if ( iswordcomplete(words[secretwordindex],rights[20])==true &&  strlen(wrongs) <= 6  ) { // The if condition here might be problematic.
        printf("You have won!\n");
    }
    else { 
        printf("You have lost!\n");
    }
}

这是错误消息:

main.c:197:48:警告:传递 'iswordcomplete' 的参数 2 使指针从没有强制转换的整数 [-Wint-conversion]
main.c:55:5: 注意:预期的 'char *' 但参数是输入“字符”</p>

4

1 回答 1

0

首先要做的事情:编译器错误是由于您将单个字符传递给对 的调用iswordcomplete(),而不是字符数组。因此,在函数末尾附近的检查中main,您需要传递rights(未修饰的)作为参数,而不是rights[20](顺便说一句,它是数组的越界元素)。此外,在那个阶段,你不需要第二次检查(计算错误的数量——见下文)。这是该部分代码的修复:

//  if (iswordcomplete(words[secretwordindex], rights[20]) == true && strlen(wrongs) <= 6) { // The if condition here might be problematic.
    if (iswordcomplete(words[secretwordindex], rights)){// && strlen(wrongs) <= 6) { // Needs the whole string as an argument
        printf("You have won!\n");
    }

现在解决一些其他问题,这些问题将阻止您的代码正常工作......

(1)在你输入 6 个“错误”字母之前,你的主while循环不会停止运行——即使你对了这个词。因此,您需要对条件添加一个检查(使用运算符†</sup>否定它),以便在单词完整时才继续运行循环。像这样:iswordcomplete()while!

    while (strlen(wrongs) < 6 && !iswordcomplete(words[secretwordindex], rights)) { // Need to break loop if we win!!

        printscreen(rights, wrongs, words[secretwordindex]);
        //... 

(2)你的函数的逻辑iswordcomplete是有缺陷的,因为一旦找到任何匹配,它就会返回“true”。相反,您需要两个循环,如果在“权限”列表中找不到任何单词的字母,则返回 false 。这是一个可能的版本:

int iswordcomplete(char secretword[], char rights[])
{
    int i, j;
    for (i = 0; i < strlen(secretword); i++) {
        for (j = 0; j < strlen(rights); j++) {
            if (secretword[i] == rights[j]) break;
        }
        if (j >= strlen(rights)) return false; // Didn't find this letter
    }
    return true;
}

请随时进一步澄清和/或解释。


†</sup> 如果您(还)不熟悉!运算符的这种用法,那么您可以显式地将函数的返回值与false常量进行比较,如果您对此更满意的话,如下所示:

while (strlen(wrongs) < 6 && iswordcomplete(words[secretwordindex], rights) == false) { // Break loop if we win!
于 2021-01-17T03:00:29.717 回答