c - 我不知道如何用 C 语言编写刽子手游戏

标签 c

我是 C 编程初学者。我有两个问题! 1. 当我使用命令 guess = getchar(); 获取 char 的输入时,它第一次获取输入,但在下次循环运行时跳过!这就是为什么我使用数组来存储用户输入的字符! 2. 我的代码运行良好,但我不知道如何在这个游戏中打印 Hangman。任何想法? 问候!

#include <stdio.h>
#include <conio.h>
#include <string.h>
#define interface for (int a = 0; a < 50 ; a++) printf("*");
#define space for (int a = 0; a < 15 ; a++) printf (" ");
void MENU()
{
    interface;
    printf("\n");
    space;
    printf("Welcome to HANGMAN GAME!\n");
    interface;
    printf("\n");
    space;
    printf("Rules\n- Guess a single letter!\n- Number of lives are 5 in number!\n");
    printf("- Wrong guess will cause you to loose one life!\n- Repetition of a letter will cause you to loose a life!\n");
    printf("- To quit in middle of the game. Type in quit!\n");
}
int main(){
    int wordlength, index, oldcorrect;
    char guess;
    srand(time(NULL));
    char guesschecker[10] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
    char guessword[][10] = {
        "windows",
        "cupboard",
        "sharpener",
        "lifeguard" };
    char letterguessed[10];
    int correctguess = 0;
    int lives = 5;
    int loopindex;
    int quit = 0;
    MENU();
    index = rand() % 4;
    wordlength = strlen(guessword[index]);
    /*
    printf("\nindex: %d word : %s wrodlength : %d\n", index, guessword[index], wordlength);
    */
    while (correctguess < wordlength){
        printf("\nHangman word: ");
        for (loopindex = 0; loopindex < wordlength; loopindex++)
        {
            if (guesschecker[loopindex] == 1){
                printf("%c ", guessword[index][loopindex]);
            }
            else printf("_ ");
        }
            printf("\n\nLetter correct so far : %d", correctguess);
            printf("\nEnter your guess letter : ");
            fgets(letterguessed, 10, stdin);
            if (strncmp(letterguessed, "quit", 4) == 0){
                quit = 1;
                break;
            }
            guess = letterguessed[0];

            printf("Your guess is : %c\n", guess);
            oldcorrect = correctguess;
            for (loopindex = 0; loopindex < wordlength; loopindex++)
            {
                if (guesschecker[loopindex] == 1){
                    continue;
                }
                if (guess == guessword[index][loopindex])
                {
                    guesschecker[loopindex] = 1;
                    correctguess++;
                }
            }
            if (oldcorrect == correctguess)
            {
                lives--;
                printf("Wrong guess!\n");
                if (lives == 0)
                    break;
            }
            else printf("Correct guess!\n");
            printf("Your lives left are : %d", lives);
        }
        if (quit == 1)
            printf("You quit early!");
        else if (lives == 0){
            printf("Sorry You are out of lives!\n");
            printf("The correct word was : %s", guessword[index]);
        }
        else printf("You Won! Yayy!");
        _getch();
        return 0;
    }

最佳答案

  1. 只需在 guess=getchar(); 之后添加 getchar(); 即可修复该问题。
  2. 您必须使用多个 printfif 手动执行此操作

关于c - 我不知道如何用 C 语言编写刽子手游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28124850/

相关文章:

c - 在 C 中强制无序结构字段

c - 适用于 Windows 的 Linux 子系统中的线程差异

c - 有没有更简洁的方法来可靠地使用 write() 函数?

c - 返回类型是否节省空间或时间

c - 操作文件时如何避免 msvcr100.dll 依赖?

C++ 单行注释后跟\transforms 在多行注释中

c - 从本应作为 init 进程运行的 C 程序关闭 Linux

c - 如何修复C中的 "Error enabling this function: Address exceeds the allowed range"

c - FFmpeg:avcodec_encode_video() 和 JPEG 图像

c - C 中的字符串解析 - 带格式说明符的字符串