使用C问题的Hangman游戏

时间:2013-03-16 20:52:45

标签: c

新手到C语言,我正在尝试制作一个刽子手游戏。我有以下代码:

int main(void) {

    char userResponse;//This is the response from the user on rather they want to continue or not.

    do {
        startGame();

        printf("Do you want to continue (y or n): ");
        scanf("%c", &userResponse);

    } while(userResponse == 'y');

    return 0;
}

int startGame(void) {

    int num_letters = 0; //length of word array
    int count = 0;
    int tries = 0; //total tries player has used
    int correctGuesses = 0; //number of correct guesses
    int correctLetter = 0; //flag to check if the letter guessed was correct.
    int repeatLetter = 0; //flag to check if the letter guessed was already guessed before.
    int randomWord = 1+(rand()%6); //setting a variable that will pick a word from the word array at random.

    char word[] = {"spot", "physicist", "penny", "klingon", "bazinga", "continuum"}; // should these be all lower char or does it matter?
    char guess;
    char incorrectGuesses[5] = " "; // all the incorrect letters chosen by the player will be stored in this array.
    char gameWord[num_letters]; //starts to fill out the word as the player guesses correctly

    srand(time(NULL)); // Don't really understand this line but the interweb said I needed it.  Can you tell me why?

    num_letters = strlen(word);

    gameWord[num_letters] = '\0';

    //This will show the player what letter they have typed incorrectly.
    for(count = 0; count < num_letters; count++)
        gameWord[count] = '-'; // This will display the length of the word represented with a "-".

    printf("Welcome to Big Bang Hangman!\n\n");
    printf("Time to guess the word!");

    draw_hangman(tries);

    while(tries < NUM_TRIES_ALLOWED) { //Player only gets 7 guesses.  Found this on the interweb.
        printf("Here is your word: %s\n", gameWord);
        printf("Incorrect Letters: %s\n", incorrectGuesses);
        printf("\nGuess a letter (and press 'Enter'): ");
        fgets(&guess, sizeof(guess), stdin);

        //This compares the current guess with the previous guesses so that you won't affect a turn if you pick the same incorrect letter.
       for(count = 0; count < num_letters; count++)
            if(guess == gameWord[count] || guess == incorrectGuesses[count]) { // If the player guess is equal to the size of the word or if its equal to the 
                repeatLetter  = 1;
                correctLetter = 1;
                break;
            }

        if(repeatLetter == 0) // setting the flag back to 0 tells the compiler the game is over.
        //Checks for matches in string
            for(count = 0; count < num_letters; count++) {
                if(guess == word[count]) { // can't understand why I'm getting this warrning.
                    gameWord[count] = guess;
                    correctGuesses++;

                    //if (correctGuesses == count) {
                        //printf("\n\nCONGRATULATIONS! You guessed the word %s!", gameWord);
                        //exit(0);
                    //}
                    if(correctGuesses == num_letters) {
                        printf("\n\nCONGRATULATIONS! You guessed the word %s!", gameWord);
                        exit(0);
                    }

                    correctLetter = 1;
                }
            }

        if(correctLetter == 0) {
            incorrectGuesses[tries] = guess;
            tries++;
        }

        // reset both flags
        repeatLetter  = 0;
        correctLetter = 0;

        //startGame(); // Start the game over when the flag is reset.
    }

    printf("Sorry but you did not guess the word.\n");
    printf("The word is: %s\n\n", gameWord);

    return 0;
}

输出以下内容:

欢迎来到Big Bang Hangman!

时间猜这个词!

  ___
 |
 |
 |
 |
 |
---
Here is your word: ----
Incorrect Letters:  

Guess a letter (and press 'Enter'): Here is your word: ----
Incorrect Letters:  

最后两行一直持续到我停止它为止。我完全迷失了。
任何建议都会很棒。

1 个答案:

答案 0 :(得分:0)

您的代码中有多个(我的意思是)问题。但是,我们只选择一个

char guess;

fgets(&guess, sizeof(guess), stdin);

不是让用户输入信件的正确方法。也许

scanf("%c", &guess);

会更适合你。

但实际上你的做法是错的。重新开始,写一个少数代码行,让它们工作,然后写一个少数。你已经编写了很多代码,其中很多都是错误的。这不是一种有效的编程方式。