You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C语言Hangman游戏用户输入与胜利条件问题求助

修复Hangman游戏的输入处理、胜利判断与机会扣除问题

原代码的核心问题

  • 输入处理异常:scanf("%c*c", &userinput); 格式字符串错误,且未处理输入缓冲区的换行符,导致程序跳过输入或读取错误字符。
  • 机会扣除逻辑错误:遍历单词每个字符时,只要当前字符不匹配就扣减机会,一次输入会被多次扣减(比如单词长度5,没匹配的话直接扣5次机会)。
  • 胜利/失败条件判断混乱:在字符遍历循环内反复修改gameover变量,逻辑矛盾,无法正确判断游戏结束状态。
  • 未限制单次输入单个字符:未处理用户输入多个字符的情况,多余字符会残留到下一次输入。

修复后的完整代码

#include <stdio.h>
#include <time.h>
#include "./random.h"
#include <stdbool.h>
#include <windows.h>
#include <stdlib.h>
#define ARRAY_SIZE sizeof(words) / sizeof(words[0])

int main()
{
    srand(time(NULL));
    char *words[] = {
        "hello",
        "superman"};
    char *random_word = random(ARRAY_SIZE, words);
    char userinput;
    bool gameover = false;
    int length = 0;
    int index;
    int chances = 5;
    char *empty_list = (char *)malloc(sizeof(char) * 100);
    if (empty_list == NULL) {
        printf("内存分配失败\n");
        return 1;
    }
    char letter;

    // 计算目标单词长度
    while (random_word[length] != '\0')
    {
        length++;
    }
    // 初始化下划线列表
    for (index = 0; index < length; index++)
    {
        empty_list[index] = '_';
    }
    empty_list[length] = '\0';

    printf("\n%s\n", empty_list);

    while (!gameover)
    {
        bool is_matched = false;
        printf("Guess the word: ");
        // 读取单个字符,跳过空白字符(包括换行)
        scanf(" %c", &userinput);
        // 清空输入缓冲区,处理用户输入多个字符的情况
        while(getchar() != '\n');

        // 遍历目标单词,检查输入字符是否匹配
        for (index = 0; index < length; index++)
        {
            letter = random_word[index];
            if (letter == userinput)
            {
                empty_list[index] = letter;
                is_matched = true;
            }
        }

        // 处理匹配结果
        if (is_matched)
        {
            printf("\n%s\n", empty_list);
            Beep(720, 300);
        }
        else
        {
            chances--;
            printf("\n错误!剩余机会:%d\n", chances);
        }

        // 检查胜利条件:下划线全部被替换
        bool has_underscore = false;
        for (index = 0; index < length; index++)
        {
            if (empty_list[index] == '_')
            {
                has_underscore = true;
                break;
            }
        }
        if (!has_underscore)
        {
            printf("\nYOU WON!\n");
            gameover = true;
        }
        // 检查失败条件:机会耗尽
        else if (chances <= 0)
        {
            printf("\nYOU LOST! 正确单词是:%s\n", random_word);
            gameover = true;
        }
    }

    free(empty_list);
    return 0;
}

关键修改说明

  1. 输入处理修复:

    • 改用 scanf(" %c", &userinput);,前置空格自动跳过所有空白字符(换行、空格等),确保读取有效单个字符。
    • 新增while(getchar() != '\n');清空输入缓冲区,避免用户输入多个字符时,多余字符残留影响后续输入。
  2. 机会扣除逻辑修复:

    • 新增is_matched标记,遍历完整单词后,仅当完全未匹配时才扣减一次机会,解决单次输入多次扣减的问题。
  3. 胜利/失败条件修复:

    • 单独遍历empty_list检查是否还有下划线,无下划线则判定胜利。
    • 机会耗尽时直接判定失败,并显示正确单词。
    • 将游戏结束状态的判断移到字符遍历循环外,逻辑清晰无矛盾。
  4. 内存安全优化:

    • 增加malloc失败的判断,避免空指针操作。
    • 程序结束前释放empty_list,避免内存泄漏。

内容的提问来源于stack exchange,提问作者IllTime00qw

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 13:20:20