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