刽子手:C中的初学者

时间:2013-11-22 20:43:31

标签: c

我似乎无法弄清楚为什么当用户猜出所有字母时我的程序不会结束游戏。我知道问题出在play()函数中。有关播放功能中有什么变化的指针或导致此问题的任何内容?

#include    <stdio.h>
#include    <string.h>
#include    <ctype.h>
// MAXWORD, which will be the max word length 
#define     MAXWORD     10
// INCORRECT_GUESSES, which will be the max guesses
#define     INCORRECT_GUESSES   5

/* Prototypes */

// Fills theArray with howMany copies of theLetter
void    fill_array( char *theArray, int howMany, char theLetter );

// Get char from player, checks the letter, shows progress so far
int     get_letter( char *theWord, char *soFar );

// Check if letter is in word, updates progress so far
int     letter_in_word( char *theWord, char *soFar, char theLetter );

// Convert the word to lowercase
void    lower_string( char *someWord );

// Play one game
void    play( char *theWord );

/* Function definitions */

int main( )
{
    char theWord [ MAXWORD ];
    FILE*   word;
    word = fopen( "guesswords.txt", "r" );

    if ( word == NULL )
    {
        printf( "No input file found..........\n" );
        return -1;
    }
    printf("Want to play a game?\n");
    fscanf( word, "%s", theWord );

    lower_string( theWord );

    play( theWord );
    fprintf( word, "%s", theWord);
    fclose( word );
    return 0;
}

// Get char from player, checks the letter, shows progress so far
int get_letter( char *theWord, char *soFar )
{
    char theLetter;
    printf("\nPlease enter a letter: ");
    scanf( " %c", &theLetter );
    theLetter = tolower(theLetter);

    letter_in_word( theWord, soFar, theLetter );

    return theLetter;
}

// Fills theArray with howMany copies of theLetter
void fill_array( char *theArray, int howMany, char theLetter )
{
    int i;
    for( i=0; i<howMany; i++ )
    {
        theArray[i]= theLetter;
        *(theArray + i) = theLetter;
        *theArray = theLetter;
    }
    theArray[howMany] = '\0';
}

// Check if letter is in word, updates progress so far
int letter_in_word( char *theWord, char *soFar, char theLetter )
{
    int i;
    int num=0;
    int len = strlen(theWord);

    for( i=0; i<len; i++)
    {
        if (theWord[i] == theLetter )
        {
            soFar[i] = theLetter;
            num++;
        }
    }
    if (num == 0)
    {
        printf( "\nSORRY! your letter is not in the word\n" );
        printf("%s\n", soFar);
        return 0;

    }
    else if (num>0)
    {
        printf( "\nCongratz! your letter was in the word\n" );
        printf("%s\n", soFar);
        return 1;
    }
}

// Convert the word to lowercase
void lower_string( char *someWord )
{
    int i, cha;
    int len = strlen( someWord );
    for( i=0; i<len; i++ )
    {
        cha = someWord[i];
        cha = tolower(cha);
        someWord[i] = cha;
    }
}

// Play one game
void play( char *theWord )
{
    int i;
    int len = strlen(theWord);
    int guess = INCORRECT_GUESSES;
    int result = 0;
    char soFar[MAXWORD];
    fill_array( soFar, len, '*');
    printf( "Guess this word: %s\n", soFar ); 

    for( i=0; i<INCORRECT_GUESSES; i++ ) 
    {
        guess = INCORRECT_GUESSES - (i+1);
        get_letter( theWord, soFar );

        if(get_letter<0)
        {
            printf( "\nYou have %d guesses left\n", guess);
        }
        else
        {
            printf( "\nYou have guessed all the letters! You have won!" );
            return;
        }

        if( i == INCORRECT_GUESSES-1)
        {
            printf( "\nSorry, you're out of guesses\nBetter luck next time!\n" ); 
        }
    }
}

2 个答案:

答案 0 :(得分:2)

play

确实存在问题
    get_letter( theWord, soFar );

    if(get_letter<0)
    {
        printf( "\nYou have %d guesses left\n", guess);
    }

get_letter是一个函数,当您使用()调用它时,它会返回一个值。你忽略了这个值。然后,您询问get_letter函数)是否小于0.相反,您需要指定get_letter的结果或直接使用它:

    if (get_letter( theWord, soFar ) < 0)
    {
        printf( "\nYou have %d guesses left\n", guess);
    }

但是你也肯定在代码中还有其他问题。我建议您在程序运行时尝试逐步执行该程序并查看值并查看代码是如何工作的。

答案 1 :(得分:0)

您似乎没有对函数的返回值执行任何操作。例如:

letter_in_word( theWord, soFar, theLetter );

此函数返回一个整数值(0或1),但您没有将其分配给任何内容,因此此信息将丢失。

同样的事情:

get_letter( theWord, soFar );