C中的Hangman代码

时间:2014-10-29 21:27:56

标签: c scanf

我正在尝试用C编写一个刽子手程序。

有时,某些句子会在程序的输出中出现两次。该程序还不能容忍除1以外的用户输入。我该如何解决这个问题?

这是我的代码:

#include <stdio.h>
#include <string.h>

/* contant declarations */
#define NUM_TRIES_ALLOWED 10

int main() {

    /* variable declarations */
    int num_letters = 0, /* length of word char array */
    count = 0, /* for word char array       */
    tries = 0, /* total tries user has used */
    num_vis_chars = 0, /* # of visible characters   */
    correct_guesses = 0, /* # of correct guesses      */
    correct_flag = 0, /* was guess correct?        */
    repeat_flag = 0, /* was guess a repeat?       */
    choice;

    char guess, guessword;

    /* array declarations */
    char word[255] = " ";
    char incorrect_letters[255] = " ";
    /* get word */
    puts("Enter a word for player to guess.");
    gets(word);

    ("Ready to start!\n");

    num_letters = strlen(word);

    char visible_word[num_letters]; /* displays correct guesses */
    /* initialize visble_word */
    for (count = 0; count < num_letters; count++)
        visible_word[count] = '*';

    visible_word[num_letters] = '\0';

    if (guess == visible_word[count]) {

        while (tries < NUM_TRIES_ALLOWED) {

            printf("The word is: %s\n\n", visible_word);
            printf("Number of turns remaining: %d", NUM_TRIES_ALLOWED - tries);

            printf(
                    "\nWould you like to guess the word [w] or guess a letter [l]:");
            choice = getchar();

            if (choice == 'l') {

                printf("\nWhat letter have you chosen?:\t ");
                scanf(" %c", &guess);
            }

            /* match guess against previous guesses */
            for (count = 0; count < num_letters; count++)
                if (guess == visible_word[count]
                        || guess == incorrect_letters[count]) {
                    repeat_flag = 1;
                    correct_flag = 1;
                    break;
                }

            if (repeat_flag == 0)
                /* check for matches in string */
                for (count = 0; count < num_letters; count++) {
                    if (guess == word[count]) {
                        visible_word[count] = guess;
                        correct_guesses++;
                        printf(
                                "\n**************************************************************\n\n");
                        printf("Good choice!\n\n");

                        if (correct_guesses == num_letters) {
                            puts("\n\nCONGRATULATIONS! You guessed the word!");
                            printf("WORD: %s\n\n", visible_word);
                            exit(0);
                        }

                        correct_flag = 1;
                    }

                }

            if (correct_flag == 0) {
                incorrect_letters[tries] = guess;
                tries++;
                printf(
                        "\n**************************************************************\n\n");
                printf("Bad choice!\n\n");

            }

            /* reset flags */
            repeat_flag = 0;
            correct_flag = 0;

        }

        puts("You did not guess the word.");
        printf("WORD: %s\n\n", visible_word);

    }
    if (choice = 'w') {

        printf("\nWhat word have you chosen?:\t ");

        scanf("%s", &guessword);

        if (guessword == word) {
            printf("CONGRATULATIONS! You guessed the word!");
        } else {
            printf("nops");
        }
    }

    return 0;
}

1 个答案:

答案 0 :(得分:0)

我建议为此代码提供函数。代码很难阅读和遵循。首先创建菜单功能,然后获取用户输入功能;也可以在函数中进行检查,以确认输入在发送字符之前是否有效。然后检查猜测是否正确并使用函数发布响应。您会发现,如果您绘制代码并创建必要的函数,则可以更轻松地进行跟踪和调试。

print_menu()显示用户的选项和退出选择。

get_user_input()获取用户输入,并检查它是否为有效输入

print_remaining()打印正在播放的单词的剩余和猜测字符

请注意,例如,如果您从用户那里获取了一封信,他们就会输入一封信。

 scanf_s("%c", &guess, _countof(guess));   

注意:_countof(ArrayName)只是验证是否有足够的空间来放置要分配的char或字符串。确保#include&#34; stdlib.h&#34;该功能的文件。

使用scanf_s的原因是它读取空格。它不会读取错误的输入,如果您遇到一些来自用户的错误输入,它会将错误的输入放回输入缓冲区(如果来自的话)。 scanf_s()具有返回值,其值将是填充了多少正确的输入响应。这意味着如果%c实际上读取了一个char,那么返回值将为1.如果你输入一个4,它将把四个返回并返回0.读取输入的好方法。 getchar()也适用于此,但有一些详细的条件,如果不理解可能会导致一些悲伤。需要考虑的事情。

while(scanf_s("%c", &guess, _countof(guess)) != 1 )  
 // clear the input buffer and then try again.

然后你只需用

清除输入缓冲区
 while(getchar() != '\n')  
   continue;

上图:清除输入缓冲区中的所有内容,包括换行符。然后,要求用户此次输入有效输入。

如果你得到两次打印的东西,告诉我循环正在超过其退出条件。如果退出条件与读取输入有关。上面的循环将处理输入缓冲区中的任何信息chillen。希望这可以帮助。专注于仅在实际代码中调用函数。使调试更容易。

相关问题