猜单词系列改动

为尊重C++的STL标准模版库的标准和国际OOP规定

tolower(char a)函数为cctype库函数

在版本0.88.2中删去的cctype头文件正式增回。

因纯属个人习惯原因,不为其新增版本号。

更改后版本1.88.3

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <cctype>
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;
}

1.88.3.3

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <cctype>
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;
            cout << "Will you play again?<y/n>";
            cin >> play;
            play = tolower(play);
            continue;
        }
        size_t 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;
            }
            size_t 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, static_cast<basic_string<char, char_traits<char>, allocator<char>>::size_type>(loc) + 1);
                while (loc != string::npos)
                {
                    attempt[loc] = letter;
                    loc = target.find(letter, static_cast<basic_string<char, char_traits<char>, allocator<char>>::size_type>(loc) + 1);
                }
            }
            cout << "Your word: " << attempt << endl;
            if (attempt != target)
            {
                if (badchars.length())
                    cout << "Bad choice: " << badchars << endl;
                cout << guesses << " bad guess left\n";
            }
        }
        if (guesses)
            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;
}

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值