C++PrimerPlus(第6版)中文版:Chapter16.1string类_16.3hangman.cpp

这是一个历史悠久的小游戏,早在维多利亚时代就已流行起来,规则简单且有趣,在7步之内猜出单词,否则小人将会“上吊”。

Hangman直译为“上吊的人”,是一个猜单词的双人游戏。由一个玩家想出一个单词或短语,另一个玩家猜该单词或短语中的每一个字母 [1]  。第一个人抽走单词或短语,只留下相应数量的空白与下划线。

通过这个例子可以学习string.find() 的用法.

代码如下:

hangman.cpp

// Example16.3hangman.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <cctype>
using std::string;
const int NUM = 26;
const string wordlist[NUM] = { "apiary","beetle", "cereal",
"danger", "ensign", "florid", "garage", "health", "insult",
"jackal", "keeper", "loaner", "manage", "nonce","onset",
"plaid", "quilt", "remote", "stolid", "train", "useful",
"valid", "whence", "xenon", "yearn", "zippy" };
int main()
{
    //std::cout << "Hello World!\n";
    using std::cout;
    using std::cin;
    using std::tolower;
    using std::endl;
    std::srand(std::time(0));
    char play;
    cout << "Will you play a word game? <y/n>";
    cin >> play;
    play = tolower(play);
    while (play == 'y')
    {
        string target = wordlist[std::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 \n"<<"one letter at a time.You get "<<guesses<<"wrong guesses.\n";
        cout << "Your word: " << attempt << endl;
        while (guesses>0&&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;//add to string 
            }
            else
            {
                cout << "Good guess!\n";
                attempt[loc] = letter;
                //check if letter appears again.
                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 choices: " << badchars << endl; }
                cout << guesses << " bad guess left \n";
            }
        }
        //***以上是while 循环语句块***以上是while 循环语句块***以上是while 循环语句块***
        if (guesses > 0)
            cout << "That is right!\n";
        else
            cout << "Sorry,the word is " << target << ".\n";
        cout << "Will you play another ? <y/n>";
        cin >> play;
        play = tolower(play);
    }
    cout << "Bye \n";
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值