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

C++ GUESS OR DIE类Hangman猜词游戏猜错4次后匹配提示实现问题

问题根因
  • 核心问题是你在Obj()函数中每次触发提示逻辑时,都重新调用randWord()生成了全新的随机词,和本局游戏开局时生成的目标词没有关联,所以提示内容必然是随机的。
  • 次要问题是randWord()函数中每次调用都执行srand(time(0))初始化随机数种子:time(0)精度为秒,同一秒内调用生成的随机词相同,间隔超过1秒调用就会生成不同的词,进一步加剧了提示随机的问题。
  • 额外bug:教程页返回菜单的判断逻辑if(option == 'A' || 'a')写法错误,'a'本身是真值,无论用户输入什么都会触发返回菜单的逻辑。
修复步骤
  1. 调整随机数种子初始化位置,全局仅初始化一次
    将randWord()函数中的srand(time(0));移到main()函数最开头,修改后的randWord()如下:
string randWord()
{
    string words[5] = {"PROGRAMMING", "COMPUTER", "CODER", "DEBUGGING", "COMPILE"};
    return words[rand() % 5];
}

main函数开头新增随机种子初始化:

int main()
{
    srand(time(0)); // 全局仅初始化一次随机种子
    Menu:
    // 其余原有代码保持不变
  1. 修改Obj()函数的传参逻辑,将本局目标词传入
    首先修改Obj()的函数声明:
// 原声明:int Obj(int x);
int Obj(int x, const string& reserveString);

然后修改main中调用Obj()的代码,将保存的本局目标词传入:

switch(Obj(++StickMan, reserveString))

最后修改Obj()函数实现,删除内部重新生成随机词的逻辑:

int Obj(int x, const string& reserveString)
{
    // 删掉原来这三行:
    // string randWord();
    // string rWord = randWord();
    // string reserveString = rWord;

    switch(x)
    {
        // 原有case逻辑完全保留即可,提示判断用传入的reserveString
        case 1:
            gotoxy(9, 4);
            cout << '\x1f';
            break;
        case 2:
            gotoxy(9, 4);
            cout << ' ';
            gotoxy(9, 5);
            cout << '\x1f';
            break;
        case 3:
            gotoxy(9, 4);
            cout << ' ';
            gotoxy(9, 5);
            cout << ' ';
            gotoxy(9, 6);
            cout << '\x1f';
            break;
        case 4:
            gotoxy(9, 4);
            cout << ' ';
            gotoxy(9, 5);
            cout << ' ';
            gotoxy(9, 6);
            cout << ' ';
            gotoxy(9, 7);
            cout << '\x1f';

            if(reserveString == "PROGRAMMING")
            {
                gotoxy(1, 17);
                cout << "Hint: " << "It is the process of creating a set of instructions that tell a computer how to perform a task.";
            }
            if(reserveString == "COMPUTER")
            {
                gotoxy(1, 17);
                cout << "Hint: " << "It is a programmable device for processing, storing, and displaying information.";
            }
            if(reserveString == "CODER")
            {
                gotoxy(1, 17);
                cout << "Hint: " << "It is a person who writes code for computer programs.";
            }
            if(reserveString == "DEBUGGING")
            {
                gotoxy(1, 17);
                cout << "Hint: " << "It is the process of finding and resolving bugs within computer programs, software, or systems.";
            }
            if(reserveString == "COMPILE")
            {
                gotoxy(1, 17);
                cout << "Hint: " << "It converts (a program) into a machine-code or lower-level form in which the program can be executed.";
            }

            break;
        case 5:
            return 0;
    }
    return 1; // 补充缺失的返回值,避免编译警告
}
  1. 修复教程页返回菜单的判断逻辑
// 原错误写法:if(option == 'A' || 'a')
if(option == 'A' || option == 'a')

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.23 14:54:02