猜单词游戏更新啦 (0.88.2及1.88.3)

0.88.2:

更新内容:

1.减少了不必要的cctype头文件;

2.增加了16个词语;

3.将time()函数的参数改为了NULL。

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
const int NUM=88;
const string wordlist[NUM]={"racket","badminton","panda","deep","early","prepare","gift","chocolate","fashion","designer","keyboard","feel","medal","modern","middle","quilt","dictionary","difficult","future","mainly","autumn","butterfly","greedy","useful","dancer","businessman","view","best","interesting","view","space","travel","bridge","telephone","tree","sing","player","change","juice","soccer","tennis","morning","afternoon","night","evening","season","spring","summer","winter","fall","leave","monday","tuesday","thursday","friday","wednesday","saturday","sunday","january","february","march","april","may","june","july","augest","september","october","november","december","favourite","factory","sale","male","trousers","skirt","million","desert","noon","were","often","never","seldom","son","daughter","danger","just","hurt"};
int main()
{
    cout<<"----------------Hand Man----------------\n";
    srand(time(NULL));
    char play;
    cout<<"Will you play a word game?<y/n>";
    cin>>play;
    play=tolower(play);
    while(play=='y')
    {
        string target=wordlist[rand()%NUM];
        int length=target.length();
        string attempt(length,'-');
        string badchars;
        int guesses=6;
        cout<<"Guess my secret word.It has "<<length<<" letters ,and you guess\none letter at a time .You get "<<guesses<<" wrong guesses.\n";
        cout<<"Your word: "<<attempt<<endl;
        while(guesses&&attempt!=target)
        {
            char letter;
            cout<<"Guess a letter: ";
            cin>>letter;
            if(badchars.find(letter)!=string::npos||attempt.find(letter)!=string::npos)
            {
                cout<<"You already guessed that.Try again.\n";
                continue;
            }
            int loc=target.find(letter);
            if(loc==string::npos)
            {
                cout<<"Oh,bad guess!\n";
                guesses--;
                badchars+=letter;
            }
            else
            {
                cout<<"Good guess!\n";
                attempt[loc]=letter;
                loc=target.find(letter,loc+1);
                while(loc!=string::npos)
                {
                    attempt[loc]=letter;
                    loc=target.find(letter,loc+1);
                }
            }
            cout<<"Your word: "<<attempt<<endl;
            if(attempt!=target)
            {
                if(badchars.length()>0)
                    cout<<"Bad choice: "<<badchars<<endl;
                cout<<guesses<<" bad guess left\n";
            }
        }
        if(guesses>0)
            cout<<"That's right!\n";
        else
            cout<<"Sorry,the word is "<<target<<".\n";
        cout<<"Will you play another?<y/n>";
        cin>>play;
        play=tolower(play);
    }
    cout<<"Thanks for your playing.Bye!\n";
    return 0;
}

1.87.3:

更新内容:

增加了1/89的直接获胜概率

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
const int NUM=89;
const string wordlist[NUM]={"racket","badminton","panda","deep","early","prepare","gift","chocolate","fashion","designer","keyboard","feel","medal","modern","middle","quilt","dictionary","difficult","future","mainly","autumn","butterfly","greedy","useful","dancer","businessman","view","best","interesting","view","space","travel","bridge","telephone","tree","sing","player","change","juice","soccer","tennis","morning","afternoon","night","evening","season","spring","summer","winter","fall","leave","monday","tuesday","thursday","friday","wednesday","saturday","sunday","january","february","march","april","may","june","july","augest","september","october","november","december","favourite","factory","sale","male","trousers","skirt","million","desert","noon","were","often","never","seldom","son","daughter","danger","just","hurt",""};
int main()
{
    cout<<"----------------Hand Man----------------\n";
    srand(time(NULL));
    char play;
    cout<<"Will you play a word game?<y/n>";
    cin>>play;
    play=tolower(play);
    while(play=='y')
    {
        string target=wordlist[rand()%NUM];
        if(target=="")
        {
            cout<<"Well,you are so luck,you win!"<<endl;
            continue;
        }
        int length=target.length();
        string attempt(length,'-');
        string badchars;
        int guesses=6;
        cout<<"Guess my secret word.It has "<<length<<" letters ,and you guess\none letter at a time .You get "<<guesses<<" wrong guesses.\n";
        cout<<"Your word: "<<attempt<<endl;
        while(guesses&&attempt!=target)
        {
            char letter;
            cout<<"Guess a letter: ";
            cin>>letter;
            if(badchars.find(letter)!=string::npos||attempt.find(letter)!=string::npos)
            {
                cout<<"You already guessed that.Try again.\n";
                continue;
            }
            int loc=target.find(letter);
            if(loc==string::npos)
            {
                cout<<"Oh,bad guess!\n";
                guesses--;
                badchars+=letter;
            }
            else
            {
                cout<<"Good guess!\n";
                attempt[loc]=letter;
                loc=target.find(letter,loc+1);
                while(loc!=string::npos)
                {
                    attempt[loc]=letter;
                    loc=target.find(letter,loc+1);
                }
            }
            cout<<"Your word: "<<attempt<<endl;
            if(attempt!=target)
            {
                if(badchars.length()>0)
                    cout<<"Bad choice: "<<badchars<<endl;
                cout<<guesses<<" bad guess left\n";
            }
        }
        if(guesses>0)
            cout<<"That's right!\n";
        else
            cout<<"Sorry,the word is "<<target<<".\n";
        cout<<"Will you play another?<y/n>";
        cin>>play;
        play=tolower(play);
    }
    cout<<"Thanks for your playing.Bye!\n";
    return 0;
}

无敌beta版2.101.4将在12月底进行粉丝内测!!!

届时将修复部分把std::basic_string<char>::size_type(unsigned long)赋值为int的问题

猜你喜欢

转载自blog.csdn.net/algorithmyyds/article/details/121588061