首页 / 💻 技术交流 / <p>#include <code>&l
💻 技术交流

<p>#include <code>&lt;iostream&gt;</code> <br /> #...

#include <iostream> 
#include <string> 
#include <vector> 
#include <fstream> 
#include <cstdlib> 
#include <ctime> 
#include <cctype> 

using namespace std;

// 单词列表
const vector <string>  WORDS = {
"brown","correct","duck","elephant","flood","giant","home","insect","jocker","kingdom","life","moon","night","open","person",
"question","rest","super","temple","unlock","value","weather","xray","young","zebra","beautiful", "brave", "bright", "calm",
"clever","cool", "curious", "eager", "elegant", "fierce","friendly", "gentle", "glad", "graceful", "happy","honest", "humble",
"joyful", "kind", "lively","loyal", "lucky", "mighty", "noble", "patient","peaceful", "proud", "pure", "quick", "quiet",
"rapid", "rare", "rich", "rough", "sad","safe", "shy", "silent", "simple", "smooth","soft", "strong", "sweet", "swift",
"tall","tender", "tough", "warm", "wise", "young", "bird", "book", "bridge", "castle","cloud", "diamond", "dream", "eagle",
"earth","fire", "flower", "forest", "garden", "gold","heart", "hill", "horse", "island", "king","lake", "light", "lion",
"moon", "mountain","ocean", "palace", "path", "pearl", "planet","rose", "sea", "shadow", "ship", "silver","sky", "star",
"stone", "storm", "sun","tower","tree", "valley", "water", "wind"
};

// 吊死鬼图案(7个阶段,0-6次错误)
void drawHangman(int wrong) {
cout << " +---+" << endl;
cout << " | |" << endl;
cout << " " << (wrong >= 1 ? "O" : " ") << " |" << endl;
cout << " " << (wrong >= 3 ? "/" : " ") << (wrong >= 2 ? "|" : " ") << (wrong >= 4 ? "" : " ") << " |" << endl;
cout << " " << (wrong >= 5 ? "/" : " ") << " " << (wrong >= 6 ? "" : " ") << " |" << endl;
cout << " |" << endl;
cout << "=========" << endl;
}

int main() {
srand(static_cast <unsigned> (time(nullptr)));

// 随机选择一个单词
string word = WORDS[rand() % WORDS.size()];
string guessed(word.length(), '_');   // 当前猜出的残缺单词
string wrongLetters;                 // 猜错的字母
int maxWrong = 6;                    // 最大错误次数
int wrong = 0;                       // 当前错误次数

cout << "===== 猜单词游戏 =====" << endl;
cout << "单词长度: " << word.length() << " 个字母,错 " << maxWrong << " 次就输了!" << endl;

while (wrong < maxWrong && guessed != word) {
    drawHangman(wrong);
    cout << "当前: " << guessed << endl;
    if (!wrongLetters.empty())
        cout << "猜错: " << wrongLetters << endl;
    cout << "剩余机会: " << maxWrong - wrong << endl;

    cout << "请输入一个字母: ";
    char letter;
    cin >> letter;
    letter = tolower(letter);   // 统一转小写

    // 检查是否已猜过
    if (guessed.find(letter) != string::npos || wrongLetters.find(letter) != string::npos) {
        cout << "你已经猜过这个字母了,换个试试~" << endl;
        continue;
    }

    // 判断字母是否在单词中
    if (word.find(letter) != string::npos) {
        // 在单词中,更新 guessed
        for (size_t i = 0; i < word.length(); ++i) {
            if (word[i] == letter)
                guessed[i] = letter;
        }
        cout << "猜对了!" << endl;
    } else {
        // 猜错
        wrong++;
        wrongLetters += letter;
        cout << "很遗憾,单词中没有 '" << letter << "'!" << endl;
    }
    cout << endl;
}

drawHangman(wrong);
if (guessed == word) {
    cout << "恭喜你,猜对了!单词是: " << word << endl;
} else {
    cout << "游戏结束,你输了!单词是: " << word << endl;
}

return 0;

}

💬 全部评论 2

共 1 条 · 按楼层排序
登录 注册 后即可发表评论
a194g988 楼主 Lv.2 一砖一瓦
29天前
#1 楼
没囤货了,断更几天
👍 1 💰 打赏