刽子手:如果玩家猜到所有字母,就会结束游戏

时间:2013-11-22 17:49:19

标签: c

我正在为C类编程做一个hangman程序。我似乎无法弄清楚我将如何做到最后一步,即在玩家猜出所有正确的字母后停止游戏。 我觉得问题出在播放功能中。

#include    <stdio.h>
#include    <string.h>
#include    <ctype.h>
// MAXWORD, which will be the max word length 
#define     MAXWORD     20
// 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 );

    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( i == INCORRECT_GUESSES-1)
        {
            printf( "\nSorry, you're out of guesses\nBetter luck next time!\n" ); 
        }

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

1 个答案:

答案 0 :(得分:1)

获胜者意味着您已经完成了比赛。因此,除了打印"\nYou have won",您需要在玩家获胜后从return函数play

// ...
    if( i == INCORRECT_GUESSES-1)
    {
        printf( "\nSorry, you're out of guesses\nBetter luck next time!\n" ); 
        return;  // <- player lost, return
    }
// ...
    else
    {
        printf( "\nYou have won!" );     
        return;  // <- player won, return
    }
}