c++小游戏 随机选取单词,猜单词背单词

#include <iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<ctime>
#include<cctype>
using namespace std;
int main()
{
    //set up
    const int MAX_WRONG=8;
    vector<string>words;
/*    words.push_back("GUESS");
    words.push_back("HANGMAN");
    words.push_back("DIFFICULT");*/
    words.push_back("YES");
    srand(static_cast<unsigned int>(time(0)));
    random_shuffle(words.begin(),words.end());
    const string THE_WORD=words[0];
    int wrong=0;
    string soFar(THE_WORD.size(),'-');
    string used=" ";
    cout<<"Welcone to Hangman.Good luck!\n";
    //main loop
    while((wrong <MAX_WRONG)&&(soFar!=THE_WORD))
    {
        cout<<"\n\nYou hace"<<(MAX_WRONG - wrong);
        cout<<"incorrent guesses left\n";
        cout<<"您使用以下字母"<<used<<endl;
        cout<<"\n到目前为止,这个词是"<<soFar<<endl;
        char guess;
        cout<<"输入您的猜测"<<endl;
        cin>>guess;
        guess=toupper(guess);
        while(used.find(guess)!=string::npos)
        {
            cout<<"您已经猜到了"<<endl;
            cout<<"Enter your guess";
            cin>>guess;
            guess=toupper(guess);
        }
        used+=guess;
        if(THE_WORD.find(guess)!=string::npos)
        {
            cout<<"That's right"<<guess<<"is in the word.\n";
            for(int i=0;i<THE_WORD.length();i++)
            {
                if(THE_WORD[i]==guess)
                {
                    soFar[i]=guess;
                }
            }
        }
        else
        {
            cout<<"Sorry,"<<guess<<"isn't in the word\n";
            ++wrong;
        }
    }
        //shut down
        if(wrong==MAX_WRONG)
        {
            cout<<"\n你是被绞死的"<<endl;
        }
        else
        {
            cout<<"\n你猜对了"<<endl;
        }
    cout<<"这个词是"<<THE_WORD<<endl;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

逆向爱好者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值